Skip to content

Commit 02c6e8c

Browse files
author
MarkBaker
committed
Escape double quotes in worksheet names for column range and row range references
1 parent 23e207c commit 02c6e8c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4203,7 +4203,7 @@ private function internalParseFormula($formula, ?Cell $cell = null)
42034203
$expectingOperator = false;
42044204
}
42054205
$stack->push('Brace', '(');
4206-
} elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $val, $matches)) {
4206+
} elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/miu', $val, $matches)) {
42074207
// Watch for this case-change when modifying to allow cell references in different worksheets...
42084208
// Should only be applied to the actual cell column, not the worksheet name
42094209
// If the last entry on the stack was a : operator, then we have a cell range reference
@@ -4326,6 +4326,8 @@ private function internalParseFormula($formula, ?Cell $cell = null)
43264326
$val = $rowRangeReference[1];
43274327
$length = strlen($rowRangeReference[1]);
43284328
$stackItemType = 'Row Reference';
4329+
// unescape any apostrophes or double quotes in worksheet name
4330+
$val = str_replace(["''", '""'], ["'", '"'], $val);
43294331
$column = 'A';
43304332
if (($testPrevOp !== null && $testPrevOp['value'] === ':') && $pCellParent !== null) {
43314333
$column = $pCellParent->getHighestDataColumn($val);
@@ -4338,6 +4340,8 @@ private function internalParseFormula($formula, ?Cell $cell = null)
43384340
$val = $columnRangeReference[1];
43394341
$length = strlen($val);
43404342
$stackItemType = 'Column Reference';
4343+
// unescape any apostrophes or double quotes in worksheet name
4344+
$val = str_replace(["''", '""'], ["'", '"'], $val);
43414345
$row = '1';
43424346
if (($testPrevOp !== null && $testPrevOp['value'] === ':') && $pCellParent !== null) {
43434347
$row = $pCellParent->getHighestDataRow($val);

tests/PhpSpreadsheetTests/Calculation/ParseFormulaTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function providerBinaryOperations(): array
176176
],
177177
'Combined Cell Reference and Column Range with quote' => [
178178
[
179-
['type' => 'Column Reference', 'value' => "'Mark''s sheet1'!A1", 'reference' => "'Mark''s sheet1'!A1"],
180-
['type' => 'Column Reference', 'value' => "'Mark''s sheet1'!A1048576", 'reference' => "'Mark''s sheet1'!A1048576"],
179+
['type' => 'Column Reference', 'value' => "'Mark's sheet1'!A1", 'reference' => "'Mark's sheet1'!A1"],
180+
['type' => 'Column Reference', 'value' => "'Mark's sheet1'!A1048576", 'reference' => "'Mark's sheet1'!A1048576"],
181181
['type' => 'Binary Operator', 'value' => ':', 'reference' => null],
182182
['type' => 'Operand Count for Function MIN()', 'value' => 1, 'reference' => null],
183183
['type' => 'Function', 'value' => 'MIN(', 'reference' => null],
@@ -213,8 +213,8 @@ public function providerBinaryOperations(): array
213213
'Combined Column Range and Cell Reference with quote' => [
214214
[
215215
['type' => 'Cell Reference', 'value' => "'Mark's sheet1'!A1", 'reference' => "'Mark's sheet1'!A1"],
216-
['type' => 'Column Reference', 'value' => "'Mark''s sheet1'!A1", 'reference' => "'Mark''s sheet1'!A1"],
217-
['type' => 'Column Reference', 'value' => "'Mark''s sheet1'!A1048576", 'reference' => "'Mark''s sheet1'!A1048576"],
216+
['type' => 'Column Reference', 'value' => "'Mark's sheet1'!A1", 'reference' => "'Mark's sheet1'!A1"],
217+
['type' => 'Column Reference', 'value' => "'Mark's sheet1'!A1048576", 'reference' => "'Mark's sheet1'!A1048576"],
218218
['type' => 'Binary Operator', 'value' => ':', 'reference' => null],
219219
['type' => 'Operand Count for Function MIN()', 'value' => 1, 'reference' => null],
220220
['type' => 'Function', 'value' => 'MIN(', 'reference' => null],

0 commit comments

Comments
 (0)