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

Reference to Defined Name Specifying Worksheet Name #4096

Merged
merged 1 commit into from
Jul 27, 2024
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
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5074,9 +5074,10 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
if ($cell === null || $pCellWorksheet === null) {
return $this->raiseFormulaError("undefined name '$token'");
}
$specifiedWorksheet = trim($matches[2], "'");

$this->debugLog->writeDebugLog('Evaluating Defined Name %s', $definedName);
$namedRange = DefinedName::resolveName($definedName, $pCellWorksheet);
$namedRange = DefinedName::resolveName($definedName, $pCellWorksheet, $specifiedWorksheet);
// If not Defined Name, try as Table.
if ($namedRange === null && $this->spreadsheet !== null) {
$table = $this->spreadsheet->getTableByName($definedName);
Expand All @@ -5101,7 +5102,7 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
return $this->raiseFormulaError("undefined name '$definedName'");
}

$result = $this->evaluateDefinedName($cell, $namedRange, $pCellWorksheet, $stack);
$result = $this->evaluateDefinedName($cell, $namedRange, $pCellWorksheet, $stack, $specifiedWorksheet !== '');
if (isset($storeKey)) {
$branchStore[$storeKey] = $result;
}
Expand Down Expand Up @@ -5580,10 +5581,10 @@ private function addCellReference(array $args, bool $passCellReference, array|st
return $args;
}

private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksheet $cellWorksheet, Stack $stack): mixed
private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksheet $cellWorksheet, Stack $stack, bool $ignoreScope = false): mixed
{
$definedNameScope = $namedRange->getScope();
if ($definedNameScope !== null && $definedNameScope !== $cellWorksheet) {
if ($definedNameScope !== null && $definedNameScope !== $cellWorksheet && !$ignoreScope) {
// The defined name isn't in our current scope, so #REF
$result = ExcelError::REF();
$stack->push('Error', $result, $namedRange->getName());
Expand Down
52 changes: 52 additions & 0 deletions tests/PhpSpreadsheetTests/NamedRange3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests;

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

class NamedRange3Test extends TestCase
{
public function testSheetNamePlusDefinedName(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->setTitle('sheet1');
$sheet1->setCellValue('B1', 100);
$sheet1->setCellValue('B2', 200);
$sheet1->setCellValue('B3', 300);
$sheet1->setCellValue('B4', 400);
$sheet1->setCellValue('B5', 500);

$sheet2 = $spreadsheet->createsheet();
$sheet2->setTitle('sheet2');
$sheet2->setCellValue('A1', 10);
$sheet2->setCellValue('A2', 20);
$sheet2->setCellValue('A3', 30);
$sheet2->setCellValue('A4', 40);
$sheet2->setCellValue('A5', 50);

$spreadsheet->addNamedRange(
new NamedRange('somecells', $sheet2, '$A$1:$A$5', true)
);
$spreadsheet->addNamedRange(
new NamedRange('cellsonsheet1', $sheet1, '$B$1:$B$5')
);

$sheet1->getCell('G1')->setValue('=SUM(cellsonsheet1)');
self::assertSame(1500, $sheet1->getCell('G1')->getCalculatedValue());
$sheet1->getCell('G2')->setValue('=SUM(sheet2!somecells)');
self::assertSame(150, $sheet1->getCell('G2')->getCalculatedValue());
$sheet1->getCell('G3')->setValue('=SUM(somecells)');
self::assertSame('#NAME?', $sheet1->getCell('G3')->getCalculatedValue());
$sheet1->getCell('G4')->setValue('=SUM(sheet2!cellsonsheet1)');
self::assertSame(1500, $sheet1->getCell('G4')->getCalculatedValue());
$sheet1->getCell('G5')->setValue('=SUM(sheet2xxx!cellsonsheet1)');
self::assertSame('#NAME?', $sheet1->getCell('G5')->getCalculatedValue());

$spreadsheet->disconnectWorksheets();
}
}
Loading