44
55use Generator ;
66use PhpOffice \PhpSpreadsheet \Cell \Cell ;
7+ use PhpOffice \PhpSpreadsheet \Cell \Coordinate ;
78use PhpOffice \PhpSpreadsheet \Exception as PhpSpreadsheetException ;
89use PhpOffice \PhpSpreadsheet \Settings ;
910use PhpOffice \PhpSpreadsheet \Worksheet \Worksheet ;
@@ -150,41 +151,16 @@ public function getCoordinates()
150151 public function getSortedCoordinates ()
151152 {
152153 $ sortKeys = [];
153- foreach ($ this ->getCoordinates () as $ coord ) {
154- sscanf ($ coord , '%[A-Z]%d ' , $ column , $ row );
155- $ sortKeys [sprintf ('%09d%3s ' , $ row , $ column )] = $ coord ;
154+ foreach ($ this ->getCoordinates () as $ coordinate ) {
155+ sscanf ($ coordinate , '%[A-Z]%d ' , $ column , $ row );
156+ $ key = (--$ row * 16384 ) + Coordinate::columnIndexFromString ($ column );
157+ $ sortKeys [$ key ] = $ coordinate ;
156158 }
157159 ksort ($ sortKeys );
158160
159161 return array_values ($ sortKeys );
160162 }
161163
162- /**
163- * Get highest worksheet column and highest row that have cell records.
164- *
165- * @return array Highest column name and highest row number
166- */
167- public function getHighestRowAndColumn ()
168- {
169- // Lookup highest column and highest row
170- $ col = ['A ' => '1A ' ];
171- $ row = [1 ];
172- foreach ($ this ->getCoordinates () as $ coord ) {
173- sscanf ($ coord , '%[A-Z]%d ' , $ c , $ r );
174- $ row [$ r ] = $ r ;
175- $ col [$ c ] = strlen ($ c ) . $ c ;
176- }
177-
178- // Determine highest column and row
179- $ highestRow = max ($ row );
180- $ highestColumn = substr ((string ) @max ($ col ), 1 );
181-
182- return [
183- 'row ' => $ highestRow ,
184- 'column ' => $ highestColumn ,
185- ];
186- }
187-
188164 /**
189165 * Return the cell coordinate of the currently active cell object.
190166 *
@@ -219,6 +195,32 @@ public function getCurrentRow()
219195 return (int ) $ row ;
220196 }
221197
198+ /**
199+ * Get highest worksheet column and highest row that have cell records.
200+ *
201+ * @return array Highest column name and highest row number
202+ */
203+ public function getHighestRowAndColumn ()
204+ {
205+ // Lookup highest column and highest row
206+ $ columns = ['1A ' ];
207+ $ rows = [1 ];
208+ foreach ($ this ->getCoordinates () as $ coordinate ) {
209+ sscanf ($ coordinate , '%[A-Z]%d ' , $ column , $ row );
210+ $ rows [$ row ] = $ rows [$ row ] ?? $ row ;
211+ $ columns [$ column ] = $ columns [$ column ] ?? strlen ($ column ) . $ column ;
212+ }
213+
214+ // Determine highest column and row
215+ $ highestRow = max ($ rows );
216+ $ highestColumn = substr ((string ) max ($ columns ), 1 );
217+
218+ return [
219+ 'row ' => $ highestRow ,
220+ 'column ' => $ highestColumn ,
221+ ];
222+ }
223+
222224 /**
223225 * Get highest worksheet column.
224226 *
@@ -239,7 +241,8 @@ public function getHighestColumn($row = null)
239241 if ($ r != $ row ) {
240242 continue ;
241243 }
242- $ maxColumn = max ($ maxColumn , strlen ($ c ) . $ c );
244+ $ sortableColum = strlen ($ c ) . $ c ;
245+ $ maxColumn = $ maxColumn > $ sortableColum ? $ maxColumn : $ sortableColum ;
243246 }
244247
245248 return substr ($ maxColumn , 1 );
@@ -265,7 +268,7 @@ public function getHighestRow($column = null)
265268 if ($ c != $ column ) {
266269 continue ;
267270 }
268- $ maxRow = max ($ maxRow, $ r );
271+ $ maxRow = ($ maxRow > $ r ) ? $ maxRow : $ r ;
269272 }
270273
271274 return $ maxRow ;
@@ -314,7 +317,9 @@ public function cloneCellCollection(Worksheet $worksheet)
314317
315318 // Store new values
316319 $ stored = $ newCollection ->cache ->setMultiple ($ newValues );
317- $ this ->destructIfNeeded ($ stored , $ newCollection , 'Failed to copy cells in cache ' );
320+ if ($ stored === false ) {
321+ $ this ->destructIfNeeded ($ newCollection , 'Failed to copy cells in cache ' );
322+ }
318323
319324 return $ newCollection ;
320325 }
@@ -359,21 +364,21 @@ private function storeCurrentCell(): void
359364 $ this ->currentCell ->detach ();
360365
361366 $ stored = $ this ->cache ->set ($ this ->cachePrefix . $ this ->currentCoordinate , $ this ->currentCell );
362- $ this ->destructIfNeeded ($ stored , $ this , "Failed to store cell {$ this ->currentCoordinate } in cache " );
367+ if ($ stored === false ) {
368+ $ this ->destructIfNeeded ($ this , "Failed to store cell {$ this ->currentCoordinate } in cache " );
369+ }
363370 $ this ->currentCellIsDirty = false ;
364371 }
365372
366373 $ this ->currentCoordinate = null ;
367374 $ this ->currentCell = null ;
368375 }
369376
370- private function destructIfNeeded (bool $ stored , self $ cells , string $ message ): void
377+ private function destructIfNeeded (self $ cells , string $ message ): void
371378 {
372- if (!$ stored ) {
373- $ cells ->__destruct ();
379+ $ cells ->__destruct ();
374380
375- throw new PhpSpreadsheetException ($ message );
376- }
381+ throw new PhpSpreadsheetException ($ message );
377382 }
378383
379384 /**
@@ -413,7 +418,7 @@ public function get($cellCoordinate)
413418 $ this ->storeCurrentCell ();
414419
415420 // Return null if requested entry doesn't exist in collection
416- if (! $ this ->has ($ cellCoordinate )) {
421+ if ($ this ->has ($ cellCoordinate ) === false ) {
417422 return null ;
418423 }
419424
0 commit comments