From cba0e13b2aa549121182a8861200d843cbcbf6af Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sat, 5 Mar 2022 12:31:11 +0100 Subject: [PATCH] Escape double quotes in a string value unless it's an empty string value --- CHANGELOG.md | 2 +- src/PhpSpreadsheet/Calculation/Functions.php | 4 ++++ tests/data/Calculation/Functions/IF_CONDITION.php | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd78be46e3..c1cdb9b6a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Note that this method is used when translating Excel functions between `en_us` and other locale languages, as well as when converting formulae between different spreadsheet formats (e.g. Ods to Excel). Nor is this a perfect solution, as there may still be issues when function calls have array arguments that themselves contain function calls; but it's still better than the current logic. - +- Fix for escaping double quotes within a formula [Issue #1971](https://github.com/PHPOffice/PhpSpreadsheet/issues/1971) [PR #2651](https://github.com/PHPOffice/PhpSpreadsheet/pull/2651) ## 1.22.0 - 2022-02-18 diff --git a/src/PhpSpreadsheet/Calculation/Functions.php b/src/PhpSpreadsheet/Calculation/Functions.php index 340f5cd0eb..00d8a790d4 100644 --- a/src/PhpSpreadsheet/Calculation/Functions.php +++ b/src/PhpSpreadsheet/Calculation/Functions.php @@ -158,6 +158,10 @@ public static function ifCondition($condition) if (is_bool($condition)) { return '=' . ($condition ? 'TRUE' : 'FALSE'); } elseif (!is_numeric($condition)) { + if ($condition !== '""') { // Not an empty string + // Escape any quotes in the string value + $condition = preg_replace('/"/ui', '""', $condition); + } $condition = Calculation::wrapResult(strtoupper($condition)); } diff --git a/tests/data/Calculation/Functions/IF_CONDITION.php b/tests/data/Calculation/Functions/IF_CONDITION.php index 0ac81d5d2e..fce8c6410b 100644 --- a/tests/data/Calculation/Functions/IF_CONDITION.php +++ b/tests/data/Calculation/Functions/IF_CONDITION.php @@ -29,6 +29,18 @@ '<""< PLEASE SELECT >"', '<>< Please Select >',