@@ -432,7 +432,7 @@ public function insertNewBefore($pBefore, $pNumCols, $pNumRows, Worksheet $pShee
432
432
if ($ cell ->getDataType () == DataType::TYPE_FORMULA ) {
433
433
// Formula should be adjusted
434
434
$ pSheet ->getCell ($ newCoordinate )
435
- ->setValue ($ this ->updateFormulaReferences ($ cell ->getValue (), $ pBefore , $ pNumCols , $ pNumRows , $ pSheet ->getTitle ()));
435
+ ->setValue ($ this ->updateFormulaReferences ($ cell ->getValue (), $ pBefore , $ pNumCols , $ pNumRows , $ pSheet ->getTitle ()));
436
436
} else {
437
437
// Formula should not be adjusted
438
438
$ pSheet ->getCell ($ newCoordinate )->setValue ($ cell ->getValue ());
@@ -760,7 +760,7 @@ public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCo
760
760
* Update cell reference.
761
761
*
762
762
* @param string $pCellRange Cell range
763
- * @param int $pBefore Insert before this one
763
+ * @param string $pBefore Insert before this one
764
764
* @param int $pNumCols Number of columns to increment
765
765
* @param int $pNumRows Number of rows to increment
766
766
*
@@ -774,13 +774,14 @@ public function updateCellReference($pCellRange = 'A1', $pBefore = 'A1', $pNumCo
774
774
if (strpos ($ pCellRange , '! ' ) !== false ) {
775
775
return $ pCellRange ;
776
776
// Is it a range or a single cell?
777
- } elseif (strpos ( $ pCellRange , ' : ' ) === false && strpos ($ pCellRange, ' , ' ) === false ) {
777
+ } elseif (!Coordinate:: coordinateIsRange ($ pCellRange) ) {
778
778
// Single cell
779
779
return $ this ->updateSingleCellReference ($ pCellRange , $ pBefore , $ pNumCols , $ pNumRows );
780
- } elseif (strpos ( $ pCellRange , ' : ' ) !== false || strpos ($ pCellRange, ' , ' ) !== false ) {
780
+ } elseif (Coordinate:: coordinateIsRange ($ pCellRange) ) {
781
781
// Range
782
782
return $ this ->updateCellRange ($ pCellRange , $ pBefore , $ pNumCols , $ pNumRows );
783
783
}
784
+
784
785
// Return original
785
786
return $ pCellRange ;
786
787
}
@@ -817,7 +818,7 @@ public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $ne
817
818
* Update cell range.
818
819
*
819
820
* @param string $pCellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3')
820
- * @param int $pBefore Insert before this one
821
+ * @param string $pBefore Insert before this one
821
822
* @param int $pNumCols Number of columns to increment
822
823
* @param int $pNumRows Number of rows to increment
823
824
*
@@ -827,37 +828,37 @@ public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $ne
827
828
*/
828
829
private function updateCellRange ($ pCellRange = 'A1:A1 ' , $ pBefore = 'A1 ' , $ pNumCols = 0 , $ pNumRows = 0 )
829
830
{
830
- if (strpos ($ pCellRange , ': ' ) !== false || strpos ($ pCellRange , ', ' ) !== false ) {
831
- // Update range
832
- $ range = Coordinate::splitRange ($ pCellRange );
833
- $ ic = count ($ range );
834
- for ($ i = 0 ; $ i < $ ic ; ++$ i ) {
835
- $ jc = count ($ range [$ i ]);
836
- for ($ j = 0 ; $ j < $ jc ; ++$ j ) {
837
- if (ctype_alpha ($ range [$ i ][$ j ])) {
838
- $ r = Coordinate::coordinateFromString ($ this ->updateSingleCellReference ($ range [$ i ][$ j ] . '1 ' , $ pBefore , $ pNumCols , $ pNumRows ));
839
- $ range [$ i ][$ j ] = $ r [0 ];
840
- } elseif (ctype_digit ($ range [$ i ][$ j ])) {
841
- $ r = Coordinate::coordinateFromString ($ this ->updateSingleCellReference ('A ' . $ range [$ i ][$ j ], $ pBefore , $ pNumCols , $ pNumRows ));
842
- $ range [$ i ][$ j ] = $ r [1 ];
843
- } else {
844
- $ range [$ i ][$ j ] = $ this ->updateSingleCellReference ($ range [$ i ][$ j ], $ pBefore , $ pNumCols , $ pNumRows );
845
- }
831
+ if (!Coordinate::coordinateIsRange ($ pCellRange )) {
832
+ throw new Exception ('Only cell ranges may be passed to this method. ' );
833
+ }
834
+
835
+ // Update range
836
+ $ range = Coordinate::splitRange ($ pCellRange );
837
+ $ ic = count ($ range );
838
+ for ($ i = 0 ; $ i < $ ic ; ++$ i ) {
839
+ $ jc = count ($ range [$ i ]);
840
+ for ($ j = 0 ; $ j < $ jc ; ++$ j ) {
841
+ if (ctype_alpha ($ range [$ i ][$ j ])) {
842
+ $ r = Coordinate::coordinateFromString ($ this ->updateSingleCellReference ($ range [$ i ][$ j ] . '1 ' , $ pBefore , $ pNumCols , $ pNumRows ));
843
+ $ range [$ i ][$ j ] = $ r [0 ];
844
+ } elseif (ctype_digit ($ range [$ i ][$ j ])) {
845
+ $ r = Coordinate::coordinateFromString ($ this ->updateSingleCellReference ('A ' . $ range [$ i ][$ j ], $ pBefore , $ pNumCols , $ pNumRows ));
846
+ $ range [$ i ][$ j ] = $ r [1 ];
847
+ } else {
848
+ $ range [$ i ][$ j ] = $ this ->updateSingleCellReference ($ range [$ i ][$ j ], $ pBefore , $ pNumCols , $ pNumRows );
846
849
}
847
850
}
848
-
849
- // Recreate range string
850
- return Coordinate::buildRange ($ range );
851
851
}
852
852
853
- throw new Exception ('Only cell ranges may be passed to this method. ' );
853
+ // Recreate range string
854
+ return Coordinate::buildRange ($ range );
854
855
}
855
856
856
857
/**
857
858
* Update single cell reference.
858
859
*
859
860
* @param string $pCellReference Single cell reference
860
- * @param int $pBefore Insert before this one
861
+ * @param string $pBefore Insert before this one
861
862
* @param int $pNumCols Number of columns to increment
862
863
* @param int $pNumRows Number of rows to increment
863
864
*
@@ -867,32 +868,32 @@ private function updateCellRange($pCellRange = 'A1:A1', $pBefore = 'A1', $pNumCo
867
868
*/
868
869
private function updateSingleCellReference ($ pCellReference = 'A1 ' , $ pBefore = 'A1 ' , $ pNumCols = 0 , $ pNumRows = 0 )
869
870
{
870
- if (strpos ( $ pCellReference , ' : ' ) === false && strpos ($ pCellReference, ' , ' ) === false ) {
871
- // Get coordinate of $pBefore
872
- list ( $ beforeColumn , $ beforeRow ) = Coordinate:: coordinateFromString ( $ pBefore );
871
+ if (Coordinate:: coordinateIsRange ($ pCellReference) ) {
872
+ throw new Exception ( ' Only single cell references may be passed to this method. ' );
873
+ }
873
874
874
- // Get coordinate of $pCellReference
875
- list ($ newColumn , $ newRow ) = Coordinate::coordinateFromString ($ pCellReference );
875
+ // Get coordinate of $pBefore
876
+ list ($ beforeColumn , $ beforeRow ) = Coordinate::coordinateFromString ($ pBefore );
876
877
877
- // Verify which parts should be updated
878
- $ updateColumn = (($ newColumn [0 ] != '$ ' ) && ($ beforeColumn [0 ] != '$ ' ) && (Coordinate::columnIndexFromString ($ newColumn ) >= Coordinate::columnIndexFromString ($ beforeColumn )));
879
- $ updateRow = (($ newRow [0 ] != '$ ' ) && ($ beforeRow [0 ] != '$ ' ) && $ newRow >= $ beforeRow );
878
+ // Get coordinate of $pCellReference
879
+ list ($ newColumn , $ newRow ) = Coordinate::coordinateFromString ($ pCellReference );
880
880
881
- // Create new column reference
882
- if ($ updateColumn ) {
883
- $ newColumn = Coordinate::stringFromColumnIndex (Coordinate::columnIndexFromString ($ newColumn ) + $ pNumCols );
884
- }
881
+ // Verify which parts should be updated
882
+ $ updateColumn = (($ newColumn [0 ] != '$ ' ) && ($ beforeColumn [0 ] != '$ ' ) && (Coordinate::columnIndexFromString ($ newColumn ) >= Coordinate::columnIndexFromString ($ beforeColumn )));
883
+ $ updateRow = (($ newRow [0 ] != '$ ' ) && ($ beforeRow [0 ] != '$ ' ) && $ newRow >= $ beforeRow );
885
884
886
- // Create new row reference
887
- if ($ updateRow ) {
888
- $ newRow = $ newRow + $ pNumRows ;
889
- }
885
+ // Create new column reference
886
+ if ($ updateColumn ) {
887
+ $ newColumn = Coordinate:: stringFromColumnIndex (Coordinate:: columnIndexFromString ( $ newColumn ) + $ pNumCols ) ;
888
+ }
890
889
891
- // Return new reference
892
- return $ newColumn . $ newRow ;
890
+ // Create new row reference
891
+ if ($ updateRow ) {
892
+ $ newRow = $ newRow + $ pNumRows ;
893
893
}
894
894
895
- throw new Exception ('Only single cell references may be passed to this method. ' );
895
+ // Return new reference
896
+ return $ newColumn . $ newRow ;
896
897
}
897
898
898
899
/**
0 commit comments