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\Worksheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   7  
   8  class RowCellIterator extends CellIterator
   9  {
  10      /**
  11       * Current iterator position.
  12       *
  13       * @var int
  14       */
  15      private $currentColumnIndex;
  16  
  17      /**
  18       * Row index.
  19       *
  20       * @var int
  21       */
  22      private $rowIndex = 1;
  23  
  24      /**
  25       * Start position.
  26       *
  27       * @var int
  28       */
  29      private $startColumnIndex = 1;
  30  
  31      /**
  32       * End position.
  33       *
  34       * @var int
  35       */
  36      private $endColumnIndex = 1;
  37  
  38      /**
  39       * Create a new column iterator.
  40       *
  41       * @param Worksheet $worksheet The worksheet to iterate over
  42       * @param int $rowIndex The row that we want to iterate
  43       * @param string $startColumn The column address at which to start iterating
  44       * @param string $endColumn Optionally, the column address at which to stop iterating
  45       */
  46      public function __construct(?Worksheet $worksheet = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
  47      {
  48          // Set subject and row index
  49          $this->worksheet = $worksheet;
  50          $this->rowIndex = $rowIndex;
  51          $this->resetEnd($endColumn);
  52          $this->resetStart($startColumn);
  53      }
  54  
  55      /**
  56       * (Re)Set the start column and the current column pointer.
  57       *
  58       * @param string $startColumn The column address at which to start iterating
  59       *
  60       * @return $this
  61       */
  62      public function resetStart($startColumn = 'A')
  63      {
  64          $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn);
  65          $this->adjustForExistingOnlyRange();
  66          $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex));
  67  
  68          return $this;
  69      }
  70  
  71      /**
  72       * (Re)Set the end column.
  73       *
  74       * @param string $endColumn The column address at which to stop iterating
  75       *
  76       * @return $this
  77       */
  78      public function resetEnd($endColumn = null)
  79      {
  80          $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn();
  81          $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
  82          $this->adjustForExistingOnlyRange();
  83  
  84          return $this;
  85      }
  86  
  87      /**
  88       * Set the column pointer to the selected column.
  89       *
  90       * @param string $column The column address to set the current pointer at
  91       *
  92       * @return $this
  93       */
  94      public function seek($column = 'A')
  95      {
  96          $columnx = $column;
  97          $column = Coordinate::columnIndexFromString($column);
  98          if ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($column, $this->rowIndex))) {
  99              throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
 100          }
 101          if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {
 102              throw new PhpSpreadsheetException("Column $columnx is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})");
 103          }
 104          $this->currentColumnIndex = $column;
 105  
 106          return $this;
 107      }
 108  
 109      /**
 110       * Rewind the iterator to the starting column.
 111       */
 112      public function rewind(): void
 113      {
 114          $this->currentColumnIndex = $this->startColumnIndex;
 115      }
 116  
 117      /**
 118       * Return the current cell in this worksheet row.
 119       *
 120       * @return \PhpOffice\PhpSpreadsheet\Cell\Cell
 121       */
 122      public function current()
 123      {
 124          return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex);
 125      }
 126  
 127      /**
 128       * Return the current iterator key.
 129       *
 130       * @return string
 131       */
 132      public function key()
 133      {
 134          return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
 135      }
 136  
 137      /**
 138       * Set the iterator to its next value.
 139       */
 140      public function next(): void
 141      {
 142          do {
 143              ++$this->currentColumnIndex;
 144          } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex));
 145      }
 146  
 147      /**
 148       * Set the iterator to its previous value.
 149       */
 150      public function prev(): void
 151      {
 152          do {
 153              --$this->currentColumnIndex;
 154          } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex));
 155      }
 156  
 157      /**
 158       * Indicate if more columns exist in the worksheet range of columns that we're iterating.
 159       *
 160       * @return bool
 161       */
 162      public function valid()
 163      {
 164          return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex;
 165      }
 166  
 167      /**
 168       * Return the current iterator position.
 169       *
 170       * @return int
 171       */
 172      public function getCurrentColumnIndex()
 173      {
 174          return $this->currentColumnIndex;
 175      }
 176  
 177      /**
 178       * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
 179       */
 180      protected function adjustForExistingOnlyRange(): void
 181      {
 182          if ($this->onlyExistingCells) {
 183              while ((!$this->worksheet->cellExistsByColumnAndRow($this->startColumnIndex, $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) {
 184                  ++$this->startColumnIndex;
 185              }
 186              while ((!$this->worksheet->cellExistsByColumnAndRow($this->endColumnIndex, $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) {
 187                  --$this->endColumnIndex;
 188              }
 189          }
 190      }
 191  }