@@ -1113,12 +1113,9 @@ public function getHighestRowAndColumn()
1113
1113
* @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
1114
1114
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
1115
1115
*/
1116
- protected function validateCellAddress ($ cellAddress, bool $ allowNull = false ): ? string
1116
+ protected function validateCellAddress ($ cellAddress ): string
1117
1117
{
1118
- if (is_string ($ cellAddress ) || ($ cellAddress === null && $ allowNull === true )) {
1119
- if ($ cellAddress === null ) {
1120
- return null ;
1121
- }
1118
+ if (is_string ($ cellAddress )) {
1122
1119
[$ worksheet , $ address ] = self ::extractSheetTitle ($ cellAddress , true );
1123
1120
// if (!empty($worksheet) && $worksheet !== $this->getTitle()) {
1124
1121
// throw new Exception('Reference is not for this worksheet');
@@ -1134,16 +1131,38 @@ protected function validateCellAddress($cellAddress, bool $allowNull = false): ?
1134
1131
return (string ) $ cellAddress ;
1135
1132
}
1136
1133
1134
+ private function tryDefinedName (string $ coordinate ): string
1135
+ {
1136
+ // Uppercase coordinate
1137
+ $ coordinate = strtoupper ($ coordinate );
1138
+ // Eliminate leading equal sign
1139
+ $ coordinate = self ::pregReplace ('/^=/ ' , '' , $ coordinate );
1140
+ $ defined = $ this ->parent ->getDefinedName ($ coordinate , $ this );
1141
+ if ($ defined !== null ) {
1142
+ if ($ defined ->getWorksheet () === $ this && !$ defined ->isFormula ()) {
1143
+ $ coordinate = self ::pregReplace ('/^=/ ' , '' , $ defined ->getValue ());
1144
+ }
1145
+ }
1146
+
1147
+ return $ coordinate ;
1148
+ }
1149
+
1137
1150
/**
1138
- * Validate a cell range.
1151
+ * Validate a cell address or cell range.
1139
1152
*
1140
- * @param AddressRange|array<int>|CellAddress|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
1153
+ * @param AddressRange|array<int>|CellAddress|int| string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
1141
1154
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
1142
1155
* or as a CellAddress or AddressRange object.
1143
1156
*/
1144
1157
protected function validateCellOrCellRange ($ cellRange ): string
1145
1158
{
1146
- if (is_object ($ cellRange ) && $ cellRange instanceof CellAddress) {
1159
+ if (is_string ($ cellRange ) || is_numeric ($ cellRange )) {
1160
+ $ cellRange = (string ) $ cellRange ;
1161
+ // Convert a single column reference like 'A' to 'A:A'
1162
+ $ cellRange = self ::pregReplace ('/^([A-Z]+)$/ ' , '${1}:${1} ' , $ cellRange );
1163
+ // Convert a single row reference like '1' to '1:1'
1164
+ $ cellRange = self ::pregReplace ('/^(\d+)$/ ' , '${1}:${1} ' , $ cellRange );
1165
+ } elseif (is_object ($ cellRange ) && $ cellRange instanceof CellAddress) {
1147
1166
$ cellRange = new CellRange ($ cellRange , $ cellRange );
1148
1167
}
1149
1168
@@ -1162,9 +1181,9 @@ protected function validateCellRange($cellRange): string
1162
1181
if (is_string ($ cellRange )) {
1163
1182
[$ worksheet , $ addressRange ] = self ::extractSheetTitle ($ cellRange , true );
1164
1183
1165
- // Convert 'A:C' to 'A1:C1048576'
1184
+ // Convert Column ranges like 'A:C' to 'A1:C1048576'
1166
1185
$ addressRange = self ::pregReplace ('/^([A-Z]+):([A-Z]+)$/ ' , '${1}1:${2}1048576 ' , $ addressRange );
1167
- // Convert '1:3' to 'A1:XFD3'
1186
+ // Convert Row ranges like '1:3' to 'A1:XFD3'
1168
1187
$ addressRange = self ::pregReplace ('/^( \\d+):( \\d+)$/ ' , 'A${1}:XFD${2} ' , $ addressRange );
1169
1188
1170
1189
return empty ($ worksheet ) ? strtoupper ($ addressRange ) : $ worksheet . '! ' . strtoupper ($ addressRange );
@@ -1189,8 +1208,7 @@ protected function validateCellRange($cellRange): string
1189
1208
*/
1190
1209
public function setCellValue ($ coordinate , $ value )
1191
1210
{
1192
- /** @var string $cellAddress */
1193
- $ cellAddress = $ this ->validateCellAddress ($ coordinate );
1211
+ $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ));
1194
1212
$ this ->getCell ($ cellAddress )->setValue ($ value );
1195
1213
1196
1214
return $ this ;
@@ -1211,7 +1229,7 @@ public function setCellValue($coordinate, $value)
1211
1229
*/
1212
1230
public function setCellValueByColumnAndRow ($ columnIndex , $ row , $ value )
1213
1231
{
1214
- $ this ->getCell ([ $ columnIndex, $ row] )->setValue ($ value );
1232
+ $ this ->getCell (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row )->setValue ($ value );
1215
1233
1216
1234
return $ this ;
1217
1235
}
@@ -1228,8 +1246,7 @@ public function setCellValueByColumnAndRow($columnIndex, $row, $value)
1228
1246
*/
1229
1247
public function setCellValueExplicit ($ coordinate , $ value , $ dataType )
1230
1248
{
1231
- /** @var string $cellAddress */
1232
- $ cellAddress = $ this ->validateCellAddress ($ coordinate );
1249
+ $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ));
1233
1250
$ this ->getCell ($ cellAddress )->setValueExplicit ($ value , $ dataType );
1234
1251
1235
1252
return $ this ;
@@ -1251,7 +1268,7 @@ public function setCellValueExplicit($coordinate, $value, $dataType)
1251
1268
*/
1252
1269
public function setCellValueExplicitByColumnAndRow ($ columnIndex , $ row , $ value , $ dataType )
1253
1270
{
1254
- $ this ->getCell ([ $ columnIndex, $ row] )->setValueExplicit ($ value , $ dataType );
1271
+ $ this ->getCell (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row )->setValueExplicit ($ value , $ dataType );
1255
1272
1256
1273
return $ this ;
1257
1274
}
@@ -1266,8 +1283,7 @@ public function setCellValueExplicitByColumnAndRow($columnIndex, $row, $value, $
1266
1283
*/
1267
1284
public function getCell ($ coordinate ): Cell
1268
1285
{
1269
- /** @var string $cellAddress */
1270
- $ cellAddress = $ this ->validateCellAddress ($ coordinate );
1286
+ $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ));
1271
1287
1272
1288
// Shortcut for increased performance for the vast majority of simple cases
1273
1289
if ($ this ->cellCollection ->has ($ cellAddress )) {
@@ -1368,7 +1384,7 @@ private function getCellOrNull($coordinate): ?Cell
1368
1384
*/
1369
1385
public function getCellByColumnAndRow ($ columnIndex , $ row ): Cell
1370
1386
{
1371
- return $ this ->getCell ([ $ columnIndex, $ row] );
1387
+ return $ this ->getCell (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row );
1372
1388
}
1373
1389
1374
1390
/**
@@ -1418,7 +1434,6 @@ public function createNewCell($coordinate)
1418
1434
*/
1419
1435
public function cellExists ($ coordinate ): bool
1420
1436
{
1421
- /** @var string $cellAddress */
1422
1437
$ cellAddress = $ this ->validateCellAddress ($ coordinate );
1423
1438
/** @var Worksheet $sheet */
1424
1439
[$ sheet , $ finalCoordinate ] = $ this ->getWorksheetAndCoordinate ($ cellAddress );
@@ -1438,7 +1453,7 @@ public function cellExists($coordinate): bool
1438
1453
*/
1439
1454
public function cellExistsByColumnAndRow ($ columnIndex , $ row ): bool
1440
1455
{
1441
- return $ this ->cellExists ([ $ columnIndex, $ row] );
1456
+ return $ this ->cellExists (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row );
1442
1457
}
1443
1458
1444
1459
/**
@@ -1504,14 +1519,14 @@ public function getStyles()
1504
1519
/**
1505
1520
* Get style for cell.
1506
1521
*
1507
- * @param AddressRange|array<int>|CellAddress|string $cellCoordinate
1522
+ * @param AddressRange|array<int>|CellAddress|int| string $cellCoordinate
1508
1523
* A simple string containing a cell address like 'A1' or a cell range like 'A1:E10'
1509
1524
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
1510
1525
* or a CellAddress or AddressRange object.
1511
1526
*/
1512
1527
public function getStyle ($ cellCoordinate ): Style
1513
1528
{
1514
- $ cellCoordinate = $ this ->validateCellRange ($ cellCoordinate );
1529
+ $ cellCoordinate = $ this ->validateCellOrCellRange ($ cellCoordinate );
1515
1530
1516
1531
// set this sheet as active
1517
1532
$ this ->parent ->setActiveSheetIndex ($ this ->parent ->getIndex ($ this ));
@@ -1749,7 +1764,7 @@ public function duplicateConditionalStyle(array $styles, $range = '')
1749
1764
*/
1750
1765
public function setBreak ($ coordinate , $ break )
1751
1766
{
1752
- $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ) ?? '' );
1767
+ $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ));
1753
1768
1754
1769
if ($ break === self ::BREAK_NONE ) {
1755
1770
if (isset ($ this ->breaks [$ cellAddress ])) {
@@ -1777,7 +1792,7 @@ public function setBreak($coordinate, $break)
1777
1792
*/
1778
1793
public function setBreakByColumnAndRow ($ columnIndex , $ row , $ break )
1779
1794
{
1780
- return $ this ->setBreak ([ $ columnIndex, $ row] , $ break );
1795
+ return $ this ->setBreak (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row , $ break );
1781
1796
}
1782
1797
1783
1798
/**
@@ -1978,7 +1993,7 @@ public function setMergeCells(array $mergeCells)
1978
1993
/**
1979
1994
* Set protection on a cell or cell range.
1980
1995
*
1981
- * @param AddressRange|array<int>|CellAddress|string $range A simple string containing a Cell range like 'A1:E10'
1996
+ * @param AddressRange|array<int>|CellAddress|int| string $range A simple string containing a Cell range like 'A1:E10'
1982
1997
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
1983
1998
* or a CellAddress or AddressRange object.
1984
1999
* @param string $password Password to unlock the protection
@@ -1988,7 +2003,7 @@ public function setMergeCells(array $mergeCells)
1988
2003
*/
1989
2004
public function protectCells ($ range , $ password , $ alreadyHashed = false )
1990
2005
{
1991
- $ range = Functions::trimSheetFromCellReference ($ this ->validateCellRange ($ range ));
2006
+ $ range = Functions::trimSheetFromCellReference ($ this ->validateCellOrCellRange ($ range ));
1992
2007
1993
2008
if (!$ alreadyHashed ) {
1994
2009
$ password = Shared \PasswordHasher::hashPassword ($ password );
@@ -2028,15 +2043,15 @@ public function protectCellsByColumnAndRow($columnIndex1, $row1, $columnIndex2,
2028
2043
/**
2029
2044
* Remove protection on a cell or cell range.
2030
2045
*
2031
- * @param AddressRange|array<int>|CellAddress|string $range A simple string containing a Cell range like 'A1:E10'
2046
+ * @param AddressRange|array<int>|CellAddress|int| string $range A simple string containing a Cell range like 'A1:E10'
2032
2047
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
2033
2048
* or a CellAddress or AddressRange object.
2034
2049
*
2035
2050
* @return $this
2036
2051
*/
2037
2052
public function unprotectCells ($ range )
2038
2053
{
2039
- $ range = Functions::trimSheetFromCellReference ($ this ->validateCellRange ($ range ));
2054
+ $ range = Functions::trimSheetFromCellReference ($ this ->validateCellOrCellRange ($ range ));
2040
2055
2041
2056
if (isset ($ this ->protectedCells [$ range ])) {
2042
2057
unset($ this ->protectedCells [$ range ]);
@@ -2180,11 +2195,15 @@ public function getFreezePane()
2180
2195
*/
2181
2196
public function freezePane ($ coordinate , $ topLeftCell = null )
2182
2197
{
2183
- $ cellAddress = $ this ->validateCellAddress ($ coordinate , true );
2184
- if (is_string ($ cellAddress ) && Coordinate::coordinateIsRange ($ cellAddress )) {
2198
+ $ cellAddress = ($ coordinate !== null )
2199
+ ? Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ coordinate ))
2200
+ : null ;
2201
+ if ($ cellAddress !== null && Coordinate::coordinateIsRange ($ cellAddress )) {
2185
2202
throw new Exception ('Freeze pane can not be set on a range of cells. ' );
2186
2203
}
2187
- $ topLeftCell = $ this ->validateCellAddress ($ topLeftCell , true );
2204
+ $ topLeftCell = ($ topLeftCell !== null )
2205
+ ? Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ topLeftCell ))
2206
+ : null ;
2188
2207
2189
2208
if ($ cellAddress !== null && $ topLeftCell === null ) {
2190
2209
$ coordinate = Coordinate::coordinateFromString ($ cellAddress );
@@ -2218,7 +2237,7 @@ public function setTopLeftCell(string $topLeftCell): self
2218
2237
*/
2219
2238
public function freezePaneByColumnAndRow ($ columnIndex , $ row )
2220
2239
{
2221
- return $ this ->freezePane ([ $ columnIndex, $ row] );
2240
+ return $ this ->freezePane (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row );
2222
2241
}
2223
2242
2224
2243
/**
@@ -2587,8 +2606,7 @@ public function setComments(array $comments)
2587
2606
*/
2588
2607
public function getComment ($ cellCoordinate )
2589
2608
{
2590
- /** @var string $cellAddress */
2591
- $ cellAddress = $ this ->validateCellAddress ($ cellCoordinate );
2609
+ $ cellAddress = Functions::trimSheetFromCellReference ($ this ->validateCellAddress ($ cellCoordinate ));
2592
2610
2593
2611
if (Coordinate::coordinateIsRange ($ cellAddress )) {
2594
2612
throw new Exception ('Cell coordinate string can not be a range of cells. ' );
@@ -2624,7 +2642,7 @@ public function getComment($cellCoordinate)
2624
2642
*/
2625
2643
public function getCommentByColumnAndRow ($ columnIndex , $ row )
2626
2644
{
2627
- return $ this ->getComment ([ $ columnIndex, $ row] );
2645
+ return $ this ->getComment (Coordinate:: stringFromColumnIndex ( $ columnIndex) . $ row );
2628
2646
}
2629
2647
2630
2648
/**
@@ -2675,48 +2693,21 @@ public static function pregReplace(string $pattern, string $replacement, string
2675
2693
return self ::ensureString (preg_replace ($ pattern , $ replacement , $ subject ));
2676
2694
}
2677
2695
2678
- private function tryDefinedName (string $ coordinate ): string
2679
- {
2680
- // Uppercase coordinate
2681
- $ coordinate = strtoupper ($ coordinate );
2682
- // Eliminate leading equal sign
2683
- $ coordinate = self ::pregReplace ('/^=/ ' , '' , $ coordinate );
2684
- $ defined = $ this ->parent ->getDefinedName ($ coordinate , $ this );
2685
- if ($ defined !== null ) {
2686
- if ($ defined ->getWorksheet () === $ this && !$ defined ->isFormula ()) {
2687
- $ coordinate = self ::pregReplace ('/^=/ ' , '' , $ defined ->getValue ());
2688
- }
2689
- }
2690
-
2691
- return $ coordinate ;
2692
- }
2693
-
2694
2696
/**
2695
2697
* Select a range of cells.
2696
2698
*
2697
- * @param string $coordinate Cell range, examples: 'A1', 'B2:G5', 'A:C', '3:6'
2699
+ * @param AddressRange|array<int>|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10'
2700
+ * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
2701
+ * or a CellAddress or AddressRange object.
2698
2702
*
2699
2703
* @return $this
2700
2704
*/
2701
2705
public function setSelectedCells ($ coordinate )
2702
2706
{
2703
- $ originalCoordinate = $ coordinate ;
2704
- $ coordinate = $ this ->tryDefinedName ($ coordinate );
2705
-
2706
- // Convert 'A' to 'A:A'
2707
- $ coordinate = self ::pregReplace ('/^([A-Z]+)$/ ' , '${1}:${1} ' , $ coordinate );
2708
-
2709
- // Convert '1' to '1:1'
2710
- $ coordinate = self ::pregReplace ('/^(\d+)$/ ' , '${1}:${1} ' , $ coordinate );
2711
-
2712
- // Convert 'A:C' to 'A1:C1048576'
2713
- $ coordinate = self ::pregReplace ('/^([A-Z]+):([A-Z]+)$/ ' , '${1}1:${2}1048576 ' , $ coordinate );
2714
-
2715
- // Convert '1:3' to 'A1:XFD3'
2716
- $ coordinate = self ::pregReplace ('/^(\d+):(\d+)$/ ' , 'A${1}:XFD${2} ' , $ coordinate );
2717
- if (preg_match ('/^ \\$?[A-Z]{1,3} \\$?\d{1,7}(: \\$?[A-Z]{1,3} \\$?\d{1,7})?$/ ' , $ coordinate ) !== 1 ) {
2718
- throw new Exception ("Invalid setSelectedCells $ originalCoordinate $ coordinate " );
2707
+ if (is_string ($ coordinate )) {
2708
+ $ coordinate = $ this ->tryDefinedName ($ coordinate );
2719
2709
}
2710
+ $ coordinate = $ this ->validateCellOrCellRange ($ coordinate );
2720
2711
2721
2712
if (Coordinate::coordinateIsRange ($ coordinate )) {
2722
2713
[$ first ] = Coordinate::splitRange ($ coordinate );
0 commit comments