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

XLSX reader - Exception : You must specify a worksheet or a scope for a Named Range #1686

Closed
vpratfr opened this issue Oct 21, 2020 · 5 comments

Comments

@vpratfr
Copy link

vpratfr commented Oct 21, 2020

This is a bug report

What is the expected behavior?

Read a simple XLSX file (no formula inside) without exception

What is the current behavior?

The XLSX reader crashes with the message

PhpOffice\PhpSpreadsheet\Exception : You must specify a worksheet or a scope for a Named Range
 /home/vagrant/code/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/NamedRange.php:21
 /home/vagrant/code/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/DefinedName.php:97
 /home/vagrant/code/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php:1398

What are the steps to reproduce?

// This works properly, file is open
$reader = new Xls();
$reader->load(storage_path('default.xls'));

// This fails to open with the above exception
$reader = new Xlsx();
$reader->load(storage_path('default.xlsx'));

test.zip

The above zip file contains the XLS and XLSX files that exhibit the behaviour. The XLS file is simply the XLSX file saved as XLS (no change in the spreadsheet itself).

Which versions of PhpSpreadsheet and PHP are affected?

Package 1.15.0
PHP 7.4

@ghost
Copy link

ghost commented Oct 22, 2020

I have this problem... Solutions?? Return to PHPExcel-1.8 ?

$archivo = $_FILES["archivoCoe"]["tmp_name"];
$spreadsheet = IOFactory::load($archivo);
$codigo = $spreadsheet->getSheet(0)->getCell('A8')->getValue();

pittyplatsch pushed a commit to pittyplatsch/PhpSpreadsheet that referenced this issue Oct 24, 2020
pittyplatsch pushed a commit to pittyplatsch/PhpSpreadsheet that referenced this issue Oct 24, 2020
@jonothedev
Copy link

Is it possible you could tag this in a release so it fixes Laravel Excel package as they have explicitly included this package as "phpoffice/phpspreadsheet": "^1.14", and tagging would bump this fix.

@kevsharp81
Copy link

If you feel comfortable go into NamedRange.php in the source code and comment lines 20,21,22 out. Save the file and this should then work. I was having the same issue some where its not setting $worksheet or $scope variables. There should be logic in this code file that doesn't default them to null but instead sets them to something else a default value.

@alexxxxkkkk
Copy link

I have the same issue but only when filling the loadSheetsOnly array.

Example:

<?php

use PhpOffice\PhpSpreadsheet\IOFactory;

$reader = IOFactory::createReaderForFile('example.xlsx');

$reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 2']);

$spreadsheet = $reader->load('example.xlsx');

It throws the error: "PhpOffice\PhpSpreadsheet\Exception: You must specify a worksheet or a scope for a Named Range" due to loading only specific sheets. (Xlsx file contains 3 sheets ('Sheet 1', 'Sheet 2', 'Sheet 3'))

PhpOffice\PhpSpreadsheet\Exception: You must specify a worksheet or a scope for a Named Range

vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/NamedRange.php:21
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/DefinedName.php:97
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php:1399

PhpSpreadsheet - 1.15.0

MarkBaker pushed a commit that referenced this issue Dec 10, 2020
* Fix for 3 Issues Involving ReadXlsx and NamedRange

Issues #1686 and #1723, which provide sample spreadsheets, are probably
solved by this ticket. Issue #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.
@kevsharp81
Copy link

kevsharp81 commented Dec 10, 2020 via email

This was referenced Dec 31, 2020
This was referenced Jul 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

5 participants