@@ -194,8 +194,10 @@ protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns,
194
194
if ($ cellReferenceHelper ->cellAddressInDeleteRange ($ cellAddress ) === true ) {
195
195
$ worksheet ->setHyperlink ($ cellAddress , null );
196
196
} elseif ($ cellAddress !== $ newReference ) {
197
- $ worksheet ->setHyperlink ($ newReference , $ value );
198
197
$ worksheet ->setHyperlink ($ cellAddress , null );
198
+ if ($ newReference ) {
199
+ $ worksheet ->setHyperlink ($ newReference , $ value );
200
+ }
199
201
}
200
202
}
201
203
}
@@ -292,6 +294,9 @@ protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColu
292
294
if ($ cellAddress !== $ newReference ) {
293
295
$ worksheet ->setDataValidation ($ newReference , $ dataValidation );
294
296
$ worksheet ->setDataValidation ($ cellAddress , null );
297
+ if ($ newReference ) {
298
+ $ worksheet ->setDataValidation ($ newReference , $ dataValidation );
299
+ }
295
300
}
296
301
}
297
302
}
@@ -307,7 +312,9 @@ protected function adjustMergeCells(Worksheet $worksheet): void
307
312
$ aNewMergeCells = []; // the new array of all merge cells
308
313
foreach ($ aMergeCells as $ cellAddress => &$ value ) {
309
314
$ newReference = $ this ->updateCellReference ($ cellAddress );
310
- $ aNewMergeCells [$ newReference ] = $ newReference ;
315
+ if ($ newReference ) {
316
+ $ aNewMergeCells [$ newReference ] = $ newReference ;
317
+ }
311
318
}
312
319
$ worksheet ->setMergeCells ($ aNewMergeCells ); // replace the merge cells array
313
320
}
@@ -328,8 +335,10 @@ protected function adjustProtectedCells(Worksheet $worksheet, int $numberOfColum
328
335
foreach ($ aProtectedCells as $ cellAddress => $ protectedRange ) {
329
336
$ newReference = $ this ->updateCellReference ($ cellAddress );
330
337
if ($ cellAddress !== $ newReference ) {
331
- $ worksheet ->protectCells ($ newReference , $ protectedRange ->getPassword (), true );
332
338
$ worksheet ->unprotectCells ($ cellAddress );
339
+ if ($ newReference ) {
340
+ $ worksheet ->protectCells ($ newReference , $ protectedRange ->getPassword (), true );
341
+ }
333
342
}
334
343
}
335
344
}
@@ -457,7 +466,8 @@ public function insertNewBefore(
457
466
$ cell = $ worksheet ->getCell ($ coordinate );
458
467
$ cellIndex = Coordinate::columnIndexFromString ($ cell ->getColumn ());
459
468
460
- if ($ cellIndex - 1 + $ numberOfColumns < 0 ) {
469
+ // Don't update cells that are being removed
470
+ if ($ numberOfColumns < 0 && $ cellIndex >= $ beforeColumn + $ numberOfColumns && $ cellIndex < $ beforeColumn ) {
461
471
continue ;
462
472
}
463
473
@@ -633,8 +643,8 @@ public function updateFormulaReferences(
633
643
if ($ matchCount > 0 ) {
634
644
foreach ($ matches as $ match ) {
635
645
$ 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 );
638
648
639
649
if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
640
650
if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -657,8 +667,8 @@ public function updateFormulaReferences(
657
667
if ($ matchCount > 0 ) {
658
668
foreach ($ matches as $ match ) {
659
669
$ 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 );
662
672
663
673
if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
664
674
if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -681,8 +691,8 @@ public function updateFormulaReferences(
681
691
if ($ matchCount > 0 ) {
682
692
foreach ($ matches as $ match ) {
683
693
$ 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 );
686
696
687
697
if ($ match [3 ] . $ match [4 ] !== $ modified3 . $ modified4 ) {
688
698
if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
@@ -709,7 +719,7 @@ public function updateFormulaReferences(
709
719
foreach ($ matches as $ match ) {
710
720
$ fromString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "{$ match [3 ]}" );
711
721
712
- $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
722
+ $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null );
713
723
if ($ match [3 ] !== $ modified3 ) {
714
724
if (self ::matchSheetName ($ match [2 ], $ worksheetName )) {
715
725
$ toString = self ::sheetnameBeforeCells ($ match [2 ], $ worksheetName , "$ modified3 " );
@@ -890,7 +900,7 @@ private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows
890
900
*
891
901
* @return string Updated cell range
892
902
*/
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 )
894
904
{
895
905
// Is it in another worksheet? Will not have to update anything.
896
906
if (str_contains ($ cellReference , '! ' )) {
@@ -902,7 +912,7 @@ private function updateCellReference(string $cellReference = 'A1', bool $include
902
912
/** @var CellReferenceHelper */
903
913
$ cellReferenceHelper = $ this ->cellReferenceHelper ;
904
914
905
- return $ cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
915
+ return $ cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, $ topLeft );
906
916
}
907
917
908
918
// Range
@@ -1008,14 +1018,14 @@ private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsol
1008
1018
$ cellReferenceHelper = $ this ->cellReferenceHelper ;
1009
1019
if (ctype_alpha ($ range [$ i ][$ j ])) {
1010
1020
$ range [$ i ][$ j ] = Coordinate::coordinateFromString (
1011
- $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences )
1021
+ $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null )
1012
1022
)[0 ];
1013
1023
} elseif (ctype_digit ($ range [$ i ][$ j ])) {
1014
1024
$ range [$ i ][$ j ] = Coordinate::coordinateFromString (
1015
- $ cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences )
1025
+ $ cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null )
1016
1026
)[1 ];
1017
1027
} else {
1018
- $ range [$ i ][$ j ] = $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences );
1028
+ $ range [$ i ][$ j ] = $ cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences , $ onlyAbsoluteReferences, null );
1019
1029
}
1020
1030
}
1021
1031
}
0 commit comments