Skip to content

R1C1 conversion should handle absolute A1 references #2060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Nothing.

### Fixed
- Correctly handle absolute A1 references when converting to R1C1 format [PR #2060](https://github.com/PHPOffice/PhpSpreadsheet/pull/2060)
- Correct default fill style for conditional without a pattern defined [Issue #2035](https://github.com/PHPOffice/PhpSpreadsheet/issues/2035) [PR #2050](https://github.com/PHPOffice/PhpSpreadsheet/pull/2050)
- Fixed issue where array key check for existince before accessing arrays in Xlsx.php. [PR #1970](https://github.com/PHPOffice/PhpSpreadsheet/pull/1970)
- Fixed issue with quoted strings in number format mask rendered with toFormattedString() [Issue 1972#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1972) [PR #1978](https://github.com/PHPOffice/PhpSpreadsheet/pull/1978)
Expand Down
15 changes: 12 additions & 3 deletions src/PhpSpreadsheet/Cell/AddressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ public static function convertToR1C1(
?int $currentRowNumber = null,
?int $currentColumnNumber = null
): string {
$validityCheck = preg_match('/^\$?([A-Z]{1,3})\$?(\d{1,7})$/i', $address, $cellReference);
$validityCheck = preg_match(Coordinate::A1_COORDINATE_REGEX, $address, $cellReference);

if ($validityCheck === 0) {
throw new Exception('Invalid A1-format Cell Reference');
}

$columnId = Coordinate::columnIndexFromString($cellReference[1]);
$rowId = (int) $cellReference[2];
$columnId = Coordinate::columnIndexFromString($cellReference['col_ref']);
if ($cellReference['absolute_col'] === '$') {
// Column must be absolute address
$currentColumnNumber = null;
}

$rowId = (int) $cellReference['row_ref'];
if ($cellReference['absolute_row'] === '$') {
// Row must be absolute address
$currentRowNumber = null;
}

if ($currentRowNumber !== null) {
if ($rowId === $currentRowNumber) {
Expand Down
6 changes: 4 additions & 2 deletions src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
abstract class Coordinate
{
public const A1_COORDINATE_REGEX = '/^(?<absolute_col>\$?)(?<col_ref>[A-Z]{1,3})(?<absolute_row>\$?)(?<row_ref>\d{1,7})$/i';

/**
* Default range variable constant.
*
Expand All @@ -29,8 +31,8 @@ abstract class Coordinate
*/
public static function coordinateFromString($pCoordinateString)
{
if (preg_match('/^([$]?[A-Z]{1,3})([$]?\\d{1,7})$/', $pCoordinateString, $matches)) {
return [$matches[1], $matches[2]];
if (preg_match(self::A1_COORDINATE_REGEX, $pCoordinateString, $matches)) {
return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']];
} elseif (self::coordinateIsRange($pCoordinateString)) {
throw new Exception('Cell coordinate string can not be a range of cells');
} elseif ($pCoordinateString == '') {
Expand Down
15 changes: 15 additions & 0 deletions tests/data/Cell/A1ConversionToR1C1Relative.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@

return [
['R[2]C[2]', 'O18', 16, 13],
['R18C15', '$O$18', 16, 13],
['R[-2]C[2]', 'O14', 16, 13],
['R[-2]C15', '$O14', 16, 13],
['R[2]C[-2]', 'K18', 16, 13],
['R18C[-2]', 'K$18', 16, 13],
['R[-2]C[-2]', 'K14', 16, 13],
['RC[3]', 'P16', 16, 13],
['R16C[3]', 'P$16', 16, 13],
['RC[-3]', 'J16', 16, 13],
['RC10', '$J16', 16, 13],
['R[4]C', 'M20', 16, 13],
['R[4]C13', '$M20', 16, 13],
['R[-4]C', 'M12', 16, 13],
['R12C', 'M$12', 16, 13],
['RC', 'E5', 5, 5],
['R5C5', '$E$5', 5, 5],
['R5C', 'E5', null, 5],
['R5C5', '$E5', null, 5],
['R5C', 'E$5', null, 5],
['RC5', 'E5', 5, null],
['RC5', '$E5', 5, null],
['R5C5', 'E$5', 5, null],
['R5C[2]', 'E5', null, 3],
['R5C5', '$E5', null, 3],
['R5C[2]', 'E$5', null, 3],
['R[2]C5', 'E5', 3, null],
['R5C5', '$E$5', 3, null],
['R5C[-2]', 'E5', null, 7],
['R[-2]C5', 'E5', 7, null],
];