From 98a1f0a8cf9f9ca5e32840718df6bfc1555463fa Mon Sep 17 00:00:00 2001 From: Dominik Businger Date: Wed, 13 Mar 2019 11:32:22 +0100 Subject: [PATCH] Fix for worksheet lookup for worksheets with spaces in the title Sheet titles containing " " or "!" will be quoted in formulas. This commit fixes the lookup of sheets with this kind of title by trimming the quotes during the lookup. Without this any defined range referencing a sheet with " " or "!" in the title name will be lost when reading the workbook from file. Fixes #928 Closes 930 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Spreadsheet.php | 2 +- .../Worksheet/WorksheetTest.php | 1 + tests/data/Calculation/Functions/ISFORMULA.php | 10 ++++++++++ tests/data/Calculation/LookupRef/FORMULATEXT.php | 15 +++++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96574fe7..20819876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix number format masks containing named colours that were being misinterpreted as date formats; and add support for masks that fully replace the value with a full text string [Issue #1009](https://github.com/PHPOffice/PhpSpreadsheet/issues/1009) - Stricter-typed comparison testing in COUNTIF() and COUNTIFS() evaluation [Issue #1046](https://github.com/PHPOffice/PhpSpreadsheet/issues/1046) - COUPNUM should not return zero when settlement is in the last period - [Issue #1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [PR #1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021) +- Fix handling of named ranges referencing sheets with spaces or "!" in their title ## [1.8.2] - 2019-07-08 diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index 8eadcbe8..04a9f1df 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -721,7 +721,7 @@ class Spreadsheet { $worksheetCount = count($this->workSheetCollection); for ($i = 0; $i < $worksheetCount; ++$i) { - if ($this->workSheetCollection[$i]->getTitle() === $pName) { + if ($this->workSheetCollection[$i]->getTitle() === trim($pName, "'")) { return $this->workSheetCollection[$i]; } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php b/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php index 46df9405..ce2df837 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php @@ -137,6 +137,7 @@ class WorksheetTest extends TestCase ['B2', '', '', 'B2'], ['testTitle!B2', 'testTitle', 'B2', 'B2'], ['test!Title!B2', 'test!Title', 'B2', 'B2'], + ['test Title!B2', 'test Title', 'B2', 'B2'], ]; } diff --git a/tests/data/Calculation/Functions/ISFORMULA.php b/tests/data/Calculation/Functions/ISFORMULA.php index 9e0d0d0b..60d9c07f 100644 --- a/tests/data/Calculation/Functions/ISFORMULA.php +++ b/tests/data/Calculation/Functions/ISFORMULA.php @@ -81,4 +81,14 @@ return [ '\'Worksheet1\'!A16', '123', ], + [ + true, + 'A17', + '=\'Work sheet1\'!A1', + ], + [ + true, + 'A18', + '=\'Work!sheet1\'!A1', + ], ]; diff --git a/tests/data/Calculation/LookupRef/FORMULATEXT.php b/tests/data/Calculation/LookupRef/FORMULATEXT.php index 30b2d6eb..48d528bf 100644 --- a/tests/data/Calculation/LookupRef/FORMULATEXT.php +++ b/tests/data/Calculation/LookupRef/FORMULATEXT.php @@ -21,11 +21,26 @@ return [ 'A4', '=\'Worksheet1\'!A1', ], + [ + '=\'Works heet1\'!A1', + 'A4', + '=\'Works heet1\'!A1', + ], [ '="HELLO WORLD"', '\'Worksheet1\'!A5', '="HELLO WORLD"', ], + [ + '="HELLO WORLD"', + '\'Work sheet1\'!A5', + '="HELLO WORLD"', + ], + [ + '="HELLO WORLD"', + '\'Work!sheet1\'!A5', + '="HELLO WORLD"', + ], [ '#N/A', '\'Worksheet1\'!A6',