Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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