From 3cf2d9f14d5dfca9935a4c25be984c8782b481c7 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 19 Feb 2021 12:13:54 +0100 Subject: [PATCH] Bugfix #1858; `getCell()` method should support named cells correctly --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Worksheet/Worksheet.php | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfd2fc3ac..bdeaaa8ecd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Nothing. ### Fixed +- Fixed issue with Worksheet's `getCell()` method when trying to get a cell by defined name. [#1858](https://github.com/PHPOffice/PhpSpreadsheet/issues/1858) - Fix possible endless loop in NumberFormat Masks [#1792](https://github.com/PHPOffice/PhpSpreadsheet/issues/1792) - Fix problem resulting from literal dot inside quotes in number format masks. [PR #1830](https://github.com/PHPOffice/PhpSpreadsheet/pull/1830) - Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx. [PR #1761](https://github.com/PHPOffice/PhpSpreadsheet/pull/1761) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 19833b71e5..c6f655c3be 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -1186,7 +1186,8 @@ public function getCell($pCoordinate, $createIfNotExists = true) if (strpos($pCoordinate, '!') !== false) { $worksheetReference = self::extractSheetTitle($pCoordinate, true); - return $this->parent->getSheetByName($worksheetReference[0])->getCell(strtoupper($worksheetReference[1]), $createIfNotExists); + return $this->parent->getSheetByName($worksheetReference[0]) + ->getCell(strtoupper($worksheetReference[1]), $createIfNotExists); } // Named range? @@ -1196,7 +1197,7 @@ public function getCell($pCoordinate, $createIfNotExists = true) ) { $namedRange = DefinedName::resolveName($pCoordinate, $this); if ($namedRange !== null) { - $pCoordinate = $namedRange->getValue(); + $pCoordinate = str_replace('$', '', $namedRange->getValue()); return $namedRange->getWorksheet()->getCell($pCoordinate, $createIfNotExists); } @@ -1296,7 +1297,7 @@ public function cellExists($pCoordinate) ) { $namedRange = DefinedName::resolveName($pCoordinate, $this); if ($namedRange !== null) { - $pCoordinate = $namedRange->getValue(); + $pCoordinate = str_replace('$', '', $namedRange->getValue()); if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) { if (!$namedRange->getLocalOnly()) { return $namedRange->getWorksheet()->cellExists($pCoordinate); @@ -2567,7 +2568,7 @@ public function namedRangeToArray($pNamedRange, $nullValue = null, $calculateFor $namedRange = DefinedName::resolveName($pNamedRange, $this); if ($namedRange !== null) { $pWorkSheet = $namedRange->getWorksheet(); - $pCellRange = $namedRange->getValue(); + $pCellRange = str_replace('$', '', $namedRange->getValue()); return $pWorkSheet->rangeToArray($pCellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef); }