Skip to content

Commit cba0e13

Browse files
author
MarkBaker
committed
Escape double quotes in a string value unless it's an empty string value
1 parent 51453db commit cba0e13

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4343
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).
4444

4545
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.
46-
46+
- 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)
4747

4848
## 1.22.0 - 2022-02-18
4949

src/PhpSpreadsheet/Calculation/Functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public static function ifCondition($condition)
158158
if (is_bool($condition)) {
159159
return '=' . ($condition ? 'TRUE' : 'FALSE');
160160
} elseif (!is_numeric($condition)) {
161+
if ($condition !== '""') { // Not an empty string
162+
// Escape any quotes in the string value
163+
$condition = preg_replace('/"/ui', '""', $condition);
164+
}
161165
$condition = Calculation::wrapResult(strtoupper($condition));
162166
}
163167

tests/data/Calculation/Functions/IF_CONDITION.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
'<"<A"',
3030
'<<A',
3131
],
32+
[
33+
'="A"',
34+
'=A',
35+
],
36+
[
37+
'="""A"""',
38+
'="A"',
39+
],
40+
[
41+
'="""A""B"""',
42+
'="A"B"',
43+
],
3244
[
3345
'<>"< PLEASE SELECT >"',
3446
'<>< Please Select >',

0 commit comments

Comments
 (0)