Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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