Skip to content

Commit

Permalink
Merge pull request #1 from kasko/named-range
Browse files Browse the repository at this point in the history
Fix for 3 Issues Involving ReadXlsx and NamedRange (PHPOffice#1742)
  • Loading branch information
sukonovs authored Jan 4, 2023
2 parents a8e8068 + 56385b3 commit 4ddc63d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ public function load($pFilename)
}

// Valid range?
if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
if ($extractedRange == '') {
continue;
}

Expand Down Expand Up @@ -1347,7 +1347,7 @@ public function load($pFilename)
$extractedRange = (string) $definedName;

// Valid range?
if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') {
if ($extractedRange == '') {
continue;
}

Expand Down Expand Up @@ -1395,6 +1395,9 @@ public function load($pFilename)
$locatedSheet = $excel->getSheetByName($extractedSheetName);
}

if ($locatedSheet === null && !DefinedName::testIfFormula($definedRange)) {
$definedRange = '#REF!';
}
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $locatedSheet, $definedRange, false));
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/NamedRangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;

class NamedRangeTest extends TestCase
{
public static function testBug1686b(): void
{
$xlsxFile = 'tests/data/Reader/XLSX/bug1686b.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->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());
}
}
Binary file added tests/data/Reader/XLSX/bug1686b.xlsx
Binary file not shown.

0 comments on commit 4ddc63d

Please sign in to comment.