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

Protect Sheet But Allow Sort not working #3951

Closed
don-odyssey opened this issue Mar 18, 2024 · 2 comments · Fixed by #3956
Closed

Protect Sheet But Allow Sort not working #3951

don-odyssey opened this issue Mar 18, 2024 · 2 comments · Fixed by #3956

Comments

@don-odyssey
Copy link

Hi,

When applying a filter and protecting the sheet, the Sort does not work

@don-odyssey
Copy link
Author

Here is my PHP code

// set security
$protection = $spreadsheet->getActiveSheet()->getProtection();
$protection->setPassword($mslReportsPassword);
$protection->setSheet(true);
$protection->setInsertRows(true);
$protection->setFormatCells(true);
$protection->setObjects(true);
$protection->setAutoFilter(false);
$protection->setSort(true);

@oleibman oleibman changed the title Sort not working Protect Sheet But Allow Sort not working Mar 18, 2024
@oleibman
Copy link
Collaborator

oleibman commented Mar 18, 2024

Enabling sorting on a protected sheet in Excel seems more complicated than it ought to be. See https://www.extendoffice.com/documents/excel/4673-excel-sort-filter-protected-sheet.html. This means that you have to explicitly protect ranges that you want to be able to sort/filter on a protected sheet. PhpSpreadsheet supports protected ranges, except that it assumes they will always have a password (and does not support the name attribute, which I'm not sure is significant). I will treat this issue as a feature request, and start looking into that.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Mar 20, 2024
Fix PHPOffice#3951. When an Excel sheet is protected, even when sorting is explicitly allowed without a password, sorts are permitted only on "protected ranges" within the sheet. PhpSpreadsheet already supports protected ranges, and only minor tinkering is necessary for that (e.g. the protected range can have, but does not require, a password). The more important part of this change is documenting the far-from-intuitive way that Excel handles this. To that end, documentation is updated, and a new sample is added.

A new class, `Worksheet\ProtectedRange` is added in place of the string array which had been used. `Worksheet::getProtectedCells` is deprecated in favor of the new `Worksheet::getProtectedCellRanges`.
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