From b0eb272ce1d98a454dbbfb665a131a12b7ffefb7 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 19 Feb 2021 12:28:07 +0100 Subject: [PATCH] Bugfix #1858; `getCell()` method should support named cells correctly (#1865) Resolution for Issue [#1858](https://github.com/PHPOffice/PhpSpreadsheet/issues/1858) --- 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 ddfd2fc3..bdeaaa8e 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 19833b71..c6f655c3 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -1186,7 +1186,8 @@ class Worksheet implements IComparable 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 @@ class Worksheet implements IComparable ) { $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 @@ class Worksheet implements IComparable ) { $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 @@ class Worksheet implements IComparable $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); }