@@ -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 ());
@@ -924,34 +923,43 @@ private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsol
924
923
925
924
private function clearColumnStrips (int $ highestRow , int $ beforeColumn , int $ numberOfColumns , Worksheet $ worksheet ): void
926
925
{
927
- for ($ i = 1 ; $ i <= $ highestRow - 1 ; ++$ i ) {
928
- for ($ j = $ beforeColumn - 1 + $ numberOfColumns ; $ j <= $ beforeColumn - 2 ; ++$ j ) {
929
- $ 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
- }
926
+ $ startColumnId = Coordinate::stringFromColumnIndex ($ beforeColumn + $ numberOfColumns );
927
+ $ endColumnId = Coordinate::stringFromColumnIndex ($ beforeColumn );
928
+
929
+ for ($ row = 1 ; $ row <= $ highestRow - 1 ; ++$ row ) {
930
+ for ($ column = $ startColumnId ; $ column !== $ endColumnId ; ++$ column ) {
931
+ $ coordinate = $ column . $ row ;
932
+ $ this ->clearStripCell ($ worksheet , $ coordinate );
935
933
}
936
934
}
937
935
}
938
936
939
937
private function clearRowStrips (string $ highestColumn , int $ beforeColumn , int $ beforeRow , int $ numberOfRows , Worksheet $ worksheet ): void
940
938
{
941
- $ lastColumnIndex = Coordinate::columnIndexFromString ($ highestColumn ) - 1 ;
942
-
943
- for ($ i = $ beforeColumn - 1 ; $ i <= $ lastColumnIndex ; ++$ i ) {
944
- for ($ j = $ beforeRow + $ numberOfRows ; $ j <= $ beforeRow - 1 ; ++$ j ) {
945
- $ 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
- }
939
+ $ startColumnId = Coordinate::stringFromColumnIndex ($ beforeColumn );
940
+ ++$ highestColumn ;
941
+
942
+ for ($ column = $ startColumnId ; $ column !== $ highestColumn ; ++$ column ) {
943
+ for ($ row = $ beforeRow + $ numberOfRows ; $ row <= $ beforeRow - 1 ; ++$ row ) {
944
+ $ coordinate = $ column . $ row ;
945
+ $ this ->clearStripCell ($ worksheet , $ coordinate );
951
946
}
952
947
}
953
948
}
954
949
950
+ private function clearStripCell (Worksheet $ worksheet , string $ coordinate ): void
951
+ {
952
+ $ worksheet ->removeConditionalStyles ($ coordinate );
953
+ $ worksheet ->setHyperlink ($ coordinate );
954
+ $ worksheet ->setDataValidation ($ coordinate );
955
+ $ worksheet ->removeComment ($ coordinate );
956
+
957
+ if ($ worksheet ->cellExists ($ coordinate )) {
958
+ $ worksheet ->getCell ($ coordinate )->setValueExplicit (null , DataType::TYPE_NULL );
959
+ $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
960
+ }
961
+ }
962
+
955
963
private function adjustAutoFilter (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns ): void
956
964
{
957
965
$ autoFilter = $ worksheet ->getAutoFilter ();
0 commit comments