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

Default style alignment does not work for Xlsx Writer #3918

Closed
2 of 8 tasks
homersimpsons opened this issue Feb 25, 2024 · 9 comments · Fixed by #3924
Closed
2 of 8 tasks

Default style alignment does not work for Xlsx Writer #3918

homersimpsons opened this issue Feb 25, 2024 · 9 comments · Fixed by #3924

Comments

@homersimpsons
Copy link
Contributor

homersimpsons commented Feb 25, 2024

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Default alignment style should be applied with Xlsx writer.

What is the current behavior?

Default alignment style is not applied with Xlsx writer.

What are the steps to reproduce?

<?php

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

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$spreadsheetStyle = $spreadsheet->getDefaultStyle();
$spreadsheetStyle->getAlignment()
    ->setWrapText(true)
    ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)
    ->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);

$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell('A1');
$cell->setValue('aaaaaaaaaaaaaa');

// $xlsx = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet); // Working
$xlsx = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); // Not Working

$xlsx->save('alignment.xlsx'); // Optionally update with `.xls`

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

It affects Xlsx writer but not Xls writer. I did not test for other formats

Which versions of PhpSpreadsheet and PHP are affected?

At least v2.0.0 (the latest at this time): https://github.com/PHPOffice/PhpSpreadsheet/releases/tag/2.0.0

References

/cc @oleibman as you fixed the other similar issue

Thank you to all maintainers for this awesome library !

@oleibman
Copy link
Collaborator

I don't see a difference in the 2 files you supplied.
xls (presumed working)
image
xlsx (presumed not working)
image
Are you seeing something different in one of the files?

@homersimpsons
Copy link
Contributor Author

Oh, so that should be a LibreOffice issue ?
Working (xls): image
Not Working (xlsx) image

@oleibman
Copy link
Collaborator

Yes, this definitely appears to be an issue with LibreOffice. As I described it in PR #3459, Excels's implementation of Alignment is "mysterious". To match Excel's behavior, LibreOffice needs to unravel the mystery when reading (and probably writing) am Xlsx spreadsheet, as PhpSpreadsheet has done.

@oleibman
Copy link
Collaborator

BTW, I believe LibreOffice has a problem only when Alignment is specified in the default font. As a workaround, you could explicitly specify Alignment on the specific cells which you want aligned.

@homersimpsons
Copy link
Contributor Author

As a workaround, you could explicitly specify Alignment on the specific cells which you want aligned.

Yes, but that means doing so as a second step. Thank you for your help.

@oleibman
Copy link
Collaborator

Interestingly, this problem happens when the s (style number) attribute is omitted from the c (cell) tag.

<c r="A1" t="s">

However, when the s attribute is explicitly specified as 0 (default), the alignment seems okay.

<c r="A1" t="s" s="0">

I might be willing to consider adding an optional parameter to the Xlsx Writer to produce this behavior. But the problem should still be reported to LibreOffice.

@homersimpsons
Copy link
Contributor Author

I should admit that I do not know what all of those mean. But I think I understand the different parameters:

  • <c>: a cell
  • r="A1": reference of the cell
  • t="s": type is string
  • s="0": style is "0", "0" is a reference to the default style

I might be willing to consider adding an optional parameter to the Xlsx Writer to produce this behavior.

Your call, I think that would be great for LibreOffice users. I let you re-open this issue if you want too.

I think if it is expected to be omitted to mean "default" then we can keep the current behaviour (without s="0").

To get a bit more context, how does that work in the Xls writer ?

But the problem should still be reported to LibreOffice.

I will report it in the up-coming hours, it does not look like there is an open bug reported for this on https://bugs.documentfoundation.org/buglist.cgi?quicksearch=alignment%20xlsx.

@oleibman
Copy link
Collaborator

Your explanation of the xml is correct. Xls format does not use Xml; it uses a proprietary binary format that I'm not particularly familiar with, but one can certainly conjecture that style # for a cell is always present.

When you do open your issue with LibreOffice, you might want to, say, set bold in the default style. This will demonstrate that that property is handled correctly even when 's' is omitted from the xml, even though alignment is not.

@homersimpsons
Copy link
Contributor Author

I just reported it: https://bugs.documentfoundation.org/show_bug.cgi?id=159916 let's hope they can fix it easily.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Feb 29, 2024
Fix PHPOffice#3918, sort of. Xlsx cells use the default style when they omit the `s` tag, or when `s="0"` is specified. LibreOffice does not honor Alignment in the default Style unless the cell explicitly uses the second form, even though it honors other default Styles (e.g. bold font) when `s` is omitted. Gnumeric seems to have the same problem. A bug report has been filed with LibreOffice.

In the meantime, this PR adds to Xlsx Writer an optional boolean property `explicitStyle0` with setter and getter. Default is false, which will continue the current behavior by adding an `s` tag only when the cell uses a non-default style (this is how Excel itself behaves). When set to true, Xlsx Writer will explicity write `s="0"` for all cells using default style. This will allow users to create an Xlsx spreadsheet with default alignment of cells that will show up correctly when the spreadsheet is viewed with LibreOffice. Technically speaking, it is *probably* safe to always use `true`, except that the spreadsheet size will be a bit larger. However, my hope is that this is a temporary measure which can go away when the vendors have had a chance to fix their problems, hence the `false` default.
@oleibman oleibman mentioned this issue Feb 29, 2024
11 tasks
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