@@ -154,7 +154,7 @@ protected function adjustPageBreaks(Worksheet $worksheet, int $numberOfColumns,
154
154
} else {
155
155
// Otherwise update any affected breaks by inserting a new break at the appropriate point
156
156
// and removing the old affected break
157
- $ newReference = $ this ->updateCellReference ($ cellAddress );
157
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
158
158
if ($ cellAddress !== $ newReference ) {
159
159
$ worksheet ->setBreak ($ newReference , $ value )
160
160
->setBreak ($ cellAddress , Worksheet::BREAK_NONE );
@@ -177,7 +177,7 @@ protected function adjustComments(Worksheet $worksheet): void
177
177
// Any comments inside a deleted range will be ignored
178
178
if ($ this ->cellReferenceHelper ->cellAddressInDeleteRange ($ cellAddress ) === false ) {
179
179
// Otherwise build a new array of comments indexed by the adjusted cell reference
180
- $ newReference = $ this ->updateCellReference ($ cellAddress );
180
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
181
181
$ aNewComments [$ newReference ] = $ value ;
182
182
}
183
183
}
@@ -200,12 +200,14 @@ protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns,
200
200
: uksort ($ aHyperlinkCollection , [self ::class, 'cellSort ' ]);
201
201
202
202
foreach ($ aHyperlinkCollection as $ cellAddress => $ value ) {
203
- $ newReference = $ this ->updateCellReference ($ cellAddress );
203
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
204
204
if ($ this ->cellReferenceHelper ->cellAddressInDeleteRange ($ cellAddress ) === true ) {
205
205
$ worksheet ->setHyperlink ($ cellAddress , null );
206
206
} elseif ($ cellAddress !== $ newReference ) {
207
- $ worksheet ->setHyperlink ($ newReference , $ value );
208
207
$ worksheet ->setHyperlink ($ cellAddress , null );
208
+ if ($ newReference ) {
209
+ $ worksheet ->setHyperlink ($ newReference , $ value );
210
+ }
209
211
}
210
212
}
211
213
}
@@ -226,7 +228,7 @@ protected function adjustConditionalFormatting(Worksheet $worksheet, int $number
226
228
227
229
foreach ($ aStyles as $ cellAddress => $ cfRules ) {
228
230
$ worksheet ->removeConditionalStyles ($ cellAddress );
229
- $ newReference = $ this ->updateCellReference ($ cellAddress );
231
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
230
232
231
233
foreach ($ cfRules as &$ cfRule ) {
232
234
/** @var Conditional $cfRule */
@@ -264,11 +266,13 @@ protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColu
264
266
: uksort ($ aDataValidationCollection , [self ::class, 'cellSort ' ]);
265
267
266
268
foreach ($ aDataValidationCollection as $ cellAddress => $ dataValidation ) {
267
- $ newReference = $ this ->updateCellReference ($ cellAddress );
269
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
268
270
if ($ cellAddress !== $ newReference ) {
269
271
$ dataValidation ->setSqref ($ newReference );
270
- $ worksheet ->setDataValidation ($ newReference , $ dataValidation );
271
272
$ worksheet ->setDataValidation ($ cellAddress , null );
273
+ if ($ newReference ) {
274
+ $ worksheet ->setDataValidation ($ newReference , $ dataValidation );
275
+ }
272
276
}
273
277
}
274
278
}
@@ -283,8 +287,10 @@ protected function adjustMergeCells(Worksheet $worksheet): void
283
287
$ aMergeCells = $ worksheet ->getMergeCells ();
284
288
$ aNewMergeCells = []; // the new array of all merge cells
285
289
foreach ($ aMergeCells as $ cellAddress => &$ value ) {
286
- $ newReference = $ this ->updateCellReference ($ cellAddress );
287
- $ aNewMergeCells [$ newReference ] = $ newReference ;
290
+ $ newReference = $ this ->updateCellReference ($ cellAddress , false , null );
291
+ if ($ newReference ) {
292
+ $ aNewMergeCells [$ newReference ] = $ newReference ;
293
+ }
288
294
}
289
295
$ worksheet ->setMergeCells ($ aNewMergeCells ); // replace the merge cells array
290
296
}
@@ -303,10 +309,12 @@ protected function adjustProtectedCells(Worksheet $worksheet, int $numberOfColum
303
309
? uksort ($ aProtectedCells , [self ::class, 'cellReverseSort ' ])
304
310
: uksort ($ aProtectedCells , [self ::class, 'cellSort ' ]);
305
311
foreach ($ aProtectedCells as $ cellAddress => $ value ) {
306
- $ newReference = $ this ->updateCellReference ($ cellAddress );
312
+ $ newReference = $ this ->updateCellReference ($ cellAddress, false , null );
307
313
if ($ cellAddress !== $ newReference ) {
308
- $ worksheet ->protectCells ($ newReference , $ value , true );
309
314
$ worksheet ->unprotectCells ($ cellAddress );
315
+ if ($ newReference ) {
316
+ $ worksheet ->protectCells ($ newReference , $ value , true );
317
+ }
310
318
}
311
319
}
312
320
}
@@ -321,7 +329,7 @@ protected function adjustColumnDimensions(Worksheet $worksheet): void
321
329
$ aColumnDimensions = array_reverse ($ worksheet ->getColumnDimensions (), true );
322
330
if (!empty ($ aColumnDimensions )) {
323
331
foreach ($ aColumnDimensions as $ objColumnDimension ) {
324
- $ newReference = $ this ->updateCellReference ($ objColumnDimension ->getColumnIndex () . '1 ' );
332
+ $ newReference = $ this ->updateCellReference ($ objColumnDimension ->getColumnIndex () . '1 ' , false , null );
325
333
[$ newReference ] = Coordinate::coordinateFromString ($ newReference );
326
334
if ($ objColumnDimension ->getColumnIndex () !== $ newReference ) {
327
335
$ objColumnDimension ->setColumnIndex ($ newReference );
@@ -344,7 +352,7 @@ protected function adjustRowDimensions(Worksheet $worksheet, $beforeRow, $number
344
352
$ aRowDimensions = array_reverse ($ worksheet ->getRowDimensions (), true );
345
353
if (!empty ($ aRowDimensions )) {
346
354
foreach ($ aRowDimensions as $ objRowDimension ) {
347
- $ newReference = $ this ->updateCellReference ('A ' . $ objRowDimension ->getRowIndex ());
355
+ $ newReference = $ this ->updateCellReference ('A ' . $ objRowDimension ->getRowIndex (), false , null );
348
356
[, $ newReference ] = Coordinate::coordinateFromString ($ newReference );
349
357
$ newRoweference = (int ) $ newReference ;
350
358
if ($ objRowDimension ->getRowIndex () !== $ newRoweference ) {
@@ -434,7 +442,8 @@ function ($coordinate) use ($cellCollection) {
434
442
$ cell = $ worksheet ->getCell ($ coordinate );
435
443
$ cellIndex = Coordinate::columnIndexFromString ($ cell ->getColumn ());
436
444
437
- if ($ cellIndex - 1 + $ numberOfColumns < 0 ) {
445
+ // Don't update cells that are being removed
446
+ if ($ numberOfColumns < 0 && $ cellIndex >= $ beforeColumn + $ numberOfColumns && $ cellIndex < $ beforeColumn ) {
438
447
continue ;
439
448
}
440
449
@@ -518,28 +527,28 @@ function ($coordinate) use ($cellCollection) {
518
527
$ splitCell = $ worksheet ->getFreezePane ();
519
528
$ topLeftCell = $ worksheet ->getTopLeftCell () ?? '' ;
520
529
521
- $ splitCell = $ this ->updateCellReference ($ splitCell );
522
- $ topLeftCell = $ this ->updateCellReference ($ topLeftCell );
530
+ $ splitCell = $ this ->updateCellReference ($ splitCell, false , null );
531
+ $ topLeftCell = $ this ->updateCellReference ($ topLeftCell, false , null );
523
532
524
533
$ worksheet ->freezePane ($ splitCell , $ topLeftCell );
525
534
}
526
535
527
536
// Page setup
528
537
if ($ worksheet ->getPageSetup ()->isPrintAreaSet ()) {
529
538
$ worksheet ->getPageSetup ()->setPrintArea (
530
- $ this ->updateCellReference ($ worksheet ->getPageSetup ()->getPrintArea ())
539
+ $ this ->updateCellReference ($ worksheet ->getPageSetup ()->getPrintArea (), false , null )
531
540
);
532
541
}
533
542
534
543
// Update worksheet: drawings
535
544
$ aDrawings = $ worksheet ->getDrawingCollection ();
536
545
foreach ($ aDrawings as $ objDrawing ) {
537
- $ newReference = $ this ->updateCellReference ($ objDrawing ->getCoordinates ());
546
+ $ newReference = $ this ->updateCellReference ($ objDrawing ->getCoordinates (), false , null );
538
547
if ($ objDrawing ->getCoordinates () != $ newReference ) {
539
548
$ objDrawing ->setCoordinates ($ newReference );
540
549
}
541
550
if ($ objDrawing ->getCoordinates2 () !== '' ) {
542
- $ newReference = $ this ->updateCellReference ($ objDrawing ->getCoordinates2 ());
551
+ $ newReference = $ this ->updateCellReference ($ objDrawing ->getCoordinates2 (), false , null );
543
552
if ($ objDrawing ->getCoordinates2 () != $ newReference ) {
544
553
$ objDrawing ->setCoordinates2 ($ newReference );
545
554
}
@@ -596,8 +605,8 @@ public function updateFormulaReferences(
596
605
foreach ($ matches as $ match ) {
597
606
$ fromString = ($ match [2 ] > '' ) ? $ match [2 ] . '! ' : '' ;
598
607
$ fromString .= $ match [3 ] . ': ' . $ match [4 ];
599
- $ modified3 = substr ($ this ->updateCellReference ('$A ' . $ match [3 ], $ includeAbsoluteReferences ), 2 );
600
- $ modified4 = substr ($ this ->updateCellReference ('$A ' . $ match [4 ], $ includeAbsoluteReferences ), 2 );
608
+ $ modified3 = substr ($ this ->updateCellReference ('$A ' . $ match [3 ], $ includeAbsoluteReferences, true ), 2 );
609
+ $ modified4 = substr ($ this ->updateCellReference ('$A ' . $ match [4 ], $ includeAbsoluteReferences, false ), 2 );
601
610
602
611
if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
603
612
if (($ match [2 ] == '' ) || (trim ($ match [2 ], "' " ) == $ worksheetName )) {
@@ -621,8 +630,8 @@ public function updateFormulaReferences(
621
630
foreach ($ matches as $ match ) {
622
631
$ fromString = ($ match [2 ] > '' ) ? $ match [2 ] . '! ' : '' ;
623
632
$ fromString .= $ match [3 ] . ': ' . $ match [4 ];
624
- $ modified3 = substr ($ this ->updateCellReference ($ match [3 ] . '$1 ' , $ includeAbsoluteReferences ), 0 , -2 );
625
- $ modified4 = substr ($ this ->updateCellReference ($ match [4 ] . '$1 ' , $ includeAbsoluteReferences ), 0 , -2 );
633
+ $ modified3 = substr ($ this ->updateCellReference ($ match [3 ] . '$1 ' , $ includeAbsoluteReferences, true ), 0 , -2 );
634
+ $ modified4 = substr ($ this ->updateCellReference ($ match [4 ] . '$1 ' , $ includeAbsoluteReferences, false ), 0 , -2 );
626
635
627
636
if ($ match [3 ] . ': ' . $ match [4 ] !== $ modified3 . ': ' . $ modified4 ) {
628
637
if (($ match [2 ] == '' ) || (trim ($ match [2 ], "' " ) == $ worksheetName )) {
@@ -646,8 +655,8 @@ public function updateFormulaReferences(
646
655
foreach ($ matches as $ match ) {
647
656
$ fromString = ($ match [2 ] > '' ) ? $ match [2 ] . '! ' : '' ;
648
657
$ fromString .= $ match [3 ] . ': ' . $ match [4 ];
649
- $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences );
650
- $ modified4 = $ this ->updateCellReference ($ match [4 ], $ includeAbsoluteReferences );
658
+ $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences, true );
659
+ $ modified4 = $ this ->updateCellReference ($ match [4 ], $ includeAbsoluteReferences, false );
651
660
652
661
if ($ match [3 ] . $ match [4 ] !== $ modified3 . $ modified4 ) {
653
662
if (($ match [2 ] == '' ) || (trim ($ match [2 ], "' " ) == $ worksheetName )) {
@@ -674,7 +683,7 @@ public function updateFormulaReferences(
674
683
$ fromString = ($ match [2 ] > '' ) ? $ match [2 ] . '! ' : '' ;
675
684
$ fromString .= $ match [3 ];
676
685
677
- $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences );
686
+ $ modified3 = $ this ->updateCellReference ($ match [3 ], $ includeAbsoluteReferences, null );
678
687
if ($ match [3 ] !== $ modified3 ) {
679
688
if (($ match [2 ] == '' ) || (trim ($ match [2 ], "' " ) == $ worksheetName )) {
680
689
$ toString = ($ match [2 ] > '' ) ? $ match [2 ] . '! ' : '' ;
@@ -854,7 +863,7 @@ private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows
854
863
*
855
864
* @return string Updated cell range
856
865
*/
857
- private function updateCellReference ($ cellReference = 'A1 ' , bool $ includeAbsoluteReferences = false )
866
+ private function updateCellReference ($ cellReference = 'A1 ' , bool $ includeAbsoluteReferences = false , ? bool $ topLeft = null )
858
867
{
859
868
// Is it in another worksheet? Will not have to update anything.
860
869
if (strpos ($ cellReference , '! ' ) !== false ) {
@@ -863,7 +872,7 @@ private function updateCellReference($cellReference = 'A1', bool $includeAbsolut
863
872
// Is it a range or a single cell?
864
873
if (!Coordinate::coordinateIsRange ($ cellReference )) {
865
874
// Single cell
866
- return $ this ->cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences );
875
+ return $ this ->cellReferenceHelper ->updateCellReference ($ cellReference , $ includeAbsoluteReferences, $ topLeft );
867
876
}
868
877
869
878
// Range
@@ -925,7 +934,7 @@ private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet
925
934
$ formula = $ this ->updateFormulaReferences ($ cellAddress , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows , $ worksheet ->getTitle (), true );
926
935
$ definedName ->setValue ($ formula );
927
936
} else {
928
- $ definedName ->setValue ($ this ->updateCellReference (ltrim ($ cellAddress , '= ' ), true ));
937
+ $ definedName ->setValue ($ this ->updateCellReference (ltrim ($ cellAddress , '= ' ), true , null ));
929
938
}
930
939
}
931
940
}
@@ -967,14 +976,14 @@ private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsol
967
976
for ($ j = 0 ; $ j < $ jc ; ++$ j ) {
968
977
if (ctype_alpha ($ range [$ i ][$ j ])) {
969
978
$ range [$ i ][$ j ] = Coordinate::coordinateFromString (
970
- $ this ->cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences )
979
+ $ this ->cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ] . '1 ' , $ includeAbsoluteReferences, null )
971
980
)[0 ];
972
981
} elseif (ctype_digit ($ range [$ i ][$ j ])) {
973
982
$ range [$ i ][$ j ] = Coordinate::coordinateFromString (
974
- $ this ->cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences )
983
+ $ this ->cellReferenceHelper ->updateCellReference ('A ' . $ range [$ i ][$ j ], $ includeAbsoluteReferences, null )
975
984
)[1 ];
976
985
} else {
977
- $ range [$ i ][$ j ] = $ this ->cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences );
986
+ $ range [$ i ][$ j ] = $ this ->cellReferenceHelper ->updateCellReference ($ range [$ i ][$ j ], $ includeAbsoluteReferences, null );
978
987
}
979
988
}
980
989
}
@@ -1052,7 +1061,7 @@ private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddres
1052
1061
}
1053
1062
1054
1063
$ worksheet ->setAutoFilter (
1055
- $ this ->updateCellReference ($ autoFilterRange )
1064
+ $ this ->updateCellReference ($ autoFilterRange, false , null )
1056
1065
);
1057
1066
}
1058
1067
}
@@ -1131,7 +1140,7 @@ private function adjustTable(Worksheet $worksheet, string $beforeCellAddress, in
1131
1140
}
1132
1141
}
1133
1142
1134
- $ table ->setRange ($ this ->updateCellReference ($ tableRange ));
1143
+ $ table ->setRange ($ this ->updateCellReference ($ tableRange, false , null ));
1135
1144
}
1136
1145
}
1137
1146
}
0 commit comments