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