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.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Writer\XLSX\Entity;
   6  
   7  use OpenSpout\Common\Exception\InvalidArgumentException;
   8  use OpenSpout\Reader\XLSX\Helper\CellHelper;
   9  
  10  final class SheetView
  11  {
  12      private bool $showFormulas = false;
  13      private bool $showGridLines = true;
  14      private bool $showRowColHeaders = true;
  15      private bool $showZeroes = true;
  16      private bool $rightToLeft = false;
  17      private bool $tabSelected = false;
  18      private bool $showOutlineSymbols = true;
  19      private bool $defaultGridColor = true;
  20      private string $view = 'normal';
  21      private string $topLeftCell = 'A1';
  22      private int $colorId = 64;
  23      private int $zoomScale = 100;
  24      private int $zoomScaleNormal = 100;
  25      private int $zoomScalePageLayoutView = 100;
  26      private int $workbookViewId = 0;
  27      private int $freezeRow = 0;
  28      private string $freezeColumn = 'A';
  29  
  30      /**
  31       * @return $this
  32       */
  33      public function setShowFormulas(bool $showFormulas): self
  34      {
  35          $this->showFormulas = $showFormulas;
  36  
  37          return $this;
  38      }
  39  
  40      /**
  41       * @return $this
  42       */
  43      public function setShowGridLines(bool $showGridLines): self
  44      {
  45          $this->showGridLines = $showGridLines;
  46  
  47          return $this;
  48      }
  49  
  50      /**
  51       * @return $this
  52       */
  53      public function setShowRowColHeaders(bool $showRowColHeaders): self
  54      {
  55          $this->showRowColHeaders = $showRowColHeaders;
  56  
  57          return $this;
  58      }
  59  
  60      /**
  61       * @return $this
  62       */
  63      public function setShowZeroes(bool $showZeroes): self
  64      {
  65          $this->showZeroes = $showZeroes;
  66  
  67          return $this;
  68      }
  69  
  70      /**
  71       * @return $this
  72       */
  73      public function setRightToLeft(bool $rightToLeft): self
  74      {
  75          $this->rightToLeft = $rightToLeft;
  76  
  77          return $this;
  78      }
  79  
  80      /**
  81       * @return $this
  82       */
  83      public function setTabSelected(bool $tabSelected): self
  84      {
  85          $this->tabSelected = $tabSelected;
  86  
  87          return $this;
  88      }
  89  
  90      /**
  91       * @return $this
  92       */
  93      public function setShowOutlineSymbols(bool $showOutlineSymbols): self
  94      {
  95          $this->showOutlineSymbols = $showOutlineSymbols;
  96  
  97          return $this;
  98      }
  99  
 100      /**
 101       * @return $this
 102       */
 103      public function setDefaultGridColor(bool $defaultGridColor): self
 104      {
 105          $this->defaultGridColor = $defaultGridColor;
 106  
 107          return $this;
 108      }
 109  
 110      /**
 111       * @return $this
 112       */
 113      public function setView(string $view): self
 114      {
 115          $this->view = $view;
 116  
 117          return $this;
 118      }
 119  
 120      /**
 121       * @return $this
 122       */
 123      public function setTopLeftCell(string $topLeftCell): self
 124      {
 125          $this->topLeftCell = $topLeftCell;
 126  
 127          return $this;
 128      }
 129  
 130      /**
 131       * @return $this
 132       */
 133      public function setColorId(int $colorId): self
 134      {
 135          $this->colorId = $colorId;
 136  
 137          return $this;
 138      }
 139  
 140      /**
 141       * @return $this
 142       */
 143      public function setZoomScale(int $zoomScale): self
 144      {
 145          $this->zoomScale = $zoomScale;
 146  
 147          return $this;
 148      }
 149  
 150      /**
 151       * @return $this
 152       */
 153      public function setZoomScaleNormal(int $zoomScaleNormal): self
 154      {
 155          $this->zoomScaleNormal = $zoomScaleNormal;
 156  
 157          return $this;
 158      }
 159  
 160      /**
 161       * @return $this
 162       */
 163      public function setZoomScalePageLayoutView(int $zoomScalePageLayoutView): self
 164      {
 165          $this->zoomScalePageLayoutView = $zoomScalePageLayoutView;
 166  
 167          return $this;
 168      }
 169  
 170      /**
 171       * @return $this
 172       */
 173      public function setWorkbookViewId(int $workbookViewId): self
 174      {
 175          $this->workbookViewId = $workbookViewId;
 176  
 177          return $this;
 178      }
 179  
 180      /**
 181       * @param positive-int $freezeRow Set to 2 to fix the first row
 182       *
 183       * @return $this
 184       */
 185      public function setFreezeRow(int $freezeRow): self
 186      {
 187          if ($freezeRow < 1) {
 188              throw new InvalidArgumentException('Freeze row must be a positive integer');
 189          }
 190  
 191          $this->freezeRow = $freezeRow;
 192  
 193          return $this;
 194      }
 195  
 196      /**
 197       * @param string $freezeColumn Set to B to fix the first column
 198       *
 199       * @return $this
 200       */
 201      public function setFreezeColumn(string $freezeColumn): self
 202      {
 203          $this->freezeColumn = strtoupper($freezeColumn);
 204  
 205          return $this;
 206      }
 207  
 208      public function getXml(): string
 209      {
 210          return '<sheetView'.$this->getSheetViewAttributes().'>'.
 211          $this->getFreezeCellPaneXml().
 212          '</sheetView>';
 213      }
 214  
 215      private function getSheetViewAttributes(): string
 216      {
 217          return $this->generateAttributes([
 218              'showFormulas' => $this->showFormulas,
 219              'showGridLines' => $this->showGridLines,
 220              'showRowColHeaders' => $this->showRowColHeaders,
 221              'showZeroes' => $this->showZeroes,
 222              'rightToLeft' => $this->rightToLeft,
 223              'tabSelected' => $this->tabSelected,
 224              'showOutlineSymbols' => $this->showOutlineSymbols,
 225              'defaultGridColor' => $this->defaultGridColor,
 226              'view' => $this->view,
 227              'topLeftCell' => $this->topLeftCell,
 228              'colorId' => $this->colorId,
 229              'zoomScale' => $this->zoomScale,
 230              'zoomScaleNormal' => $this->zoomScaleNormal,
 231              'zoomScalePageLayoutView' => $this->zoomScalePageLayoutView,
 232              'workbookViewId' => $this->workbookViewId,
 233          ]);
 234      }
 235  
 236      private function getFreezeCellPaneXml(): string
 237      {
 238          if ($this->freezeRow < 2 && 'A' === $this->freezeColumn) {
 239              return '';
 240          }
 241  
 242          $columnIndex = CellHelper::getColumnIndexFromCellIndex($this->freezeColumn.'1');
 243  
 244          return '<pane'.$this->generateAttributes([
 245              'xSplit' => $columnIndex,
 246              'ySplit' => $this->freezeRow - 1,
 247              'topLeftCell' => $this->freezeColumn.$this->freezeRow,
 248              'activePane' => 'bottomRight',
 249              'state' => 'frozen',
 250          ]).'/>';
 251      }
 252  
 253      /**
 254       * @param array<string, bool|int|string> $data with key containing the attribute name and value containing the attribute value
 255       */
 256      private function generateAttributes(array $data): string
 257      {
 258          // Create attribute for each key
 259          $attributes = array_map(static function (string $key, bool|string|int $value): string {
 260              if (\is_bool($value)) {
 261                  $value = $value ? 'true' : 'false';
 262              }
 263  
 264              return $key.'="'.$value.'"';
 265          }, array_keys($data), $data);
 266  
 267          // Append all attributes
 268          return ' '.implode(' ', $attributes);
 269      }
 270  }