Skip to content

Conversation

@oleibman
Copy link
Collaborator

In PhpSpreadsheet Release 1, if the LoadSheetsOnly option was specified, and no sheets matched, a new blank sheet was created. This behavior changed in PhpSpreadsheet Release 2, so that an exception wound up being thrown instead. Although the Release 2 approach seems more sensible to me, it was actually collateral damage from a different change, and was not an intentional result.

The difference in behavior is causing a problem for Laravel-Excel. In particular, a PR which would move their supported PhpSpreadsheet release from 1 to 5, is delayed because this change in behavior breaks part of their test suite. See SpartnerNL/Laravel-Excel#4302. We would very much like them to get off release 1. I volunteered to add a compatibility option to the Readers which would emulate the release 1 behavior. The result is this PR.

Usage:

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setLoadSheetsOnly([list of sheet names]);
if (method_exists($reader, 'setCreateBlankSheetIfNoneRead')) {
    $reader->setCreateBlankSheetIfNoneRead(true);
}

In addition to Xlsx Reader, the method is available for Xls, Ods, Xml, and Gnumeric.

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

In PhpSpreadsheet Release 1, if the LoadSheetsOnly option was specified, and no sheets matched, a new blank sheet was created. This behavior changed in PhpSpreadsheet Release 2, so that an exception wound up being thrown instead. Although the Release 2 approach seems more sensible to me, it was actually collateral damage from a different change, and was not an intentional result.

The difference in behavior is causing a problem for Laravel-Excel. In particular, a PR which would move their supported PhpSpreadsheet release from 1 to 5, is delayed because this change in behavior breaks part of their test suite. See SpartnerNL/Laravel-Excel#4302. We would very much like them to get off release 1. I volunteered to add a compatibility option to the Readers which would emulate the release 1 behavior. The result is this PR.

Usage:
```php
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setLoadSheetsOnly([list of sheet names]);
if (method_exists($reader, 'setCreateBlankSheetIfNoneRead')) {
    $reader->setCreateBlankSheetIfNoneRead(true);
}
```
In addition to Xlsx Reader, the method is available for Xls, Ods, Xml, and Gnumeric.
@patrickbrouwers
Copy link
Contributor

Looks good @oleibman ! If it's possible, could you release this earlier than around the PHP8.5 release, with this addition I think I can complete the update to 5.0 in Laravel Excel. Thanks!

@oleibman
Copy link
Collaborator Author

@patrickbrouwers Thanks for confirming. I will schedule a release for next week, tentatively Wed. Sep. 3.

@oleibman oleibman enabled auto-merge September 1, 2025 06:43
@oleibman oleibman added this pull request to the merge queue Sep 1, 2025
Merged via the queue into PHPOffice:master with commit ff7195c Sep 1, 2025
13 checks passed
@oleibman oleibman deleted the blanksheet branch September 1, 2025 06:50
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Sep 1, 2025
oleibman added a commit that referenced this pull request Sep 1, 2025
…tsOnly list. (#4622)

* Option for Readers to create a new blank sheet if none match LoadSheetsOnly list.

Backport of PR #4618.

* Update CHANGELOG.md
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Sep 1, 2025
oleibman added a commit that referenced this pull request Sep 1, 2025
…tsOnly list. (#4623)

* Option for Readers to create a new blank sheet if none match LoadSheetsOnly list.

Backport of PR #4618.

* Update CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants