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

can't open XLSX sheet, out of bounds -1 #4391

Open
gauze opened this issue Mar 2, 2025 · 8 comments
Open

can't open XLSX sheet, out of bounds -1 #4391

gauze opened this issue Mar 2, 2025 · 8 comments

Comments

@gauze
Copy link

gauze commented Mar 2, 2025

PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php:626

$ file po1326.xlsx
po1326.xlsx: Microsoft Excel 2007+

^ opens ok in windows and libreoffice

$ composer show|grep spreadsheet
phpoffice/phpspreadsheet 4.1.0 PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine

[code snippet]
$inputFileType = 'Xlsx';
$inputFileName = DIR . '/po1326.xlsx';

$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);

$spreadsheet = $reader->load($inputFileName);
$activeWorksheet = $spreadsheet->getActiveSheet();
[snip]
throws error here.

I do not know what is wrong here.

thanks
brian

@oleibman
Copy link
Collaborator

oleibman commented Mar 3, 2025

Neither do I. Can you upload your spreadsheet?

@gauze
Copy link
Author

gauze commented Mar 3, 2025

sure thing. I think I deleted anything "incriminating"

po1326.xlsx

@oleibman
Copy link
Collaborator

oleibman commented Mar 3, 2025

Thank you for providing your spreadsheet. I am unable to duplicate your result with that spreadsheet and your code. Is it possible that the error is happening later than you think? What release of PhpSpreadsheet (from the above I think it's 4.1.0) and Php are you using? What environment?

My code (only minor modifications from yours):

<?php

require __DIR__ . '/PhpSpreadsheet' . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

class issue4391 {
    public static function issue(): void
    {
        $inputFileType = 'Xlsx';
        $inputFileName = 'issue.4391.xlsx';
        $reader = IOFactory::createReader($inputFileType);
        $reader->setReadDataOnly(true);
        $spreadsheet = $reader->load($inputFileName);
        $activeWorksheet = $spreadsheet->getActiveSheet();
        var_dump($activeWorksheet->getCell('A7')->getValue());
    }
}

issue4391::issue();

My result:

C:\git\issue.4391.php:16:
string(3) "SEA"

@gauze
Copy link
Author

gauze commented Mar 3, 2025

I copied your code, changed the path to vendor/autoload.php
same error :S

$ php testsheet.php
PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php:626
Stack trace:
#0 /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php(501): PhpOffice\PhpSpreadsheet\Spreadsheet->getSheet()
#1 /home/gauze/phpspread/testsheet.php(14): PhpOffice\PhpSpreadsheet\Spreadsheet->getActiveSheet()
#2 /home/gauze/phpspread/testsheet.php(19): issue4391::issue()
#3 {main}
thrown in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php on line 626

do my dependencies look ok?
$ composer show
composer/pcre 3.3.2 PCRE wrapping library that offers type-safe preg_* replacements.
maennchen/zipstream-php 3.1.2 ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on...
markbaker/complex 3.0.2 PHP Class for working with complex numbers
markbaker/matrix 3.0.1 PHP Class for working with matrices
phpoffice/phpspreadsheet 4.1.0 PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
psr/http-client 1.0.3 Common interface for HTTP clients
psr/http-factory 1.1.0 PSR-17: Common interfaces for PSR-7 HTTP message factories
psr/http-message 2.0 Common interface for HTTP messages
psr/simple-cache 3.0.0 Common interfaces for simple caching

@oleibman
Copy link
Collaborator

oleibman commented Mar 3, 2025

Your dependencies match mine. I am at a loss to think of why your result should differ from mine.

@gauze
Copy link
Author

gauze commented Mar 3, 2025

are there any environmental variables that could effect this? php?

@gauze
Copy link
Author

gauze commented Mar 3, 2025

I just tried this on an Ubuntu 24.04 machine (vs Fedora 41) and no errors.
some kind of Fedora problem

@oleibman
Copy link
Collaborator

oleibman commented Mar 4, 2025

I'm glad you found this, but it seems awfully surprising. Do you have a way of testing if you can use ZipArchive on Fedora 41 to read other zip files? (An Xlsx spreadsheet is a zip archive composed mostly of xml files.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants