@@ -367,7 +367,6 @@ public function insertNewBefore(
367
367
Worksheet $ worksheet
368
368
): void {
369
369
$ remove = ($ numberOfColumns < 0 || $ numberOfRows < 0 );
370
- $ allCoordinates = $ worksheet ->getCoordinates ();
371
370
372
371
if (
373
372
$ this ->cellReferenceHelper === null ||
@@ -394,12 +393,13 @@ public function insertNewBefore(
394
393
}
395
394
396
395
// Find missing coordinates. This is important when inserting column before the last column
396
+ $ cellCollection = $ worksheet ->getCellCollection ();
397
397
$ missingCoordinates = array_filter (
398
398
array_map (function ($ row ) use ($ highestColumn ) {
399
399
return $ highestColumn . $ row ;
400
400
}, range (1 , $ highestRow )),
401
- function ($ coordinate ) use ($ allCoordinates ) {
402
- return in_array ($ coordinate, $ allCoordinates , true ) === false ;
401
+ function ($ coordinate ) use ($ cellCollection ) {
402
+ return $ cellCollection -> has ($ coordinate ) === false ;
403
403
}
404
404
);
405
405
@@ -408,16 +408,15 @@ function ($coordinate) use ($allCoordinates) {
408
408
foreach ($ missingCoordinates as $ coordinate ) {
409
409
$ worksheet ->createNewCell ($ coordinate );
410
410
}
411
-
412
- // Refresh all coordinates
413
- $ allCoordinates = $ worksheet ->getCoordinates ();
414
411
}
415
412
416
- // Loop through cells, bottom-up, and change cell coordinate
413
+ $ allCoordinates = $ worksheet -> getCoordinates ();
417
414
if ($ remove ) {
418
415
// It's faster to reverse and pop than to use unshift, especially with large cell collections
419
416
$ allCoordinates = array_reverse ($ allCoordinates );
420
417
}
418
+
419
+ // Loop through cells, bottom-up, and change cell coordinate
421
420
while ($ coordinate = array_pop ($ allCoordinates )) {
422
421
$ cell = $ worksheet ->getCell ($ coordinate );
423
422
$ cellIndex = Coordinate::columnIndexFromString ($ cell ->getColumn ());
@@ -927,11 +926,7 @@ private function clearColumnStrips(int $highestRow, int $beforeColumn, int $numb
927
926
for ($ i = 1 ; $ i <= $ highestRow - 1 ; ++$ i ) {
928
927
for ($ j = $ beforeColumn - 1 + $ numberOfColumns ; $ j <= $ beforeColumn - 2 ; ++$ j ) {
929
928
$ coordinate = Coordinate::stringFromColumnIndex ($ j + 1 ) . $ i ;
930
- $ worksheet ->removeConditionalStyles ($ coordinate );
931
- if ($ worksheet ->cellExists ($ coordinate )) {
932
- $ worksheet ->getCell ($ coordinate )->setValueExplicit (null , DataType::TYPE_NULL );
933
- $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
934
- }
929
+ $ this ->clearStripCell ($ worksheet , $ coordinate );
935
930
}
936
931
}
937
932
}
@@ -943,15 +938,24 @@ private function clearRowStrips(string $highestColumn, int $beforeColumn, int $b
943
938
for ($ i = $ beforeColumn - 1 ; $ i <= $ lastColumnIndex ; ++$ i ) {
944
939
for ($ j = $ beforeRow + $ numberOfRows ; $ j <= $ beforeRow - 1 ; ++$ j ) {
945
940
$ coordinate = Coordinate::stringFromColumnIndex ($ i + 1 ) . $ j ;
946
- $ worksheet ->removeConditionalStyles ($ coordinate );
947
- if ($ worksheet ->cellExists ($ coordinate )) {
948
- $ worksheet ->getCell ($ coordinate )->setValueExplicit (null , DataType::TYPE_NULL );
949
- $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
950
- }
941
+ $ this ->clearStripCell ($ worksheet , $ coordinate );
951
942
}
952
943
}
953
944
}
954
945
946
+ private function clearStripCell (Worksheet $ worksheet , string $ coordinate )
947
+ {
948
+ // TODO - Should also clear down comments, but wait until after comment removal PR-2875 is merged
949
+ $ worksheet ->removeConditionalStyles ($ coordinate );
950
+ $ worksheet ->setHyperlink ($ coordinate );
951
+ $ worksheet ->setDataValidation ($ coordinate );
952
+
953
+ if ($ worksheet ->cellExists ($ coordinate )) {
954
+ $ worksheet ->getCell ($ coordinate )->setValueExplicit (null , DataType::TYPE_NULL );
955
+ $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
956
+ }
957
+ }
958
+
955
959
private function adjustAutoFilter (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns ): void
956
960
{
957
961
$ autoFilter = $ worksheet ->getAutoFilter ();
0 commit comments