@@ -372,30 +372,12 @@ public function insertNewBefore($beforeCellAddress, $numberOfColumns, $numberOfR
372
372
373
373
// 1. Clear column strips if we are removing columns
374
374
if ($ numberOfColumns < 0 && $ beforeColumn - 2 + $ numberOfColumns > 0 ) {
375
- for ($ i = 1 ; $ i <= $ highestRow - 1 ; ++$ i ) {
376
- for ($ j = $ beforeColumn - 1 + $ numberOfColumns ; $ j <= $ beforeColumn - 2 ; ++$ j ) {
377
- $ coordinate = Coordinate::stringFromColumnIndex ($ j + 1 ) . $ i ;
378
- $ worksheet ->removeConditionalStyles ($ coordinate );
379
- if ($ worksheet ->cellExists ($ coordinate )) {
380
- $ worksheet ->getCell ($ coordinate )->setValueExplicit ('' , DataType::TYPE_NULL );
381
- $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
382
- }
383
- }
384
- }
375
+ $ this ->clearColumnStrips ($ highestRow , $ beforeColumn , $ numberOfColumns , $ worksheet );
385
376
}
386
377
387
378
// 2. Clear row strips if we are removing rows
388
379
if ($ numberOfRows < 0 && $ beforeRow - 1 + $ numberOfRows > 0 ) {
389
- for ($ i = $ beforeColumn - 1 ; $ i <= Coordinate::columnIndexFromString ($ highestColumn ) - 1 ; ++$ i ) {
390
- for ($ j = $ beforeRow + $ numberOfRows ; $ j <= $ beforeRow - 1 ; ++$ j ) {
391
- $ coordinate = Coordinate::stringFromColumnIndex ($ i + 1 ) . $ j ;
392
- $ worksheet ->removeConditionalStyles ($ coordinate );
393
- if ($ worksheet ->cellExists ($ coordinate )) {
394
- $ worksheet ->getCell ($ coordinate )->setValueExplicit ('' , DataType::TYPE_NULL );
395
- $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
396
- }
397
- }
398
- }
380
+ $ this ->clearRowStrips ($ highestColumn , $ beforeColumn , $ beforeRow , $ numberOfRows , $ worksheet );
399
381
}
400
382
401
383
// Find missing coordinates. This is important when inserting column before the last column
@@ -466,9 +448,10 @@ function ($coordinate) use ($allCoordinates) {
466
448
$ highestRow = $ worksheet ->getHighestRow ();
467
449
468
450
if ($ numberOfColumns > 0 && $ beforeColumn - 2 > 0 ) {
451
+ $ beforeColumnName = Coordinate::stringFromColumnIndex ($ beforeColumn - 1 );
469
452
for ($ i = $ beforeRow ; $ i <= $ highestRow - 1 ; ++$ i ) {
470
453
// Style
471
- $ coordinate = Coordinate:: stringFromColumnIndex ( $ beforeColumn - 1 ) . $ i ;
454
+ $ coordinate = $ beforeColumnName . $ i ;
472
455
if ($ worksheet ->cellExists ($ coordinate )) {
473
456
$ xfIndex = $ worksheet ->getCell ($ coordinate )->getXfIndex ();
474
457
$ conditionalStyles = $ worksheet ->conditionalStylesExists ($ coordinate ) ?
@@ -488,7 +471,8 @@ function ($coordinate) use ($allCoordinates) {
488
471
}
489
472
490
473
if ($ numberOfRows > 0 && $ beforeRow - 1 > 0 ) {
491
- for ($ i = $ beforeColumn ; $ i <= Coordinate::columnIndexFromString ($ highestColumn ); ++$ i ) {
474
+ $ highestColumnIndex = Coordinate::columnIndexFromString ($ highestColumn );
475
+ for ($ i = $ beforeColumn ; $ i <= $ highestColumnIndex ; ++$ i ) {
492
476
// Style
493
477
$ coordinate = Coordinate::stringFromColumnIndex ($ i ) . ($ beforeRow - 1 );
494
478
if ($ worksheet ->cellExists ($ coordinate )) {
@@ -534,59 +518,7 @@ function ($coordinate) use ($allCoordinates) {
534
518
$ this ->adjustProtectedCells ($ worksheet , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows );
535
519
536
520
// Update worksheet: autofilter
537
- $ autoFilter = $ worksheet ->getAutoFilter ();
538
- $ autoFilterRange = $ autoFilter ->getRange ();
539
- if (!empty ($ autoFilterRange )) {
540
- if ($ numberOfColumns != 0 ) {
541
- $ autoFilterColumns = $ autoFilter ->getColumns ();
542
- if (count ($ autoFilterColumns ) > 0 ) {
543
- $ column = '' ;
544
- $ row = 0 ;
545
- sscanf ($ beforeCellAddress , '%[A-Z]%d ' , $ column , $ row );
546
- $ columnIndex = Coordinate::columnIndexFromString ($ column );
547
- [$ rangeStart , $ rangeEnd ] = Coordinate::rangeBoundaries ($ autoFilterRange );
548
- if ($ columnIndex <= $ rangeEnd [0 ]) {
549
- if ($ numberOfColumns < 0 ) {
550
- // If we're actually deleting any columns that fall within the autofilter range,
551
- // then we delete any rules for those columns
552
- $ deleteColumn = $ columnIndex + $ numberOfColumns - 1 ;
553
- $ deleteCount = abs ($ numberOfColumns );
554
- for ($ i = 1 ; $ i <= $ deleteCount ; ++$ i ) {
555
- if (isset ($ autoFilterColumns [Coordinate::stringFromColumnIndex ($ deleteColumn + 1 )])) {
556
- $ autoFilter ->clearColumn (Coordinate::stringFromColumnIndex ($ deleteColumn + 1 ));
557
- }
558
- ++$ deleteColumn ;
559
- }
560
- }
561
- $ startCol = ($ columnIndex > $ rangeStart [0 ]) ? $ columnIndex : $ rangeStart [0 ];
562
-
563
- // Shuffle columns in autofilter range
564
- if ($ numberOfColumns > 0 ) {
565
- $ startColRef = $ startCol ;
566
- $ endColRef = $ rangeEnd [0 ];
567
- $ toColRef = $ rangeEnd [0 ] + $ numberOfColumns ;
568
-
569
- do {
570
- $ autoFilter ->shiftColumn (Coordinate::stringFromColumnIndex ($ endColRef ), Coordinate::stringFromColumnIndex ($ toColRef ));
571
- --$ endColRef ;
572
- --$ toColRef ;
573
- } while ($ startColRef <= $ endColRef );
574
- } else {
575
- // For delete, we shuffle from beginning to end to avoid overwriting
576
- $ startColID = Coordinate::stringFromColumnIndex ($ startCol );
577
- $ toColID = Coordinate::stringFromColumnIndex ($ startCol + $ numberOfColumns );
578
- $ endColID = Coordinate::stringFromColumnIndex ($ rangeEnd [0 ] + 1 );
579
- do {
580
- $ autoFilter ->shiftColumn ($ startColID , $ toColID );
581
- ++$ startColID ;
582
- ++$ toColID ;
583
- } while ($ startColID != $ endColID );
584
- }
585
- }
586
- }
587
- }
588
- $ worksheet ->setAutoFilter ($ this ->updateCellReference ($ autoFilterRange , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows ));
589
- }
521
+ $ this ->adjustAutoFilter ($ worksheet , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows );
590
522
591
523
// Update worksheet: freeze pane
592
524
if ($ worksheet ->getFreezePane ()) {
@@ -1038,6 +970,93 @@ private function updateSingleCellReference($cellReference = 'A1', $beforeCellAdd
1038
970
return $ newColumn . $ newRow ;
1039
971
}
1040
972
973
+ private function clearColumnStrips (int $ highestRow , int $ beforeColumn , int $ numberOfColumns , Worksheet $ worksheet ): void
974
+ {
975
+ for ($ i = 1 ; $ i <= $ highestRow - 1 ; ++$ i ) {
976
+ for ($ j = $ beforeColumn - 1 + $ numberOfColumns ; $ j <= $ beforeColumn - 2 ; ++$ j ) {
977
+ $ coordinate = Coordinate::stringFromColumnIndex ($ j + 1 ) . $ i ;
978
+ $ worksheet ->removeConditionalStyles ($ coordinate );
979
+ if ($ worksheet ->cellExists ($ coordinate )) {
980
+ $ worksheet ->getCell ($ coordinate )->setValueExplicit ('' , DataType::TYPE_NULL );
981
+ $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
982
+ }
983
+ }
984
+ }
985
+ }
986
+
987
+ private function clearRowStrips (string $ highestColumn , int $ beforeColumn , int $ beforeRow , int $ numberOfRows , Worksheet $ worksheet ): void
988
+ {
989
+ $ lastColumnIndex = Coordinate::columnIndexFromString ($ highestColumn ) - 1 ;
990
+
991
+ for ($ i = $ beforeColumn - 1 ; $ i <= $ lastColumnIndex ; ++$ i ) {
992
+ for ($ j = $ beforeRow + $ numberOfRows ; $ j <= $ beforeRow - 1 ; ++$ j ) {
993
+ $ coordinate = Coordinate::stringFromColumnIndex ($ i + 1 ) . $ j ;
994
+ $ worksheet ->removeConditionalStyles ($ coordinate );
995
+ if ($ worksheet ->cellExists ($ coordinate )) {
996
+ $ worksheet ->getCell ($ coordinate )->setValueExplicit ('' , DataType::TYPE_NULL );
997
+ $ worksheet ->getCell ($ coordinate )->setXfIndex (0 );
998
+ }
999
+ }
1000
+ }
1001
+ }
1002
+
1003
+ private function adjustAutoFilter (Worksheet $ worksheet , string $ beforeCellAddress , int $ numberOfColumns , int $ numberOfRows ): void
1004
+ {
1005
+ $ autoFilter = $ worksheet ->getAutoFilter ();
1006
+ $ autoFilterRange = $ autoFilter ->getRange ();
1007
+ if (!empty ($ autoFilterRange )) {
1008
+ if ($ numberOfColumns != 0 ) {
1009
+ $ autoFilterColumns = $ autoFilter ->getColumns ();
1010
+ if (count ($ autoFilterColumns ) > 0 ) {
1011
+ $ column = '' ;
1012
+ $ row = 0 ;
1013
+ sscanf ($ beforeCellAddress , '%[A-Z]%d ' , $ column , $ row );
1014
+ $ columnIndex = Coordinate::columnIndexFromString ($ column );
1015
+ [$ rangeStart , $ rangeEnd ] = Coordinate::rangeBoundaries ($ autoFilterRange );
1016
+ if ($ columnIndex <= $ rangeEnd [0 ]) {
1017
+ if ($ numberOfColumns < 0 ) {
1018
+ // If we're actually deleting any columns that fall within the autofilter range,
1019
+ // then we delete any rules for those columns
1020
+ $ deleteColumn = $ columnIndex + $ numberOfColumns - 1 ;
1021
+ $ deleteCount = abs ($ numberOfColumns );
1022
+ for ($ i = 1 ; $ i <= $ deleteCount ; ++$ i ) {
1023
+ if (isset ($ autoFilterColumns [Coordinate::stringFromColumnIndex ($ deleteColumn + 1 )])) {
1024
+ $ autoFilter ->clearColumn (Coordinate::stringFromColumnIndex ($ deleteColumn + 1 ));
1025
+ }
1026
+ ++$ deleteColumn ;
1027
+ }
1028
+ }
1029
+ $ startCol = ($ columnIndex > $ rangeStart [0 ]) ? $ columnIndex : $ rangeStart [0 ];
1030
+
1031
+ // Shuffle columns in autofilter range
1032
+ if ($ numberOfColumns > 0 ) {
1033
+ $ startColRef = $ startCol ;
1034
+ $ endColRef = $ rangeEnd [0 ];
1035
+ $ toColRef = $ rangeEnd [0 ] + $ numberOfColumns ;
1036
+
1037
+ do {
1038
+ $ autoFilter ->shiftColumn (Coordinate::stringFromColumnIndex ($ endColRef ), Coordinate::stringFromColumnIndex ($ toColRef ));
1039
+ --$ endColRef ;
1040
+ --$ toColRef ;
1041
+ } while ($ startColRef <= $ endColRef );
1042
+ } else {
1043
+ // For delete, we shuffle from beginning to end to avoid overwriting
1044
+ $ startColID = Coordinate::stringFromColumnIndex ($ startCol );
1045
+ $ toColID = Coordinate::stringFromColumnIndex ($ startCol + $ numberOfColumns );
1046
+ $ endColID = Coordinate::stringFromColumnIndex ($ rangeEnd [0 ] + 1 );
1047
+ do {
1048
+ $ autoFilter ->shiftColumn ($ startColID , $ toColID );
1049
+ ++$ startColID ;
1050
+ ++$ toColID ;
1051
+ } while ($ startColID != $ endColID );
1052
+ }
1053
+ }
1054
+ }
1055
+ }
1056
+ $ worksheet ->setAutoFilter ($ this ->updateCellReference ($ autoFilterRange , $ beforeCellAddress , $ numberOfColumns , $ numberOfRows ));
1057
+ }
1058
+ }
1059
+
1041
1060
/**
1042
1061
* __clone implementation. Cloning should not be allowed in a Singleton!
1043
1062
*/
0 commit comments