Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolution for Issue 3335 - Calculation Engine doesn't evaluate Defined Name when default cell A1 is quote-prefixed #3336

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Nothing
- 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)


## 1.27.0 - 2023-01-24
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,19 @@ Supporters will receive access to articles about working with PhpSpreadsheet, an
Posts already available to Patreon supporters:
- The Dating Game
- A look at how MS Excel (and PhpSpreadsheet) handle date and time values.
- Looping the Loop
- Advice on Iterating through the rows and cells in a worksheet.

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

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.

Planned posts for the future include topics like:
- Tables
- Structured References
- AutoFiltering
- Array Formulae
- Conditional Formatting
- Data Validation
Expand Down
7 changes: 4 additions & 3 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3696,15 +3696,16 @@ public function saveValueToCache($cellReference, $cellValue): void
* @param string $formula The formula to parse and calculate
* @param string $cellID The ID (e.g. A3) of the cell that we are calculating
* @param Cell $cell Cell to calculate
* @param bool $ignoreQuotePrefix If set to true, evaluate the formyla even if the referenced cell is quote prefixed
*
* @return mixed
*/
public function _calculateFormulaValue($formula, $cellID = null, ?Cell $cell = null)
public function _calculateFormulaValue($formula, $cellID = null, ?Cell $cell = null, bool $ignoreQuotePrefix = false)
{
$cellValue = null;

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

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

if ($this->getDebugLog()->getWriteDebugLog()) {
$this->debugLog->mergeDebugLog(array_slice($recursiveCalculator->getDebugLog()->getLog(), 3));
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Calculation/CalculationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function testBranchPruningFormulaParsingInequalitiesConditionsCase(): voi
*
* @dataProvider dataProviderBranchPruningFullExecution
*/
public function testFullExecution(
public function testFullExecutionDataPruning(
$expectedResult,
$dataArray,
$formula,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Calculation;

use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class DefinedNameWithQuotePrefixedCellTest extends TestCase
{
public function testDefinedNameIsAlwaysEvaluated(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->setTitle('Sheet1');
$sheet2 = $spreadsheet->createSheet();
$sheet2->setTitle('Sheet2');
$sheet2->getCell('A1')->setValue('July 2019');
$sheet2->getStyle('A1')
->setQuotePrefix(true);
$sheet2->getCell('A2')->setValue(3);
$spreadsheet->addNamedRange(new NamedRange('FM', $sheet2, '$A$2'));
$sheet1->getCell('A1')->setValue('=(A2+FM)');
$sheet1->getCell('A2')->setValue(38.42);

self::assertSame(41.42, $sheet1->getCell('A1')->getCalculatedValue());
}
}