Skip to content

column width lost on load and save with Xlsx #4416

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

Closed
comfine opened this issue Mar 21, 2025 · 12 comments · Fixed by #4423
Closed

column width lost on load and save with Xlsx #4416

comfine opened this issue Mar 21, 2025 · 12 comments · Fixed by #4423

Comments

@comfine
Copy link

comfine commented Mar 21, 2025

its my first bugreport on github, so please dont judge if something is wrong here.

when loading a specific xlsx file and using a ireadfilter the columnwidth gets lost. i dont know what exactly causing the problem, but i will add an example xlsx-file.

if i open libreoffice, pressing STRG+A to select everything, then deselect my necessary content with SHIFT, click on first row, secon click on last row that i need, and then rightclick on all remaining selected rows, delete rows, and i try then to load and save, the problem is gone!

one more information, i convert the attached ods-file with libreoffice to xlsx.

broken.ods

hope this helps!

@oleibman
Copy link
Collaborator

Your title suggests that you are having problem with Xlsx, but you have uploaded an Ods file. Can you please upload the Xlsx file with which you are having a problem? Also, you say you are using ireadfilter. Can you share your ireadfilter code?

As for what I have been able to do with the file you uploaded - I opened it in LibreOffice and saved it as Excel. This seems to have been a very inefficient operation - the ods file is 22.7 kilobytes, but the Excel file is 2.62 megabytes. Excel offers me the opportunity to clean up the file, and, when I do that, we're back down to 25.6 Kb. At any rate, when I load and save either version of the file (the big one takes much longer), the column widths appear to be preserved correctly. Perhaps the fact that I don't have a problem is because I'm missing the ireadfilter code.

@comfine
Copy link
Author

comfine commented Mar 21, 2025

as i described, the file is beeing converted using libreOffice through PHP
(exec("$libreOfficePath --headless --convert-to xlsx "$filepath" --outdir "".DOCROOT.""", $output, $returnCode);

anyway, here is the converted file, that is beeing used after the converting process in phpspreadsheet.

broken.xlsx

i also noticed, that the file is getting very big with the convert. i dont know why. but the loss of the column width happens not within libreoffice (at least i didnt check that) but when you load it with phpspreadsheet and save it afterwards, the column width will be gone, but as i said, only if i use the IReadFilter.

@oleibman
Copy link
Collaborator

As expected, my load and save of your newly-uploaded file using PhpSpreadsheet preserves column widths. You say you were using an ireadfilter. Perhaps that accounts for the difference. Please share your ireadfilter code.

@comfine
Copy link
Author

comfine commented Mar 24, 2025

this is the code:

class ProtocolFilter implements IReadFilter {
public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool {
// Nur Spalten A bis D und Zeilen 1 bis 505 laden
$allowedColumns = USEDCOLUMNS;
$allowedRows = range(1, 505);
return in_array($columnAddress, $allowedColumns, true) && in_array($row, $allowedRows, true);
}
}

@oleibman
Copy link
Collaborator

Thank you for supplying the code. I am able to duplicate your problem when I use it, and will investigate.

@oleibman
Copy link
Collaborator

So far, I am not able to duplicate your result on a small version of your spreadsheet. I will need to figure out why there is a difference.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Mar 25, 2025
Fix PHPOffice#4416. A peculiar problem indeed. PhpSpreadsheet has been considering a column to be filtered if any cell in the column is filtered and does not preserve the column width if that is the case. It should consider the column not filtered if any cell in the column is not filtered, and consider it filtered only if there are no cells to which that applies. At least, that's how I think it should work, and this change doesn't break any existing tests, and solves this issue.
@comfine
Copy link
Author

comfine commented Mar 25, 2025

i can imagine, that there are some cell-formation-settings defined, that applies to the ENTIRE (endless?) sheet, which cause somehow the problem - but it's just an idea.

@oleibman
Copy link
Collaborator

If you can test against PR #4423, please do so.

@comfine
Copy link
Author

comfine commented Mar 25, 2025

thanks for this super fast fix!
i am pretty new to composer (and never really understood how to use github "properly" - can i update with composer somehow to the latest build? i cant find any information about that in the docs. or do i need to download the latest source and replace it in my environment to test?

@oleibman
Copy link
Collaborator

One possible approach is:

git clone -b issue4416 --single-branch https://github.com/oleibman/PhpSpreadsheet.git newdirectoryname
cd newdirectoryname
composer install --no-dev

Then require newdirectoryname/vendor/autoload.php in your test script.

@comfine
Copy link
Author

comfine commented Mar 26, 2025

thanks alot! i confirm, the bug is fixed! with the new code, the column width remains how it should. thanks alot!

@oleibman
Copy link
Collaborator

Thank you for confirming. I will merge the PR into master later today.

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