Skip to content

Commit 56385b3

Browse files
oleibmansukonovs
authored andcommitted
Fix for 3 Issues Involving ReadXlsx and NamedRange (PHPOffice#1742)
* Fix for 3 Issues Involving ReadXlsx and NamedRange Issues PHPOffice#1686 and PHPOffice#1723, which provide sample spreadsheets, are probably solved by this ticket. Issue PHPOffice#1730 is also probably solved, but I have no way to verify. There are two problems with how PhpSpreadsheet is handling things now. Although the first problem is much less severe, and isn't really a factor in the issues named above, it is helpful to get it out of the way first. If you define a named range in Excel, and then delete the sheet where the range exists, Excel saves the range as #REF!. If there is a cell which references the range, it will similarly have the value #REF! when you open the Excel file. Currently, PhpSpreadsheet discards the #REF! definition, so a cell which references the range will appear as #NAME? rather than #REF!. This PR changes the behavior so that PhpSpreadsheet retains the #REF! definition, and cells which reference it will appear as #REF!. The second problem is the more severe, and is, I believe, responsible for the 3 issues identified above. If you define a named range and the sheet on which the range is defined does not exist at the time, Excel will save the range as something like: '[1]Unknown Sheet'!$A$1 If a cell references such a range, Excel will again display #REF!. PhpSpreadsheet currently throws an Exception when it encounters such a definition while reading the file. This PR changes the behavior so that PhpSpreadsheet saves the definition as #REF!, and cells which reference it will behave similarly. For the record, I will note that Excel does not magically recalculate when a missing sheet is subsequently added, despite the fact that the reference might now become resolvable. PhpSpreadsheet behaves likewise. * Remove Dead Code in Test Identified it after push but before merge.
1 parent a8e8068 commit 56385b3

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ public function load($pFilename)
12771277
}
12781278

12791279
// Valid range?
1280-
if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
1280+
if ($extractedRange == '') {
12811281
continue;
12821282
}
12831283

@@ -1347,7 +1347,7 @@ public function load($pFilename)
13471347
$extractedRange = (string) $definedName;
13481348

13491349
// Valid range?
1350-
if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
1350+
if ($extractedRange == '') {
13511351
continue;
13521352
}
13531353

@@ -1395,6 +1395,9 @@ public function load($pFilename)
13951395
$locatedSheet = $excel->getSheetByName($extractedSheetName);
13961396
}
13971397

1398+
if ($locatedSheet === null && !DefinedName::testIfFormula($definedRange)) {
1399+
$definedRange = '#REF!';
1400+
}
13981401
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $locatedSheet, $definedRange, false));
13991402
}
14001403
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class NamedRangeTest extends TestCase
9+
{
10+
public static function testBug1686b(): void
11+
{
12+
$xlsxFile = 'tests/data/Reader/XLSX/bug1686b.xlsx';
13+
$reader = new Xlsx();
14+
$spreadsheet = $reader->load($xlsxFile);
15+
$sheet = $spreadsheet->getActiveSheet();
16+
self::assertEquals(2.1, $sheet->getCell('A1')->getCalculatedValue());
17+
self::assertEquals('#REF!', $sheet->getCell('A2')->getCalculatedValue());
18+
self::assertEquals('#REF!', $sheet->getCell('A3')->getCalculatedValue());
19+
self::assertEquals('#NAME?', $sheet->getCell('A4')->getCalculatedValue());
20+
self::assertEquals('#REF!', $sheet->getCell('A5')->getCalculatedValue());
21+
}
22+
}

tests/data/Reader/XLSX/bug1686b.xlsx

11.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)