@@ -42,15 +42,16 @@ class ColumnCellIterator extends CellIterator
4242 /**
4343 * Create a new row iterator.
4444 *
45- * @param Worksheet $subject The worksheet to iterate over
45+ * @param Worksheet $worksheet The worksheet to iterate over
4646 * @param string $columnIndex The column that we want to iterate
4747 * @param int $startRow The row number at which to start iterating
4848 * @param int $endRow Optionally, the row number at which to stop iterating
4949 */
50- public function __construct (Worksheet $ subject , $ columnIndex = 'A ' , $ startRow = 1 , $ endRow = null )
50+ public function __construct (Worksheet $ worksheet , $ columnIndex = 'A ' , $ startRow = 1 , $ endRow = null )
5151 {
5252 // Set subject
53- $ this ->worksheet = $ subject ;
53+ $ this ->worksheet = $ worksheet ;
54+ $ this ->cellCollection = $ worksheet ->getCellCollection ();
5455 $ this ->columnIndex = Coordinate::columnIndexFromString ($ columnIndex );
5556 $ this ->resetEnd ($ endRow );
5657 $ this ->resetStart ($ startRow );
@@ -96,7 +97,10 @@ public function resetEnd($endRow = null)
9697 */
9798 public function seek (int $ row = 1 )
9899 {
99- if ($ this ->onlyExistingCells && !($ this ->worksheet ->cellExistsByColumnAndRow ($ this ->columnIndex , $ row ))) {
100+ if (
101+ $ this ->onlyExistingCells &&
102+ (!$ this ->cellCollection ->has (Coordinate::stringFromColumnIndex ($ this ->columnIndex ) . $ row ))
103+ ) {
100104 throw new PhpSpreadsheetException ('In "IterateOnlyExistingCells" mode and Cell does not exist ' );
101105 }
102106 if (($ row < $ this ->startRow ) || ($ row > $ this ->endRow )) {
@@ -122,8 +126,8 @@ public function current(): ?Cell
122126 {
123127 $ cellAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex ) . $ this ->currentRow ;
124128
125- return $ this ->worksheet -> getCellCollection () ->has ($ cellAddress )
126- ? $ this ->worksheet -> getCellCollection () ->get ($ cellAddress )
129+ return $ this ->cellCollection ->has ($ cellAddress )
130+ ? $ this ->cellCollection ->get ($ cellAddress )
127131 : $ this ->worksheet ->createNewCell ($ cellAddress );
128132 }
129133
@@ -140,12 +144,13 @@ public function key(): int
140144 */
141145 public function next (): void
142146 {
147+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
143148 do {
144149 ++$ this ->currentRow ;
145150 } while (
146151 ($ this ->onlyExistingCells ) &&
147- (! $ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->currentRow ) ) &&
148- ($ this ->currentRow <= $ this ->endRow )
152+ ($ this ->currentRow <= $ this ->endRow ) &&
153+ (! $ this ->cellCollection -> has ( $ columnAddress . $ this ->currentRow ) )
149154 );
150155 }
151156
@@ -154,12 +159,13 @@ public function next(): void
154159 */
155160 public function prev (): void
156161 {
162+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
157163 do {
158164 --$ this ->currentRow ;
159165 } while (
160166 ($ this ->onlyExistingCells ) &&
161- (! $ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->currentRow ) ) &&
162- ($ this ->currentRow >= $ this ->startRow )
167+ ($ this ->currentRow >= $ this ->startRow ) &&
168+ (! $ this ->cellCollection -> has ( $ columnAddress . $ this ->currentRow ) )
163169 );
164170 }
165171
@@ -177,14 +183,15 @@ public function valid(): bool
177183 protected function adjustForExistingOnlyRange (): void
178184 {
179185 if ($ this ->onlyExistingCells ) {
186+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
180187 while (
181- (!$ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->startRow )) &&
188+ (!$ this ->cellCollection -> has ( $ columnAddress . $ this ->startRow )) &&
182189 ($ this ->startRow <= $ this ->endRow )
183190 ) {
184191 ++$ this ->startRow ;
185192 }
186193 while (
187- (!$ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->endRow )) &&
194+ (!$ this ->cellCollection -> has ( $ columnAddress . $ this ->endRow )) &&
188195 ($ this ->endRow >= $ this ->startRow )
189196 ) {
190197 --$ this ->endRow ;
0 commit comments