Skip to content

Commit

Permalink
Merge pull request #3213 from PHPOffice/Apply-Row-Column-Limits-in-Re…
Browse files Browse the repository at this point in the history
…ference-Helper

Apply Row and Column limits in Reference Helper
  • Loading branch information
MarkBaker authored Nov 26, 2022
2 parents bc01540 + 8a670b0 commit 042bacf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

- Apply Row/Column limits (1048576 and XFD) in ReferenceHelper [PR #3213](https://github.com/PHPOffice/PhpSpreadsheet/pull/3213)
- Allow the creation of In-Memory Drawings from a string of binary image data, or from a stream. [PR #3157](https://github.com/PHPOffice/PhpSpreadsheet/pull/3157)
- Xlsx Reader support for Pivot Tables [PR #2829](https://github.com/PHPOffice/PhpSpreadsheet/pull/2829)
- Permit Date/Time Entered on Spreadsheet to be calculated as Float [Issue #1416](https://github.com/PHPOffice/PhpSpreadsheet/issues/1416) [PR #3121](https://github.com/PHPOffice/PhpSpreadsheet/pull/3121)
Expand All @@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Conditionals formatting rules applied to rows/columns are removed [Issue #3184](https://github.com/PHPOffice/PhpSpreadsheet/issues/3184) [PR #3213](https://github.com/PHPOffice/PhpSpreadsheet/pull/3213)
- Treat strings containing currency or accounting values as floats in Calculation Engine operations [Issue #3165](https://github.com/PHPOffice/PhpSpreadsheet/issues/3165) [PR #3189](https://github.com/PHPOffice/PhpSpreadsheet/pull/3189)
- Treat strings containing percentage values as floats in Calculation Engine operations [Issue #3155](https://github.com/PHPOffice/PhpSpreadsheet/issues/3155) [PR #3156](https://github.com/PHPOffice/PhpSpreadsheet/pull/3156) and [PR #3164](https://github.com/PHPOffice/PhpSpreadsheet/pull/3164)
- Xlsx Reader Accept Palette of Fewer than 64 Colors [Issue #3093](https://github.com/PHPOffice/PhpSpreadsheet/issues/3093) [PR #3096](https://github.com/PHPOffice/PhpSpreadsheet/pull/3096)
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Calculation\Token\Stack;
use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
Expand Down Expand Up @@ -4404,12 +4405,12 @@ private function internalParseFormula($formula, ?Cell $cell = null)
$stackItemType = 'Row Reference';
/** @var int $valx */
$valx = $val;
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataColumn($valx) : 'XFD'; // Max 16,384 columns for Excel2007
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataColumn($valx) : AddressRange::MAX_COLUMN; // Max 16,384 columns for Excel2007
$val = "{$rangeWS2}{$endRowColRef}{$val}";
} elseif (ctype_alpha($val) && strlen($val) <= 3) {
// Column range
$stackItemType = 'Column Reference';
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataRow($val) : 1048576; // Max 1,048,576 rows for Excel2007
$endRowColRef = ($refSheet !== null) ? $refSheet->getHighestDataRow($val) : AddressRange::MAX_ROW; // Max 1,048,576 rows for Excel2007
$val = "{$rangeWS2}{$val}{$endRowColRef}";
}
$stackItemReference = $val;
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
Expand Down Expand Up @@ -114,12 +115,12 @@ private static function handleRowColumnRanges(?Worksheet $worksheet, string $sta
// Being lazy, we're only checking a single row/column to get the max
if (ctype_digit($start) && $start <= 1048576) {
// Max 16,384 columns for Excel2007
$endColRef = ($worksheet !== null) ? $worksheet->getHighestDataColumn((int) $start) : 'XFD';
$endColRef = ($worksheet !== null) ? $worksheet->getHighestDataColumn((int) $start) : AddressRange::MAX_COLUMN;

return "A{$start}:{$endColRef}{$end}";
} elseif (ctype_alpha($start) && strlen($start) <= 3) {
// Max 1,048,576 rows for Excel2007
$endRowRef = ($worksheet !== null) ? $worksheet->getHighestDataRow($start) : 1048576;
$endRowRef = ($worksheet !== null) ? $worksheet->getHighestDataRow($start) : AddressRange::MAX_ROW;

return "{$start}1:{$end}{$endRowRef}";
}
Expand Down
25 changes: 19 additions & 6 deletions src/PhpSpreadsheet/CellReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet;

use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

class CellReferenceHelper
Expand Down Expand Up @@ -79,16 +80,12 @@ public function updateCellReference(string $cellReference = 'A1', bool $includeA

// Create new column reference
if ($updateColumn) {
$newColumn = ($includeAbsoluteReferences === false)
? Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns)
: $absoluteColumn . Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns);
$newColumn = $this->updateColumnReference($newColumnIndex, $absoluteColumn);
}

// Create new row reference
if ($updateRow) {
$newRow = ($includeAbsoluteReferences === false)
? $newRowIndex + $this->numberOfRows
: $absoluteRow . (string) ($newRowIndex + $this->numberOfRows);
$newRow = $this->updateRowReference($newRowIndex, $absoluteRow);
}

// Return new reference
Expand Down Expand Up @@ -116,4 +113,20 @@ public function cellAddressInDeleteRange(string $cellAddress): bool

return false;
}

protected function updateColumnReference(int $newColumnIndex, string $absoluteColumn): string
{
$newColumn = Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns);
$newColumn = ($newColumn > AddressRange::MAX_COLUMN) ? AddressRange::MAX_COLUMN : $newColumn;

return $absoluteColumn . $newColumn;
}

protected function updateRowReference(int $newRowIndex, string $absoluteRow): string
{
$newRow = $newRowIndex + $this->numberOfRows;
$newRow = ($newRow > AddressRange::MAX_ROW) ? AddressRange::MAX_ROW : $newRow;

return $absoluteRow . (string) $newRow;
}
}
8 changes: 8 additions & 0 deletions tests/PhpSpreadsheetTests/CellReferenceHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function cellReferenceHelperInsertColumnsProvider(): array
['A1', 'A1'],
['D5', 'D5'],
['G5', 'E5'],
['XFC5', 'XFA5'],
['XFD5', 'XFB5'],
['XFD5', 'XFC5'],
['XFD5', 'XFD5'],
['$E5', '$E5'],
['G$5', 'E$5'],
['I5', 'G5'],
Expand Down Expand Up @@ -69,6 +73,10 @@ public function cellReferenceHelperInsertRowsProvider(): array
['A1', 'A1'],
['E4', 'E4'],
['E7', 'E5'],
['E1048575', 'E1048573'],
['E1048576', 'E1048574'],
['E1048576', 'E1048575'],
['E1048576', 'E1048576'],
['E$5', 'E$5'],
['$E7', '$E5'],
['E11', 'E9'],
Expand Down

0 comments on commit 042bacf

Please sign in to comment.