Skip to content

Commit 3f8b39c

Browse files
authored
Merge pull request #3336 from PHPOffice/Issue-3355_Quote-Prefix-in-Worksheet-for-Named-Range-Evaluation
Resolution for Issue 3335 - Calculation Engine doesn't evaluate Defined Name when default cell A1 is quote-prefixed
2 parents 889ad85 + ea3be3b commit 3f8b39c

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2525

2626
### Fixed
2727

28-
- Nothing
28+
- Calculation Engine doesn't evaluate Defined Name when default cell A1 is quote-prefixed [Issue #3335](https://github.com/PHPOffice/PhpSpreadsheet/issues/3335) [PR #3336](https://github.com/PHPOffice/PhpSpreadsheet/pull/3336)
2929

3030

3131
## 1.27.0 - 2023-01-24

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ Supporters will receive access to articles about working with PhpSpreadsheet, an
100100
Posts already available to Patreon supporters:
101101
- The Dating Game
102102
- A look at how MS Excel (and PhpSpreadsheet) handle date and time values.
103+
- Looping the Loop
104+
- Advice on Iterating through the rows and cells in a worksheet.
103105

104106
The next post (currently being written) will be:
105-
- Looping the Loop
106-
- Advice on Iterating through the cells in a worksheet.
107+
- Behind the Mask
108+
- A look at Number Format Masks.
107109

108110
My aim is to post at least one article each month, taking a detailed look at some feature of MS Excel and how to use that feature in PhpSpreadsheet, or on how to perform different activities in PhpSpreadsheet.
109111

110112
Planned posts for the future include topics like:
111113
- Tables
112114
- Structured References
115+
- AutoFiltering
113116
- Array Formulae
114117
- Conditional Formatting
115118
- Data Validation

src/PhpSpreadsheet/Calculation/Calculation.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -3696,15 +3696,16 @@ public function saveValueToCache($cellReference, $cellValue): void
36963696
* @param string $formula The formula to parse and calculate
36973697
* @param string $cellID The ID (e.g. A3) of the cell that we are calculating
36983698
* @param Cell $cell Cell to calculate
3699+
* @param bool $ignoreQuotePrefix If set to true, evaluate the formyla even if the referenced cell is quote prefixed
36993700
*
37003701
* @return mixed
37013702
*/
3702-
public function _calculateFormulaValue($formula, $cellID = null, ?Cell $cell = null)
3703+
public function _calculateFormulaValue($formula, $cellID = null, ?Cell $cell = null, bool $ignoreQuotePrefix = false)
37033704
{
37043705
$cellValue = null;
37053706

37063707
// Quote-Prefixed cell values cannot be formulae, but are treated as strings
3707-
if ($cell !== null && $cell->getStyle()->getQuotePrefix() === true) {
3708+
if ($cell !== null && $ignoreQuotePrefix === false && $cell->getStyle()->getQuotePrefix() === true) {
37083709
return self::wrapResult((string) $formula);
37093710
}
37103711

@@ -5720,7 +5721,7 @@ private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksh
57205721
$recursiveCalculator = new self($this->spreadsheet);
57215722
$recursiveCalculator->getDebugLog()->setWriteDebugLog($this->getDebugLog()->getWriteDebugLog());
57225723
$recursiveCalculator->getDebugLog()->setEchoDebugLog($this->getDebugLog()->getEchoDebugLog());
5723-
$result = $recursiveCalculator->_calculateFormulaValue($definedNameValue, $recursiveCalculationCellAddress, $recursiveCalculationCell);
5724+
$result = $recursiveCalculator->_calculateFormulaValue($definedNameValue, $recursiveCalculationCellAddress, $recursiveCalculationCell, true);
57245725

57255726
if ($this->getDebugLog()->getWriteDebugLog()) {
57265727
$this->debugLog->mergeDebugLog(array_slice($recursiveCalculator->getDebugLog()->getLog(), 3));

tests/PhpSpreadsheetTests/Calculation/CalculationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public function testBranchPruningFormulaParsingInequalitiesConditionsCase(): voi
369369
*
370370
* @dataProvider dataProviderBranchPruningFullExecution
371371
*/
372-
public function testFullExecution(
372+
public function testFullExecutionDataPruning(
373373
$expectedResult,
374374
$dataArray,
375375
$formula,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
4+
5+
use PhpOffice\PhpSpreadsheet\NamedRange;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DefinedNameWithQuotePrefixedCellTest extends TestCase
10+
{
11+
public function testDefinedNameIsAlwaysEvaluated(): void
12+
{
13+
$spreadsheet = new Spreadsheet();
14+
$sheet1 = $spreadsheet->getActiveSheet();
15+
$sheet1->setTitle('Sheet1');
16+
$sheet2 = $spreadsheet->createSheet();
17+
$sheet2->setTitle('Sheet2');
18+
$sheet2->getCell('A1')->setValue('July 2019');
19+
$sheet2->getStyle('A1')
20+
->setQuotePrefix(true);
21+
$sheet2->getCell('A2')->setValue(3);
22+
$spreadsheet->addNamedRange(new NamedRange('FM', $sheet2, '$A$2'));
23+
$sheet1->getCell('A1')->setValue('=(A2+FM)');
24+
$sheet1->getCell('A2')->setValue(38.42);
25+
26+
self::assertSame(41.42, $sheet1->getCell('A1')->getCalculatedValue());
27+
}
28+
}

0 commit comments

Comments
 (0)