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