See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 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 * @throws PhpSpreadsheetException 61 * 62 * @return RowCellIterator 63 */ 64 public function resetStart($startColumn = 'A') 65 { 66 $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn); 67 $this->adjustForExistingOnlyRange(); 68 $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex)); 69 70 return $this; 71 } 72 73 /** 74 * (Re)Set the end column. 75 * 76 * @param string $endColumn The column address at which to stop iterating 77 * 78 * @throws PhpSpreadsheetException 79 * 80 * @return RowCellIterator 81 */ 82 public function resetEnd($endColumn = null) 83 { 84 $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); 85 $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); 86 $this->adjustForExistingOnlyRange(); 87 88 return $this; 89 } 90 91 /** 92 * Set the column pointer to the selected column. 93 * 94 * @param string $column The column address to set the current pointer at 95 * 96 * @throws PhpSpreadsheetException 97 * 98 * @return RowCellIterator 99 */ 100 public function seek($column = 'A') 101 { 102 $column = Coordinate::columnIndexFromString($column); 103 if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) { 104 throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})"); 105 } elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($column, $this->rowIndex))) { 106 throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist'); 107 } 108 $this->currentColumnIndex = $column; 109 110 return $this; 111 } 112 113 /** 114 * Rewind the iterator to the starting column. 115 */ 116 public function rewind() 117 { 118 $this->currentColumnIndex = $this->startColumnIndex; 119 } 120 121 /** 122 * Return the current cell in this worksheet row. 123 * 124 * @return \PhpOffice\PhpSpreadsheet\Cell\Cell 125 */ 126 public function current() 127 { 128 return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex); 129 } 130 131 /** 132 * Return the current iterator key. 133 * 134 * @return string 135 */ 136 public function key() 137 { 138 return Coordinate::stringFromColumnIndex($this->currentColumnIndex); 139 } 140 141 /** 142 * Set the iterator to its next value. 143 */ 144 public function next() 145 { 146 do { 147 ++$this->currentColumnIndex; 148 } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex)); 149 } 150 151 /** 152 * Set the iterator to its previous value. 153 * 154 * @throws PhpSpreadsheetException 155 */ 156 public function prev() 157 { 158 do { 159 --$this->currentColumnIndex; 160 } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex)); 161 } 162 163 /** 164 * Indicate if more columns exist in the worksheet range of columns that we're iterating. 165 * 166 * @return bool 167 */ 168 public function valid() 169 { 170 return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex; 171 } 172 173 /** 174 * Return the current iterator position. 175 * 176 * @return int 177 */ 178 public function getCurrentColumnIndex() 179 { 180 return $this->currentColumnIndex; 181 } 182 183 /** 184 * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary. 185 * 186 * @throws PhpSpreadsheetException 187 */ 188 protected function adjustForExistingOnlyRange() 189 { 190 if ($this->onlyExistingCells) { 191 while ((!$this->worksheet->cellExistsByColumnAndRow($this->startColumnIndex, $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) { 192 ++$this->startColumnIndex; 193 } 194 if ($this->startColumnIndex > $this->endColumnIndex) { 195 throw new PhpSpreadsheetException('No cells exist within the specified range'); 196 } 197 while ((!$this->worksheet->cellExistsByColumnAndRow($this->endColumnIndex, $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) { 198 --$this->endColumnIndex; 199 } 200 if ($this->endColumnIndex < $this->startColumnIndex) { 201 throw new PhpSpreadsheetException('No cells exist within the specified range'); 202 } 203 } 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body