diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 9477131fda..df9ec18c93 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -5312,7 +5312,7 @@ private function getTokensAsString($tokens) /** * @return mixed|string */ - private function evaluateDefinedName(Cell $pCell, DefinedName $namedRange, Worksheet $pCellWorksheet, Stack $stack) + public function evaluateDefinedName(Cell $pCell, DefinedName $namedRange, Worksheet $pCellWorksheet, Stack $stack) { $definedNameScope = $namedRange->getScope(); if ($definedNameScope !== null && $definedNameScope !== $pCellWorksheet) { diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php b/src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php index c34dd9651b..6d81700287 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef/Indirect.php @@ -3,8 +3,11 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; +use PhpOffice\PhpSpreadsheet\Calculation\Exception; use PhpOffice\PhpSpreadsheet\Calculation\Functions; +use PhpOffice\PhpSpreadsheet\Calculation\Token\Stack; use PhpOffice\PhpSpreadsheet\Cell\Cell; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class Indirect @@ -34,6 +37,16 @@ public static function INDIRECT($cellAddress = null, ?Cell $pCell = null) return Functions::REF(); } + if ( + (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellAddress, $matches)) && + (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/miu', $cellAddress, $matches)) + ) { + try { + return self::evaluateDefinedName($matches[6], $pCell); + } catch (Exception $e) { + } + } + [$cellAddress, $pSheet] = self::extractWorksheet($cellAddress, $pCell); $cellAddress1 = $cellAddress; @@ -72,4 +85,17 @@ private static function extractWorksheet($cellAddress, Cell $pCell): array return [$cellAddress, $pSheet]; } + + private static function evaluateDefinedName(string $definedName, Cell $pCell) + { + $workSheet = $pCell->getWorksheet(); + $namedRange = DefinedName::resolveName($definedName, $workSheet); + if ($namedRange === null) { + throw new Exception(Functions::REF()); + } + + $calculationEngine = Calculation::getInstance($workSheet->getParent()); + + return $calculationEngine->evaluateDefinedName($pCell, $namedRange, $workSheet, new Stack()); + } }