Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   6  use SimpleXMLElement;
   7  
   8  class SheetViewOptions extends BaseParserClass
   9  {
  10      /** @var Worksheet */
  11      private $worksheet;
  12  
  13      /** @var ?SimpleXMLElement */
  14      private $worksheetXml;
  15  
  16      public function __construct(Worksheet $workSheet, ?SimpleXMLElement $worksheetXml = null)
  17      {
  18          $this->worksheet = $workSheet;
  19          $this->worksheetXml = $worksheetXml;
  20      }
  21  
  22      public function load(bool $readDataOnly, Styles $styleReader): void
  23      {
  24          if ($this->worksheetXml === null) {
  25              return;
  26          }
  27  
  28          if (isset($this->worksheetXml->sheetPr)) {
  29              $sheetPr = $this->worksheetXml->sheetPr;
  30              $this->tabColor($sheetPr, $styleReader);
  31              $this->codeName($sheetPr);
  32              $this->outlines($sheetPr);
  33              $this->pageSetup($sheetPr);
  34          }
  35  
  36          if (isset($this->worksheetXml->sheetFormatPr)) {
  37              $this->sheetFormat($this->worksheetXml->sheetFormatPr);
  38          }
  39  
  40          if (!$readDataOnly && isset($this->worksheetXml->printOptions)) {
  41              $this->printOptions($this->worksheetXml->printOptions);
  42          }
  43      }
  44  
  45      private function tabColor(SimpleXMLElement $sheetPr, Styles $styleReader): void
  46      {
  47          if (isset($sheetPr->tabColor)) {
  48              $this->worksheet->getTabColor()->setARGB($styleReader->readColor($sheetPr->tabColor));
  49          }
  50      }
  51  
  52      private function codeName(SimpleXMLElement $sheetPrx): void
  53      {
  54          $sheetPr = $sheetPrx->attributes() ?? [];
  55          if (isset($sheetPr['codeName'])) {
  56              $this->worksheet->setCodeName((string) $sheetPr['codeName'], false);
  57          }
  58      }
  59  
  60      private function outlines(SimpleXMLElement $sheetPr): void
  61      {
  62          if (isset($sheetPr->outlinePr)) {
  63              $attr = $sheetPr->outlinePr->attributes() ?? [];
  64              if (
  65                  isset($attr['summaryRight']) &&
  66                  !self::boolean((string) $attr['summaryRight'])
  67              ) {
  68                  $this->worksheet->setShowSummaryRight(false);
  69              } else {
  70                  $this->worksheet->setShowSummaryRight(true);
  71              }
  72  
  73              if (
  74                  isset($attr['summaryBelow']) &&
  75                  !self::boolean((string) $attr['summaryBelow'])
  76              ) {
  77                  $this->worksheet->setShowSummaryBelow(false);
  78              } else {
  79                  $this->worksheet->setShowSummaryBelow(true);
  80              }
  81          }
  82      }
  83  
  84      private function pageSetup(SimpleXMLElement $sheetPr): void
  85      {
  86          if (isset($sheetPr->pageSetUpPr)) {
  87              $attr = $sheetPr->pageSetUpPr->attributes() ?? [];
  88              if (
  89                  isset($attr['fitToPage']) &&
  90                  !self::boolean((string) $attr['fitToPage'])
  91              ) {
  92                  $this->worksheet->getPageSetup()->setFitToPage(false);
  93              } else {
  94                  $this->worksheet->getPageSetup()->setFitToPage(true);
  95              }
  96          }
  97      }
  98  
  99      private function sheetFormat(SimpleXMLElement $sheetFormatPrx): void
 100      {
 101          $sheetFormatPr = $sheetFormatPrx->attributes() ?? [];
 102          if (
 103              isset($sheetFormatPr['customHeight']) &&
 104              self::boolean((string) $sheetFormatPr['customHeight']) &&
 105              isset($sheetFormatPr['defaultRowHeight'])
 106          ) {
 107              $this->worksheet->getDefaultRowDimension()
 108                  ->setRowHeight((float) $sheetFormatPr['defaultRowHeight']);
 109          }
 110  
 111          if (isset($sheetFormatPr['defaultColWidth'])) {
 112              $this->worksheet->getDefaultColumnDimension()
 113                  ->setWidth((float) $sheetFormatPr['defaultColWidth']);
 114          }
 115  
 116          if (
 117              isset($sheetFormatPr['zeroHeight']) &&
 118              ((string) $sheetFormatPr['zeroHeight'] === '1')
 119          ) {
 120              $this->worksheet->getDefaultRowDimension()->setZeroHeight(true);
 121          }
 122      }
 123  
 124      private function printOptions(SimpleXMLElement $printOptionsx): void
 125      {
 126          $printOptions = $printOptionsx->attributes() ?? [];
 127          if (isset($printOptions['gridLinesSet']) && self::boolean((string) $printOptions['gridLinesSet'])) {
 128              $this->worksheet->setShowGridlines(true);
 129          }
 130          if (isset($printOptions['gridLines']) && self::boolean((string) $printOptions['gridLines'])) {
 131              $this->worksheet->setPrintGridlines(true);
 132          }
 133          if (isset($printOptions['horizontalCentered']) && self::boolean((string) $printOptions['horizontalCentered'])) {
 134              $this->worksheet->getPageSetup()->setHorizontalCentered(true);
 135          }
 136          if (isset($printOptions['verticalCentered']) && self::boolean((string) $printOptions['verticalCentered'])) {
 137              $this->worksheet->getPageSetup()->setVerticalCentered(true);
 138          }
 139      }
 140  }