diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 3a75edf6..5dc48ff8 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -1280,7 +1280,7 @@ class Xlsx extends BaseReader } // Valid range? - if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') { + if ($extractedRange == '') { continue; } @@ -1350,7 +1350,7 @@ class Xlsx extends BaseReader $extractedRange = (string) $definedName; // Valid range? - if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') { + if ($extractedRange == '') { continue; } @@ -1398,6 +1398,9 @@ class Xlsx extends BaseReader $locatedSheet = $excel->getSheetByName($extractedSheetName); } + if ($locatedSheet === null && !DefinedName::testIfFormula($definedRange)) { + $definedRange = '#REF!'; + } $excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $locatedSheet, $definedRange, false)); } } diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/NamedRangeTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/NamedRangeTest.php new file mode 100644 index 00000000..e4090d98 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/NamedRangeTest.php @@ -0,0 +1,22 @@ +load($xlsxFile); + $sheet = $spreadsheet->getActiveSheet(); + self::assertEquals(2.1, $sheet->getCell('A1')->getCalculatedValue()); + self::assertEquals('#REF!', $sheet->getCell('A2')->getCalculatedValue()); + self::assertEquals('#REF!', $sheet->getCell('A3')->getCalculatedValue()); + self::assertEquals('#NAME?', $sheet->getCell('A4')->getCalculatedValue()); + self::assertEquals('#REF!', $sheet->getCell('A5')->getCalculatedValue()); + } +} diff --git a/tests/data/Reader/XLSX/bug1686b.xlsx b/tests/data/Reader/XLSX/bug1686b.xlsx new file mode 100644 index 00000000..d496c5c7 Binary files /dev/null and b/tests/data/Reader/XLSX/bug1686b.xlsx differ