@@ -367,7 +367,6 @@ public function insertNewBefore(
367367 Worksheet $ worksheet
368368 ): void {
369369 $ remove = ($ numberOfColumns < 0 || $ numberOfRows < 0 );
370- $ allCoordinates = $ worksheet ->getCoordinates ();
371370
372371 if (
373372 $ this ->cellReferenceHelper === null ||
@@ -394,12 +393,13 @@ public function insertNewBefore(
394393 }
395394
396395 // Find missing coordinates. This is important when inserting column before the last column
396+ $ cellCollection = $ worksheet ->getCellCollection ();
397397 $ missingCoordinates = array_filter (
398398 array_map (function ($ row ) use ($ highestColumn ) {
399399 return $ highestColumn . $ row ;
400400 }, 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 ;
403403 }
404404 );
405405
@@ -408,16 +408,15 @@ function ($coordinate) use ($allCoordinates) {
408408 foreach ($ missingCoordinates as $ coordinate ) {
409409 $ worksheet ->createNewCell ($ coordinate );
410410 }
411-
412- // Refresh all coordinates
413- $ allCoordinates = $ worksheet ->getCoordinates ();
414411 }
415412
416- // Loop through cells, bottom-up, and change cell coordinate
413+ $ allCoordinates = $ worksheet -> getCoordinates ();
417414 if ($ remove ) {
418415 // It's faster to reverse and pop than to use unshift, especially with large cell collections
419416 $ allCoordinates = array_reverse ($ allCoordinates );
420417 }
418+
419+ // Loop through cells, bottom-up, and change cell coordinate
421420 while ($ coordinate = array_pop ($ allCoordinates )) {
422421 $ cell = $ worksheet ->getCell ($ coordinate );
423422 $ cellIndex = Coordinate::columnIndexFromString ($ cell ->getColumn ());
@@ -927,11 +926,7 @@ private function clearColumnStrips(int $highestRow, int $beforeColumn, int $numb
927926 for ($ i = 1 ; $ i <= $ highestRow - 1 ; ++$ i ) {
928927 for ($ j = $ beforeColumn - 1 + $ numberOfColumns ; $ j <= $ beforeColumn - 2 ; ++$ j ) {
929928 $ 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 );
935930 }
936931 }
937932 }
@@ -943,15 +938,24 @@ private function clearRowStrips(string $highestColumn, int $beforeColumn, int $b
943938 for ($ i = $ beforeColumn - 1 ; $ i <= $ lastColumnIndex ; ++$ i ) {
944939 for ($ j = $ beforeRow + $ numberOfRows ; $ j <= $ beforeRow - 1 ; ++$ j ) {
945940 $ 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 );
951942 }
952943 }
953944 }
954945
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+
955959 private function adjustAutoFilter (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns ): void
956960 {
957961 $ autoFilter = $ worksheet ->getAutoFilter ();
0 commit comments