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

Saving XLSX unhides every row #1641

Closed
wyattGTL opened this issue Sep 4, 2020 · 6 comments · Fixed by #2414
Closed

Saving XLSX unhides every row #1641

wyattGTL opened this issue Sep 4, 2020 · 6 comments · Fixed by #2414

Comments

@wyattGTL
Copy link

wyattGTL commented Sep 4, 2020

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=$fileName");
header("Cache-Control: max-age=0");
$writer->save('php://output');

All of the rows get un-hidden when exporting like that. I've verified that the rows are in fact hidden in the original file.
How can I fix this?

@wyattGTL
Copy link
Author

wyattGTL commented Sep 4, 2020

I am using version 1.14

@wyattGTL
Copy link
Author

wyattGTL commented Sep 6, 2020

Just realized it only happens with some files. Here is a file that is happens with.
libre.xlsx

@wyattGTL
Copy link
Author

wyattGTL commented Sep 6, 2020

Rows 2 and 5 will not be hidden after exporting the file with the above code.

@wyattGTL
Copy link
Author

wyattGTL commented Sep 6, 2020

Before saving the file, I get the row dimensions and verify that the rows are in fact hidden. Something is going wrong in the saving with this specific file. I would appreciate any help, I'm in a bit of a time crunch unfortunately.

@stale
Copy link

stale bot commented Dec 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Dec 25, 2020
@oleibman
Copy link
Collaborator

The worksheet includes autofilters, and Excel autofilters will find and display previously hidden rows if they satisfy the filter. PhpSpreadsheet evaluates autofilters when writing, which is leading to your problem. I'm not sure that it's practical to change this behavior - it would represent a breaking change for many programs which currently execute as expected.

@stale stale bot removed the stale label Nov 22, 2021
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 24, 2021
…aking Change

Fix PHPOffice#1641. Excel allows explicit hiding of row after filter is applied, but PhpSpreadsheet automatically invokes showHideRows on all auto-filters, preventing users from doing the same. Change to invoke showHideRows only if it hasn't already been invoked, or if filter criteria have changed since it was last invoked. Autofilters read in from an existing spreadsheet are assumed to be already invoked.

This is potentially a breaking change, probably a minor one. The conditions to set up 1641 are probably uncommon, but users who meet those conditions and are happy with the current behavior will see a break. The new behavior is closer to how Excel itself behaves. A new method `reevaluateAutoFilters` is added to `Spreadsheet`; this can be used to restore the old behavior if desired. The new method is added to the documentation, along with a description of how the situation described in 1641 is handled in Excel and PhpSpreadsheet.

While examining Excel's behavior, it became evident that, although a filter is applied to an entire column, it is actually applied only to the rows that are populated when the filter is defined, as can be verified by examining the XML definition of the filter. When you re-apply the filter, rows that have been added since are considered. It would be useful to provide PhpSpreadsheet with a method to do the same. I have added, and documented, `setRangeToMaxRow` to `AutoFilter`.
oleibman added a commit that referenced this issue Dec 5, 2021
…aking Change (#2414)

Fix #1641. Excel allows explicit hiding of row after filter is applied, but PhpSpreadsheet automatically invokes showHideRows on all auto-filters, preventing users from doing the same. Change to invoke showHideRows only if it hasn't already been invoked, or if filter criteria have changed since it was last invoked. Autofilters read in from an existing spreadsheet are assumed to be already invoked.

This is potentially a breaking change, probably a minor one. The conditions to set up 1641 are probably uncommon, but users who meet those conditions and are happy with the current behavior will see a break. The new behavior is closer to how Excel itself behaves. A new method `reevaluateAutoFilters` is added to `Spreadsheet`; this can be used to restore the old behavior if desired. The new method is added to the documentation, along with a description of how the situation described in 1641 is handled in Excel and PhpSpreadsheet.

While examining Excel's behavior, it became evident that, although a filter is applied to an entire column, it is actually applied only to the rows that are populated when the filter is defined, as can be verified by examining the XML definition of the filter. When you re-apply the filter, rows that have been added since are considered. It would be useful to provide PhpSpreadsheet with a method to do the same. I have added, and documented, `setRangeToMaxRow` to `AutoFilter`.
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.

2 participants