@@ -558,7 +558,7 @@ public function insertNewBefore(
558
558
$ worksheet ->freezePane ($ splitCell , $ topLeftCell );
559
559
}
560
560
561
- $ this ->updatePrintArea ($ worksheet , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows );
561
+ $ this ->updatePrintAreas ($ worksheet , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows );
562
562
563
563
// Update worksheet: drawings
564
564
$ aDrawings = $ worksheet ->getDrawingCollection ();
@@ -584,13 +584,30 @@ public function insertNewBefore(
584
584
$ worksheet ->garbageCollect ();
585
585
}
586
586
587
- private function updatePrintArea (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns , int $ numberOfRows ): void
587
+ private function updatePrintAreas (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns , int $ numberOfRows ): void
588
588
{
589
589
$ pageSetup = $ worksheet ->getPageSetup ();
590
590
if (!$ pageSetup ->isPrintAreaSet ()) {
591
591
return ;
592
592
}
593
- $ printArea = $ pageSetup ->getPrintArea ();
593
+ $ printAreas = explode (', ' , $ pageSetup ->getPrintArea ());
594
+ $ newPrintAreas = [];
595
+ foreach ($ printAreas as $ printArea ) {
596
+ $ result = $ this ->updatePrintArea ($ printArea , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows );
597
+ if ($ result !== '' ) {
598
+ $ newPrintAreas [] = $ result ;
599
+ }
600
+ }
601
+ $ result = implode (', ' , $ newPrintAreas );
602
+ if ($ result === '' ) {
603
+ $ pageSetup ->clearPrintArea ();
604
+ } else {
605
+ $ pageSetup ->setPrintArea ($ result );
606
+ }
607
+ }
608
+
609
+ private function updatePrintArea (string $ printArea , string $ beforeCellAddress , int $ numberOfColumns , int $ numberOfRows ): string
610
+ {
594
611
$ coordinates = Coordinate::indexesFromString ($ beforeCellAddress );
595
612
if (preg_match ('/^([A-Z]{1,3})(\d{1,7}):([A-Z]{1,3})(\d{1,7})$/i ' , $ printArea , $ matches ) === 1 ) {
596
613
$ firstRow = (int ) $ matches [2 ];
@@ -603,63 +620,55 @@ private function updatePrintArea(Worksheet $worksheet, string $beforeCellAddress
603
620
if ($ affectedRow >= $ firstRow && $ affectedRow <= $ lastRow ) {
604
621
$ newLastRow = max ($ affectedRow , $ lastRow + $ numberOfRows );
605
622
if ($ newLastRow >= $ firstRow ) {
606
- $ pageSetup ->setPrintArea ($ matches [1 ] . $ matches [2 ] . ': ' . $ matches [3 ] . $ newLastRow );
607
- } else {
608
- $ pageSetup ->clearPrintArea ();
623
+ return $ matches [1 ] . $ matches [2 ] . ': ' . $ matches [3 ] . $ newLastRow ;
609
624
}
610
625
611
- return ;
626
+ return '' ;
612
627
}
613
628
if ($ lastAffectedRow >= $ firstRow && $ affectedRow <= $ lastRow ) {
614
629
$ newFirstRow = $ affectedRow + 1 ;
615
630
$ newLastRow = $ lastRow + $ numberOfRows ;
616
631
if ($ newFirstRow >= 1 && $ newLastRow >= $ newFirstRow ) {
617
- $ pageSetup ->setPrintArea ($ matches [1 ] . $ newFirstRow . ': ' . $ matches [3 ] . $ newLastRow );
618
- } else {
619
- $ pageSetup ->clearPrintArea ();
632
+ return $ matches [1 ] . $ newFirstRow . ': ' . $ matches [3 ] . $ newLastRow ;
620
633
}
621
634
622
- return ;
635
+ return '' ;
623
636
}
624
637
}
625
638
if ($ numberOfColumns < 0 ) {
626
639
$ firstColumnInt = Coordinate::columnIndexFromString ($ firstColumnString );
627
640
$ lastColumnInt = Coordinate::columnIndexFromString ($ lastColumnString );
628
641
$ affectedColumn = $ coordinates [0 ] + $ numberOfColumns - 1 ;
629
642
$ lastAffectedColumn = $ coordinates [0 ] - 1 ;
630
- //var_dump($affectedColumn, $lastAffectedColumn, $firstColumnInt, $lastColumnInt);
631
643
if ($ affectedColumn >= $ firstColumnInt && $ affectedColumn <= $ lastColumnInt ) {
632
644
$ newLastColumnInt = max ($ affectedColumn , $ lastColumnInt + $ numberOfColumns );
633
645
if ($ newLastColumnInt >= $ firstColumnInt ) {
634
646
$ newLastColumnString = Coordinate::stringFromColumnIndex ($ newLastColumnInt );
635
- $ pageSetup ->setPrintArea ($ matches [1 ] . $ matches [2 ] . ': ' . $ newLastColumnString . $ matches [4 ]);
636
- } else {
637
- $ pageSetup ->clearPrintArea ();
647
+
648
+ return $ matches [1 ] . $ matches [2 ] . ': ' . $ newLastColumnString . $ matches [4 ];
638
649
}
639
650
640
- return ;
651
+ return '' ;
641
652
}
642
653
if ($ affectedColumn < $ firstColumnInt && $ lastAffectedColumn > $ lastColumnInt ) {
643
- $ pageSetup ->clearPrintArea ();
644
-
645
- return ;
654
+ return '' ;
646
655
}
647
656
if ($ lastAffectedColumn >= $ firstColumnInt && $ lastAffectedColumn <= $ lastColumnInt ) {
648
657
$ newFirstColumn = $ affectedColumn + 1 ;
649
658
$ newLastColumn = $ lastColumnInt + $ numberOfColumns ;
650
659
if ($ newFirstColumn >= 1 && $ newLastColumn >= $ newFirstColumn ) {
651
660
$ firstString = Coordinate::stringFromColumnIndex ($ newFirstColumn );
652
661
$ lastString = Coordinate::stringFromColumnIndex ($ newLastColumn );
653
- $ pageSetup ->setPrintArea ($ firstString . $ matches [2 ] . ': ' . $ lastString . $ matches [4 ]);
654
- } else {
655
- $ pageSetup ->clearPrintArea ();
662
+
663
+ return $ firstString . $ matches [2 ] . ': ' . $ lastString . $ matches [4 ];
656
664
}
657
665
658
- return ;
666
+ return '' ;
659
667
}
660
668
}
661
669
}
662
- $ pageSetup ->setPrintArea ($ this ->updateCellReference ($ printArea ));
670
+
671
+ return $ this ->updateCellReference ($ printArea );
663
672
}
664
673
665
674
private static function matchSheetName (?string $ match , string $ worksheetName ): bool
0 commit comments