Skip to content

Commit d230c42

Browse files
authored
Merge pull request #2926 from PHPOffice/Issue-2924_CellReference-for-Row/Column-Validation
Support row/column ranges in rangeBoundaries(), rangeDimension() and getRangeBoundaries() Coordinate methods
2 parents 09406a6 + 39e5f12 commit d230c42

File tree

4 files changed

+89
-56
lines changed

4 files changed

+89
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2626

2727
### Changed
2828

29-
- Better enforcement of value modification to match specified datatype when using setValueExplicit()
29+
- Modify `rangeBoundaries()`, `rangeDimension()` and `getRangeBoundaries()` Coordinate methods to work with row/column ranges as well as with cell ranges and cells [PR #2926](https://github.com/PHPOffice/PhpSpreadsheet/pull/2926)
30+
- Better enforcement of value modification to match specified datatype when using `setValueExplicit()`
3031
- Relax validation of merge cells to allow merge for a single cell reference [Issue #2776](https://github.com/PHPOffice/PhpSpreadsheet/issues/2776)
3132
- Memory and speed improvements, particularly for the Cell Collection, and the Writers.
3233

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ public static function buildRange(array $range)
183183
/**
184184
* Calculate range boundaries.
185185
*
186-
* @param string $range Cell range (e.g. A1:A1)
186+
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
187187
*
188188
* @return array Range coordinates [Start Cell, End Cell]
189189
* where Start Cell and End Cell are arrays (Column Number, Row Number)
190190
*/
191-
public static function rangeBoundaries($range)
191+
public static function rangeBoundaries(string $range): array
192192
{
193193
// Ensure $pRange is a valid range
194194
if (empty($range)) {
@@ -205,6 +205,16 @@ public static function rangeBoundaries($range)
205205
[$rangeA, $rangeB] = explode(':', $range);
206206
}
207207

208+
if (is_numeric($rangeA) && is_numeric($rangeB)) {
209+
$rangeA = 'A' . $rangeA;
210+
$rangeB = AddressRange::MAX_COLUMN . $rangeB;
211+
}
212+
213+
if (ctype_alpha($rangeA) && ctype_alpha($rangeB)) {
214+
$rangeA = $rangeA . '1';
215+
$rangeB = $rangeB . AddressRange::MAX_ROW;
216+
}
217+
208218
// Calculate range outer borders
209219
$rangeStart = self::coordinateFromString($rangeA);
210220
$rangeEnd = self::coordinateFromString($rangeB);
@@ -219,7 +229,7 @@ public static function rangeBoundaries($range)
219229
/**
220230
* Calculate range dimension.
221231
*
222-
* @param string $range Cell range (e.g. A1:A1)
232+
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
223233
*
224234
* @return array Range dimension (width, height)
225235
*/
@@ -234,29 +244,19 @@ public static function rangeDimension($range)
234244
/**
235245
* Calculate range boundaries.
236246
*
237-
* @param string $range Cell range (e.g. A1:A1)
247+
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
238248
*
239249
* @return array Range coordinates [Start Cell, End Cell]
240250
* where Start Cell and End Cell are arrays [Column ID, Row Number]
241251
*/
242252
public static function getRangeBoundaries($range)
243253
{
244-
// Ensure $pRange is a valid range
245-
if (empty($range)) {
246-
$range = self::DEFAULT_RANGE;
247-
}
248-
249-
// Uppercase coordinate
250-
$range = strtoupper($range);
251-
252-
// Extract range
253-
if (strpos($range, ':') === false) {
254-
$rangeA = $rangeB = $range;
255-
} else {
256-
[$rangeA, $rangeB] = explode(':', $range);
257-
}
254+
[$rangeA, $rangeB] = self::rangeBoundaries($range);
258255

259-
return [self::coordinateFromString($rangeA), self::coordinateFromString($rangeB)];
256+
return [
257+
[self::stringFromColumnIndex($rangeA[0]), $rangeA[1]],
258+
[self::stringFromColumnIndex($rangeB[0]), $rangeB[1]],
259+
];
260260
}
261261

262262
/**

tests/data/CellGetRangeBoundaries.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,45 @@
22

33
return [
44
[
5-
[
6-
[
7-
'B',
8-
4,
9-
],
10-
[
11-
'E',
12-
9,
13-
],
5+
'Cell Range' => [
6+
['B', 4],
7+
['E', 9],
148
],
159
'B4:E9',
1610
],
17-
[
11+
'Single Cell' => [
1812
[
19-
[
20-
'B',
21-
4,
22-
],
23-
[
24-
'B',
25-
4,
26-
],
13+
['B', 4],
14+
['B', 4],
2715
],
2816
'B4',
2917
],
18+
'Column Range' => [
19+
[
20+
['B', 1],
21+
['C', 1048576],
22+
],
23+
'B:C',
24+
],
25+
'Single Column Range' => [
26+
[
27+
['B', 1],
28+
['B', 1048576],
29+
],
30+
'B:B',
31+
],
32+
'Row Range' => [
33+
[
34+
['A', 2],
35+
['XFD', 3],
36+
],
37+
'2:3',
38+
],
39+
'Single Row Range' => [
40+
[
41+
['A', 2],
42+
['XFD', 2],
43+
],
44+
'2:2',
45+
],
3046
];

tests/data/CellRangeBoundaries.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
<?php
22

33
return [
4-
[
4+
'Cell Range' => [
55
[
6-
[
7-
2,
8-
4,
9-
],
10-
[
11-
5,
12-
9,
13-
],
6+
[2, 4],
7+
[5, 9],
148
],
159
'B4:E9',
1610
],
17-
[
11+
'Single Cell' => [
1812
[
19-
[
20-
2,
21-
4,
22-
],
23-
[
24-
2,
25-
4,
26-
],
13+
[2, 4],
14+
[2, 4],
2715
],
2816
'B4',
2917
],
18+
'Column Range' => [
19+
[
20+
[2, 1],
21+
[3, 1048576],
22+
],
23+
'B:C',
24+
],
25+
'Single Column Range' => [
26+
[
27+
[2, 1],
28+
[2, 1048576],
29+
],
30+
'B:B',
31+
],
32+
'Row Range' => [
33+
[
34+
[1, 2],
35+
[16384, 3],
36+
],
37+
'2:3',
38+
],
39+
'Single Row Range' => [
40+
[
41+
[1, 2],
42+
[16384, 2],
43+
],
44+
'2:2',
45+
],
3046
];

0 commit comments

Comments
 (0)