Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   7  use SimpleXMLElement;
   8  
   9  class SheetViews extends BaseParserClass
  10  {
  11      private $sheetViewXml;
  12  
  13      private $worksheet;
  14  
  15      public function __construct(SimpleXMLElement $sheetViewXml, Worksheet $workSheet)
  16      {
  17          $this->sheetViewXml = $sheetViewXml;
  18          $this->worksheet = $workSheet;
  19      }
  20  
  21      public function load(): void
  22      {
  23          $this->zoomScale();
  24          $this->view();
  25          $this->gridLines();
  26          $this->headers();
  27          $this->direction();
  28          $this->showZeros();
  29  
  30          if (isset($this->sheetViewXml->pane)) {
  31              $this->pane();
  32          }
  33          if (isset($this->sheetViewXml->selection, $this->sheetViewXml->selection['sqref'])) {
  34              $this->selection();
  35          }
  36      }
  37  
  38      private function zoomScale(): void
  39      {
  40          if (isset($this->sheetViewXml['zoomScale'])) {
  41              $zoomScale = (int) ($this->sheetViewXml['zoomScale']);
  42              if ($zoomScale <= 0) {
  43                  // setZoomScale will throw an Exception if the scale is less than or equals 0
  44                  // that is OK when manually creating documents, but we should be able to read all documents
  45                  $zoomScale = 100;
  46              }
  47  
  48              $this->worksheet->getSheetView()->setZoomScale($zoomScale);
  49          }
  50  
  51          if (isset($this->sheetViewXml['zoomScaleNormal'])) {
  52              $zoomScaleNormal = (int) ($this->sheetViewXml['zoomScaleNormal']);
  53              if ($zoomScaleNormal <= 0) {
  54                  // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0
  55                  // that is OK when manually creating documents, but we should be able to read all documents
  56                  $zoomScaleNormal = 100;
  57              }
  58  
  59              $this->worksheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal);
  60          }
  61      }
  62  
  63      private function view(): void
  64      {
  65          if (isset($this->sheetViewXml['view'])) {
  66              $this->worksheet->getSheetView()->setView((string) $this->sheetViewXml['view']);
  67          }
  68      }
  69  
  70      private function gridLines(): void
  71      {
  72          if (isset($this->sheetViewXml['showGridLines'])) {
  73              $this->worksheet->setShowGridLines(
  74                  self::boolean((string) $this->sheetViewXml['showGridLines'])
  75              );
  76          }
  77      }
  78  
  79      private function headers(): void
  80      {
  81          if (isset($this->sheetViewXml['showRowColHeaders'])) {
  82              $this->worksheet->setShowRowColHeaders(
  83                  self::boolean((string) $this->sheetViewXml['showRowColHeaders'])
  84              );
  85          }
  86      }
  87  
  88      private function direction(): void
  89      {
  90          if (isset($this->sheetViewXml['rightToLeft'])) {
  91              $this->worksheet->setRightToLeft(
  92                  self::boolean((string) $this->sheetViewXml['rightToLeft'])
  93              );
  94          }
  95      }
  96  
  97      private function showZeros(): void
  98      {
  99          if (isset($this->sheetViewXml['showZeros'])) {
 100              $this->worksheet->getSheetView()->setShowZeros(
 101                  self::boolean((string) $this->sheetViewXml['showZeros'])
 102              );
 103          }
 104      }
 105  
 106      private function pane(): void
 107      {
 108          $xSplit = 0;
 109          $ySplit = 0;
 110          $topLeftCell = null;
 111  
 112          if (isset($this->sheetViewXml->pane['xSplit'])) {
 113              $xSplit = (int) ($this->sheetViewXml->pane['xSplit']);
 114          }
 115  
 116          if (isset($this->sheetViewXml->pane['ySplit'])) {
 117              $ySplit = (int) ($this->sheetViewXml->pane['ySplit']);
 118          }
 119  
 120          if (isset($this->sheetViewXml->pane['topLeftCell'])) {
 121              $topLeftCell = (string) $this->sheetViewXml->pane['topLeftCell'];
 122          }
 123  
 124          $this->worksheet->freezePane(
 125              Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
 126              $topLeftCell
 127          );
 128      }
 129  
 130      private function selection(): void
 131      {
 132          $sqref = (string) $this->sheetViewXml->selection['sqref'];
 133          $sqref = explode(' ', $sqref);
 134          $sqref = $sqref[0];
 135  
 136          $this->worksheet->setSelectedCells($sqref);
 137      }
 138  }