Merge pull request #2926 from PHPOffice/Issue-2924_CellReference-for-Row/Column-Validation

Support row/column ranges in rangeBoundaries(), rangeDimension() and getRangeBoundaries() Coordinate methods
This commit is contained in:
Mark Baker 2022-07-08 18:57:23 +02:00 committed by GitHub
commit d230c4240e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 56 deletions

View File

@ -26,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Changed
- Better enforcement of value modification to match specified datatype when using setValueExplicit()
- 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)
- Better enforcement of value modification to match specified datatype when using `setValueExplicit()`
- Relax validation of merge cells to allow merge for a single cell reference [Issue #2776](https://github.com/PHPOffice/PhpSpreadsheet/issues/2776)
- Memory and speed improvements, particularly for the Cell Collection, and the Writers.

View File

@ -183,12 +183,12 @@ abstract class Coordinate
/**
* Calculate range boundaries.
*
* @param string $range Cell range (e.g. A1:A1)
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
*
* @return array Range coordinates [Start Cell, End Cell]
* where Start Cell and End Cell are arrays (Column Number, Row Number)
*/
public static function rangeBoundaries($range)
public static function rangeBoundaries(string $range): array
{
// Ensure $pRange is a valid range
if (empty($range)) {
@ -205,6 +205,16 @@ abstract class Coordinate
[$rangeA, $rangeB] = explode(':', $range);
}
if (is_numeric($rangeA) && is_numeric($rangeB)) {
$rangeA = 'A' . $rangeA;
$rangeB = AddressRange::MAX_COLUMN . $rangeB;
}
if (ctype_alpha($rangeA) && ctype_alpha($rangeB)) {
$rangeA = $rangeA . '1';
$rangeB = $rangeB . AddressRange::MAX_ROW;
}
// Calculate range outer borders
$rangeStart = self::coordinateFromString($rangeA);
$rangeEnd = self::coordinateFromString($rangeB);
@ -219,7 +229,7 @@ abstract class Coordinate
/**
* Calculate range dimension.
*
* @param string $range Cell range (e.g. A1:A1)
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
*
* @return array Range dimension (width, height)
*/
@ -234,29 +244,19 @@ abstract class Coordinate
/**
* Calculate range boundaries.
*
* @param string $range Cell range (e.g. A1:A1)
* @param string $range Cell range, Single Cell, Row/Column Range (e.g. A1:A1, B2, B:C, 2:3)
*
* @return array Range coordinates [Start Cell, End Cell]
* where Start Cell and End Cell are arrays [Column ID, Row Number]
*/
public static function getRangeBoundaries($range)
{
// Ensure $pRange is a valid range
if (empty($range)) {
$range = self::DEFAULT_RANGE;
}
[$rangeA, $rangeB] = self::rangeBoundaries($range);
// Uppercase coordinate
$range = strtoupper($range);
// Extract range
if (strpos($range, ':') === false) {
$rangeA = $rangeB = $range;
} else {
[$rangeA, $rangeB] = explode(':', $range);
}
return [self::coordinateFromString($rangeA), self::coordinateFromString($rangeB)];
return [
[self::stringFromColumnIndex($rangeA[0]), $rangeA[1]],
[self::stringFromColumnIndex($rangeB[0]), $rangeB[1]],
];
}
/**

View File

@ -2,29 +2,45 @@
return [
[
[
[
'B',
4,
],
[
'E',
9,
],
'Cell Range' => [
['B', 4],
['E', 9],
],
'B4:E9',
],
'Single Cell' => [
[
[
[
'B',
4,
],
[
'B',
4,
],
['B', 4],
['B', 4],
],
'B4',
],
'Column Range' => [
[
['B', 1],
['C', 1048576],
],
'B:C',
],
'Single Column Range' => [
[
['B', 1],
['B', 1048576],
],
'B:B',
],
'Row Range' => [
[
['A', 2],
['XFD', 3],
],
'2:3',
],
'Single Row Range' => [
[
['A', 2],
['XFD', 2],
],
'2:2',
],
];

View File

@ -1,30 +1,46 @@
<?php
return [
'Cell Range' => [
[
[
[
2,
4,
],
[
5,
9,
],
[2, 4],
[5, 9],
],
'B4:E9',
],
'Single Cell' => [
[
[
[
2,
4,
],
[
2,
4,
],
[2, 4],
[2, 4],
],
'B4',
],
'Column Range' => [
[
[2, 1],
[3, 1048576],
],
'B:C',
],
'Single Column Range' => [
[
[2, 1],
[2, 1048576],
],
'B:B',
],
'Row Range' => [
[
[1, 2],
[16384, 3],
],
'2:3',
],
'Single Row Range' => [
[
[1, 2],
[16384, 2],
],
'2:2',
],
];