Skip to content

Commit adf4531

Browse files
author
MarkBaker
committed
Adjust worksheet regexps for CellRef and Row/Column Ranges for improved handling of quoted worksheet names
1 parent 5608e05 commit adf4531

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ class Calculation
3333
// Function (allow for the old @ symbol that could be used to prefix a function, but we'll ignore it)
3434
const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?([\p{L}][\p{L}\p{N}\.]*)[\s]*\(';
3535
// Cell reference (cell or range of cells, with or without a sheet reference)
36-
const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=:`-]*)|(\'.*?\')|(\".*?\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])';
36+
const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'\')+?\')|(\"(?:[^\"]|\"\")+?\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])';
3737
// Cell reference (with or without a sheet reference) ensuring absolute/relative
38-
const CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'.*?\')|(\".*?\"))!)?(\$?\b[a-z]{1,3})(\$?\d{1,7})(?![\w.])';
39-
const CALCULATION_REGEXP_COLUMN_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'.*?\')|(\".*?\"))!)?(\$?[a-z]{1,3})):(?![.*])';
40-
const CALCULATION_REGEXP_ROW_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'.*?\')|(\".*?\"))!)?(\$?[1-9][0-9]{0,6})):(?![.*])';
38+
const CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'\')+?\')|(\"(?:[^\"]|\"\")+?\"))!)?(\$?\b[a-z]{1,3})(\$?\d{1,7})(?![\w.])';
39+
const CALCULATION_REGEXP_COLUMN_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'\')+?\')|(\".(?:[^\"]|\"\")?\"))!)?(\$?[a-z]{1,3})):(?![.*])';
40+
const CALCULATION_REGEXP_ROW_RANGE = '(((([^\s\(,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'\')+?\')|(\"(?:[^\"]|\"\")+?\"))!)?(\$?[1-9][0-9]{0,6})):(?![.*])';
4141
// Cell reference (with or without a sheet reference) ensuring absolute/relative
4242
// Cell ranges ensuring absolute/relative
4343
const CALCULATION_REGEXP_COLUMNRANGE_RELATIVE = '(\$?[a-z]{1,3}):(\$?[a-z]{1,3})';
4444
const CALCULATION_REGEXP_ROWRANGE_RELATIVE = '(\$?\d{1,7}):(\$?\d{1,7})';
4545
// Defined Names: Named Range of cells, or Named Formulae
46-
const CALCULATION_REGEXP_DEFINEDNAME = '((([^\s,!&%^\/\*\+<>=-]*)|(\'.*?\')|(\".*?\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)';
46+
const CALCULATION_REGEXP_DEFINEDNAME = '((([^\s,!&%^\/\*\+<>=-]*)|(\'(?:[^\']|\'\')+?\')|(\"(?:[^\"]|\"\")+?\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)';
4747
// Error
4848
const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?';
4949

tests/PhpSpreadsheetTests/Calculation/ParseFormulaTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ public function providerBinaryOperations(): array
162162
],
163163
'=B:C',
164164
],
165+
'Combined Cell Reference and Column Range' => [
166+
[
167+
['type' => 'Column Reference', 'value' => "'sheet1'!A1", 'reference' => "'sheet1'!A1"],
168+
['type' => 'Column Reference', 'value' => "'sheet1'!A1048576", 'reference' => "'sheet1'!A1048576"],
169+
['type' => 'Binary Operator', 'value' => ':', 'reference' => null],
170+
['type' => 'Operand Count for Function MIN()', 'value' => 1, 'reference' => null],
171+
['type' => 'Function', 'value' => 'MIN(', 'reference' => null],
172+
['type' => 'Cell Reference', 'value' => "'sheet1'!A1", 'reference' => "'sheet1'!A1"],
173+
['type' => 'Binary Operator', 'value' => '+','reference' => null],
174+
],
175+
"=MIN('sheet1'!A:A) + 'sheet1'!A1",
176+
],
177+
'Combined Column Range and Cell Reference' => [
178+
[
179+
['type' => 'Cell Reference', 'value' => "'sheet1'!A1", 'reference' => "'sheet1'!A1"],
180+
['type' => 'Column Reference', 'value' => "'sheet1'!A1", 'reference' => "'sheet1'!A1"],
181+
['type' => 'Column Reference', 'value' => "'sheet1'!A1048576", 'reference' => "'sheet1'!A1048576"],
182+
['type' => 'Binary Operator', 'value' => ':', 'reference' => null],
183+
['type' => 'Operand Count for Function MIN()', 'value' => 1, 'reference' => null],
184+
['type' => 'Function', 'value' => 'MIN(', 'reference' => null],
185+
['type' => 'Binary Operator', 'value' => '+','reference' => null],
186+
],
187+
"='sheet1'!A1 + MIN('sheet1'!A:A)",
188+
],
165189
'Range with Defined Names' => [
166190
[
167191
['type' => 'Defined Name', 'value' => 'GROUP1', 'reference' => 'GROUP1'],

0 commit comments

Comments
 (0)