From e2ec705ee8c5bd7d7daeee3d66b8425266287bfc Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Sat, 7 Dec 2024 00:26:40 -0800 Subject: [PATCH] Ods Writer Master Page Name PR #2850 and PR #2851 added support for Worksheet Visibility to ODS. They work well when LibreOffice is used to open the spreadsheet. However, when Excel tries to open it, it reports corruption. Strictly speaking, this is not a PhpSpreadsheet problem, but, if we can fix it, we should. It took a while to figure out what's bothering Excel. I'm not sure that all of what follows is necessary, but it works. It appears that it wants the content.xml `style:automatic-styles` definition for the `table` (i.e. worksheet) to include a `style:master-page-name` attribute. That attribute requires a corresponding definition in the `office:master-styles` section in styles.xml, and that attribute likewise requires a definition in `office:automatic-styles`. The new entries in styles.xml can be used to specify things like header and footer. However, the ways that these are specified is distinctly different from what Excel (and therefore PhpSpreadsheet) does. Implementing that will be a good future project. However, for now, they will remain unsupported for Ods. --- src/PhpSpreadsheet/Writer/Ods/Cell/Style.php | 1 + src/PhpSpreadsheet/Writer/Ods/Styles.php | 13 ++++- tests/data/Writer/Ods/content-arrays.xml | 49 ++++++++++++++++++- tests/data/Writer/Ods/content-empty.xml | 2 +- .../Writer/Ods/content-hidden-worksheet.xml | 4 +- tests/data/Writer/Ods/content-with-data.xml | 4 +- 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php index 757323068..c1c9f32c1 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php +++ b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php @@ -289,6 +289,7 @@ class Style 'style:name', sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId) ); + $this->writer->writeAttribute('style:master-page-name', 'Default'); $this->writer->startElement('style:table-properties'); diff --git a/src/PhpSpreadsheet/Writer/Ods/Styles.php b/src/PhpSpreadsheet/Writer/Ods/Styles.php index 448b1eff1..6bfdfb1b3 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Styles.php +++ b/src/PhpSpreadsheet/Writer/Ods/Styles.php @@ -56,8 +56,17 @@ class Styles extends WriterPart $objWriter->writeElement('office:font-face-decls'); $objWriter->writeElement('office:styles'); - $objWriter->writeElement('office:automatic-styles'); - $objWriter->writeElement('office:master-styles'); + $objWriter->startElement('office:automatic-styles'); + $objWriter->startElement('style:page-layout'); + $objWriter->writeAttribute('style:name', 'Mpm1'); + $objWriter->endElement(); // style:page-layout + $objWriter->endElement(); // office:automatic-styles + $objWriter->startElement('office:master-styles'); + $objWriter->startElement('style:master-page'); + $objWriter->writeAttribute('style:name', 'Default'); + $objWriter->writeAttribute('style:page-layout-name', 'Mpm1'); + $objWriter->endElement(); //style:master-page + $objWriter->endElement(); //office:master-styles $objWriter->endElement(); return $objWriter->getData(); diff --git a/tests/data/Writer/Ods/content-arrays.xml b/tests/data/Writer/Ods/content-arrays.xml index a33b7dbfc..231036ee2 100644 --- a/tests/data/Writer/Ods/content-arrays.xml +++ b/tests/data/Writer/Ods/content-arrays.xml @@ -1,2 +1,49 @@ -11133 \ No newline at end of file + + + + + + + + + + + + + + + + + + + + +1 + + +1 + + + + + +1 + + +3 + + + + + +3 + + + + + + + + + \ No newline at end of file diff --git a/tests/data/Writer/Ods/content-empty.xml b/tests/data/Writer/Ods/content-empty.xml index 84f4c2397..cb14ce7e5 100644 --- a/tests/data/Writer/Ods/content-empty.xml +++ b/tests/data/Writer/Ods/content-empty.xml @@ -3,7 +3,7 @@ - + diff --git a/tests/data/Writer/Ods/content-hidden-worksheet.xml b/tests/data/Writer/Ods/content-hidden-worksheet.xml index 88a53257a..1d8956050 100644 --- a/tests/data/Writer/Ods/content-hidden-worksheet.xml +++ b/tests/data/Writer/Ods/content-hidden-worksheet.xml @@ -3,10 +3,10 @@ - + - + diff --git a/tests/data/Writer/Ods/content-with-data.xml b/tests/data/Writer/Ods/content-with-data.xml index db7d75a74..82209609f 100644 --- a/tests/data/Writer/Ods/content-with-data.xml +++ b/tests/data/Writer/Ods/content-with-data.xml @@ -3,10 +3,10 @@ - + - +