@@ -42,15 +42,16 @@ class ColumnCellIterator extends CellIterator
42
42
/**
43
43
* Create a new row iterator.
44
44
*
45
- * @param Worksheet $subject The worksheet to iterate over
45
+ * @param Worksheet $worksheet The worksheet to iterate over
46
46
* @param string $columnIndex The column that we want to iterate
47
47
* @param int $startRow The row number at which to start iterating
48
48
* @param int $endRow Optionally, the row number at which to stop iterating
49
49
*/
50
- public function __construct (Worksheet $ subject , $ columnIndex = 'A ' , $ startRow = 1 , $ endRow = null )
50
+ public function __construct (Worksheet $ worksheet , $ columnIndex = 'A ' , $ startRow = 1 , $ endRow = null )
51
51
{
52
52
// Set subject
53
- $ this ->worksheet = $ subject ;
53
+ $ this ->worksheet = $ worksheet ;
54
+ $ this ->cellCollection = $ worksheet ->getCellCollection ();
54
55
$ this ->columnIndex = Coordinate::columnIndexFromString ($ columnIndex );
55
56
$ this ->resetEnd ($ endRow );
56
57
$ this ->resetStart ($ startRow );
@@ -96,7 +97,10 @@ public function resetEnd($endRow = null)
96
97
*/
97
98
public function seek (int $ row = 1 )
98
99
{
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
+ ) {
100
104
throw new PhpSpreadsheetException ('In "IterateOnlyExistingCells" mode and Cell does not exist ' );
101
105
}
102
106
if (($ row < $ this ->startRow ) || ($ row > $ this ->endRow )) {
@@ -120,7 +124,11 @@ public function rewind(): void
120
124
*/
121
125
public function current (): ?Cell
122
126
{
123
- return $ this ->worksheet ->getCellByColumnAndRow ($ this ->columnIndex , $ this ->currentRow );
127
+ $ cellAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex ) . $ this ->currentRow ;
128
+
129
+ return $ this ->cellCollection ->has ($ cellAddress )
130
+ ? $ this ->cellCollection ->get ($ cellAddress )
131
+ : $ this ->worksheet ->createNewCell ($ cellAddress );
124
132
}
125
133
126
134
/**
@@ -136,12 +144,13 @@ public function key(): int
136
144
*/
137
145
public function next (): void
138
146
{
147
+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
139
148
do {
140
149
++$ this ->currentRow ;
141
150
} while (
142
151
($ this ->onlyExistingCells ) &&
143
- (! $ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->currentRow ) ) &&
144
- ($ this ->currentRow <= $ this ->endRow )
152
+ ($ this ->currentRow <= $ this ->endRow ) &&
153
+ (! $ this ->cellCollection -> has ( $ columnAddress . $ this ->currentRow ) )
145
154
);
146
155
}
147
156
@@ -150,12 +159,13 @@ public function next(): void
150
159
*/
151
160
public function prev (): void
152
161
{
162
+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
153
163
do {
154
164
--$ this ->currentRow ;
155
165
} while (
156
166
($ this ->onlyExistingCells ) &&
157
- (! $ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->currentRow ) ) &&
158
- ($ this ->currentRow >= $ this ->startRow )
167
+ ($ this ->currentRow >= $ this ->startRow ) &&
168
+ (! $ this ->cellCollection -> has ( $ columnAddress . $ this ->currentRow ) )
159
169
);
160
170
}
161
171
@@ -173,14 +183,15 @@ public function valid(): bool
173
183
protected function adjustForExistingOnlyRange (): void
174
184
{
175
185
if ($ this ->onlyExistingCells ) {
186
+ $ columnAddress = Coordinate::stringFromColumnIndex ($ this ->columnIndex );
176
187
while (
177
- (!$ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->startRow )) &&
188
+ (!$ this ->cellCollection -> has ( $ columnAddress . $ this ->startRow )) &&
178
189
($ this ->startRow <= $ this ->endRow )
179
190
) {
180
191
++$ this ->startRow ;
181
192
}
182
193
while (
183
- (!$ this ->worksheet -> cellExistsByColumnAndRow ( $ this -> columnIndex , $ this ->endRow )) &&
194
+ (!$ this ->cellCollection -> has ( $ columnAddress . $ this ->endRow )) &&
184
195
($ this ->endRow >= $ this ->startRow )
185
196
) {
186
197
--$ this ->endRow ;
0 commit comments