Skip to content

Commit

Permalink
Ranges across Z and AA columns incorrectly threw an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi authored and billblume committed Jun 11, 2018
1 parent 556390a commit c75adae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- Ranges across Z and AA columns incorrectly threw an exception - [#545](https://github.com/PHPOffice/PhpSpreadsheet/issues/545)

## [1.3.0] - 2018-06-10

### Added
Expand Down
44 changes: 15 additions & 29 deletions src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static function extractAllCellReferencesInRange($pRange)
// Explode spaces
$cellBlocks = self::getCellBlocksFromRangeString($pRange);
foreach ($cellBlocks as $cellBlock) {
$returnValue = array_merge($returnValue, static::getReferencesForCellBlock($cellBlock));
$returnValue = array_merge($returnValue, self::getReferencesForCellBlock($cellBlock));
}

// Sort the result by column and row
Expand All @@ -360,8 +360,6 @@ public static function extractAllCellReferencesInRange($pRange)
*
* @param string $cellBlock A cell range e.g. A4:B5
*
* @throws Exception
*
* @return array All individual cells in that range
*/
private static function getReferencesForCellBlock($cellBlock)
Expand All @@ -385,42 +383,32 @@ private static function getReferencesForCellBlock($cellBlock)

// Range...
list($rangeStart, $rangeEnd) = $range;
list($startCol, $startRow) = static::extractColumnAndRow($rangeStart);
list($endCol, $endRow) = static::extractColumnAndRow($rangeEnd);
++$endCol;
list($startColumn, $startRow) = self::coordinateFromString($rangeStart);
list($endColumn, $endRow) = self::coordinateFromString($rangeEnd);
$startColumnIndex = self::columnIndexFromString($startColumn);
$endColumnIndex = self::columnIndexFromString($endColumn);
++$endColumnIndex;

// Current data
$currentCol = $startCol;
$currentColumnIndex = $startColumnIndex;
$currentRow = $startRow;

static::validateRange($cellBlock, $startCol, $endCol, $currentRow, $endRow);
self::validateRange($cellBlock, $startColumnIndex, $endColumnIndex, $currentRow, $endRow);

// Loop cells
while ($currentCol < $endCol) {
while ($currentColumnIndex < $endColumnIndex) {
while ($currentRow <= $endRow) {
$returnValue[] = $currentCol . $currentRow;
$returnValue[] = self::stringFromColumnIndex($currentColumnIndex) . $currentRow;
++$currentRow;
}
++$currentCol;
++$currentColumnIndex;
$currentRow = $startRow;
}
}

return $returnValue;
}

/**
* Extract the column and row from a cell reference in the format [$column, $row].
*
* @param string $cell
*
* @return array
*/
private static function extractColumnAndRow($cell)
{
return sscanf($cell, '%[A-Z]%d');
}

/**
* Convert an associative array of single cell coordinates to values to an associative array
* of cell ranges to values. Only adjacent cell coordinates with the same
Expand Down Expand Up @@ -524,16 +512,14 @@ private static function getCellBlocksFromRangeString($pRange)
* row.
*
* @param string $cellBlock The original range, for displaying a meaningful error message
* @param string $startCol
* @param string $endCol
* @param int $startColumnIndex
* @param int $endColumnIndex
* @param int $currentRow
* @param int $endRow
*
* @throws Exception
*/
private static function validateRange($cellBlock, $startCol, $endCol, $currentRow, $endRow)
private static function validateRange($cellBlock, $startColumnIndex, $endColumnIndex, $currentRow, $endRow)
{
if ($startCol >= $endCol || $currentRow > $endRow) {
if ($startColumnIndex >= $endColumnIndex || $currentRow > $endRow) {
throw new Exception('Invalid range: "' . $cellBlock . '"');
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/data/CellExtractAllCellReferencesInRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,13 @@
],
'B4:B6 B8',
],
[
[
'Z2',
'Z3',
'AA2',
'AA3',
],
'Z2:AA3',
],
];

0 comments on commit c75adae

Please sign in to comment.