@@ -194,8 +194,10 @@ protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns,
194194 if ($ cellReferenceHelper ->cellAddressInDeleteRange ($ cellAddress ) === true ) {
195195 $ worksheet ->setHyperlink ($ cellAddress , null );
196196 } elseif ($ cellAddress !== $ newReference ) {
197- $ worksheet ->setHyperlink ($ newReference , $ value );
198197 $ worksheet ->setHyperlink ($ cellAddress , null );
198+ if ($ newReference ) {
199+ $ worksheet ->setHyperlink ($ newReference , $ value );
200+ }
199201 }
200202 }
201203 }
@@ -292,6 +294,9 @@ protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColu
292294 if ($ cellAddress !== $ newReference ) {
293295 $ worksheet ->setDataValidation ($ newReference , $ dataValidation );
294296 $ worksheet ->setDataValidation ($ cellAddress , null );
297+ if ($ newReference ) {
298+ $ worksheet ->setDataValidation ($ newReference , $ dataValidation );
299+ }
295300 }
296301 }
297302 }
@@ -307,7 +312,9 @@ protected function adjustMergeCells(Worksheet $worksheet): void
307312 $ aNewMergeCells = []; // the new array of all merge cells
308313 foreach ($ aMergeCells as $ cellAddress => &$ value ) {
309314 $ newReference = $ this ->updateCellReference ($ cellAddress );
310- $ aNewMergeCells [$ newReference ] = $ newReference ;
315+ if ($ newReference ) {
316+ $ aNewMergeCells [$ newReference ] = $ newReference ;
317+ }
311318 }
312319 $ worksheet ->setMergeCells ($ aNewMergeCells ); // replace the merge cells array
313320 }
@@ -328,8 +335,10 @@ protected function adjustProtectedCells(Worksheet $worksheet, int $numberOfColum
328335 foreach ($ aProtectedCells as $ cellAddress => $ protectedRange ) {
329336 $ newReference = $ this ->updateCellReference ($ cellAddress );
330337 if ($ cellAddress !== $ newReference ) {
331- $ worksheet ->protectCells ($ newReference , $ protectedRange ->getPassword (), true );
332338 $ worksheet ->unprotectCells ($ cellAddress );
339+ if ($ newReference ) {
340+ $ worksheet ->protectCells ($ newReference , $ protectedRange ->getPassword (), true );
341+ }
333342 }
334343 }
335344 }
@@ -457,7 +466,8 @@ public function insertNewBefore(
457466 $ cell = $ worksheet ->getCell ($ coordinate );
458467 $ cellIndex = Coordinate::columnIndexFromString ($ cell ->getColumn ());
459468
460- if ($ cellIndex - 1 + $ numberOfColumns < 0 ) {
469+ // Don't update cells that are being removed
470+ if ($ numberOfColumns < 0 && $ cellIndex >= $ beforeColumn + $ numberOfColumns && $ cellIndex < $ beforeColumn ) {
461471 continue ;
462472 }
463473
@@ -633,8 +643,8 @@ public function updateFormulaReferences(
633643 if ($ matchCount > 0 ) {
634644 foreach ($ matches as $ match ) {
635645 $ fromString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "{$ match [3 ]}: {$ match [4 ]}" );
636- $ modified3 = substr ($ this ->updateCellReference ('$A ' . $ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences ), 2 );
637- $ modified4 = substr ($ this ->updateCellReference ('$A ' . $ match [4 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences ), 2 );
646+ $ modified3 = substr ($ this ->updateCellReference ('$A ' . $ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, true ), 2 );
647+ $ modified4 = substr ($ this ->updateCellReference ('$A ' . $ match [4 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, false ), 2 );
638648
639649 if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
640650 if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -657,8 +667,8 @@ public function updateFormulaReferences(
657667 if ($ matchCount > 0 ) {
658668 foreach ($ matches as $ match ) {
659669 $ fromString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "{$ match [3 ]}: {$ match [4 ]}" );
660- $ modified3 = substr ($ this ->updateCellReference ($ match [3 ] . '$1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences ), 0 , -2 );
661- $ modified4 = substr ($ this ->updateCellReference ($ match [4 ] . '$1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences ), 0 , -2 );
670+ $ modified3 = substr ($ this ->updateCellReference ($ match [3 ] . '$1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, true ), 0 , -2 );
671+ $ modified4 = substr ($ this ->updateCellReference ($ match [4 ] . '$1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, false ), 0 , -2 );
662672
663673 if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
664674 if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -681,8 +691,8 @@ public function updateFormulaReferences(
681691 if ($ matchCount > 0 ) {
682692 foreach ($ matches as $ match ) {
683693 $ fromString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "{$ match [3 ]}: {$ match [4 ]}" );
684- $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
685- $ modified4 = $ this ->updateCellReference ($ match [4 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
694+ $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, true );
695+ $ modified4 = $ this ->updateCellReference ($ match [4 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, false );
686696
687697 if ($ match [3 ] . $ match [4 ] !== $ modified3 . $ modified4 ) {
688698 if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -709,7 +719,7 @@ public function updateFormulaReferences(
709719 foreach ($ matches as $ match ) {
710720 $ fromString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "{$ match [3 ]}" );
711721
712- $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
722+ $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null );
713723 if ($ match [3 ] !== $ modified3 ) {
714724 if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
715725 $ toString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "$ modified3 " );
@@ -890,7 +900,7 @@ private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows
890900 *
891901 * @return string Updated cell range
892902 */
893- private function updateCellReference (string $ cellReference = 'A1 ' , bool $ includeAbsoluteReferences = false , bool $ onlyAbsoluteReferences = false ): string
903+ private function updateCellReference (string $ cellReference = 'A1 ' , bool $ includeAbsoluteReferences = false , bool $ onlyAbsoluteReferences = false , ? bool $ topLeft = null )
894904 {
895905 // Is it in another worksheet? Will not have to update anything.
896906 if (str_contains ($ cellReference , '! ' )) {
@@ -902,7 +912,7 @@ private function updateCellReference(string $cellReference = 'A1', bool $include
902912 /** @var CellReferenceHelper */
903913 $ cellReferenceHelper = $ this ->cellReferenceHelper ;
904914
905- return $ cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
915+ return $ cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, $ topLeft );
906916 }
907917
908918 // Range
@@ -1008,14 +1018,14 @@ private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsol
10081018 $ cellReferenceHelper = $ this ->cellReferenceHelper ;
10091019 if (ctype_alpha ($ range [$ i ][$ j ])) {
10101020 $ range [$ i ][$ j ] = Coordinate::coordinateFromString (
1011- $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences )
1021+ $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null )
10121022 )[0 ];
10131023 } elseif (ctype_digit ($ range [$ i ][$ j ])) {
10141024 $ range [$ i ][$ j ] = Coordinate::coordinateFromString (
1015- $ cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences )
1025+ $ cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null )
10161026 )[1 ];
10171027 } else {
1018- $ range [$ i ][$ j ] = $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
1028+ $ range [$ i ][$ j ] = $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null );
10191029 }
10201030 }
10211031 }
0 commit comments