Skip to content

Commit e2ec705

Browse files
committed
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.
1 parent ad02899 commit e2ec705

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

src/PhpSpreadsheet/Writer/Ods/Cell/Style.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public function writeTableStyle(Worksheet $worksheet, int $sheetId): void
289289
'style:name',
290290
sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId)
291291
);
292+
$this->writer->writeAttribute('style:master-page-name', 'Default');
292293

293294
$this->writer->startElement('style:table-properties');
294295

src/PhpSpreadsheet/Writer/Ods/Styles.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,17 @@ public function write(): string
5656

5757
$objWriter->writeElement('office:font-face-decls');
5858
$objWriter->writeElement('office:styles');
59-
$objWriter->writeElement('office:automatic-styles');
60-
$objWriter->writeElement('office:master-styles');
59+
$objWriter->startElement('office:automatic-styles');
60+
$objWriter->startElement('style:page-layout');
61+
$objWriter->writeAttribute('style:name', 'Mpm1');
62+
$objWriter->endElement(); // style:page-layout
63+
$objWriter->endElement(); // office:automatic-styles
64+
$objWriter->startElement('office:master-styles');
65+
$objWriter->startElement('style:master-page');
66+
$objWriter->writeAttribute('style:name', 'Default');
67+
$objWriter->writeAttribute('style:page-layout-name', 'Mpm1');
68+
$objWriter->endElement(); //style:master-page
69+
$objWriter->endElement(); //office:master-styles
6170
$objWriter->endElement();
6271

6372
return $objWriter->getData();
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2"><office:scripts/><office:font-face-decls/><office:automatic-styles><style:style style:family="table" style:name="ta1"><style:table-properties table:display="true"/></style:style><style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties style:vertical-align="bottom" style:rotation-align="none"/><style:paragraph-properties fo:text-align="start"/><style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/></style:style></office:automatic-styles><office:body><office:spreadsheet><table:calculation-settings/><table:table table:name="Worksheet" table:style-name="ta1"><office:forms/><table:table-row><table:table-cell table:style-name="ce0" office:value-type="float" office:value="1"><text:p>1</text:p></table:table-cell><table:table-cell table:style-name="ce0" table:number-matrix-columns-spanned="1" table:number-matrix-rows-spanned="2" table:formula="of:=UNIQUE([.A1:.A3])" office:value-type="float" office:value="1"><text:p>1</text:p></table:table-cell><table:table-cell table:number-columns-repeated="1022"/></table:table-row><table:table-row><table:table-cell table:style-name="ce0" office:value-type="float" office:value="1"><text:p>1</text:p></table:table-cell><table:table-cell table:style-name="ce0" office:value-type="float" office:value="3"><text:p>3</text:p></table:table-cell><table:table-cell table:number-columns-repeated="1022"/></table:table-row><table:table-row><table:table-cell table:style-name="ce0" office:value-type="float" office:value="3"><text:p>3</text:p></table:table-cell><table:table-cell table:style-name="ce0"/><table:table-cell table:number-columns-repeated="1022"/></table:table-row></table:table><table:named-expressions/></office:spreadsheet></office:body></office:document-content>
2+
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
3+
<office:scripts/>
4+
<office:font-face-decls/>
5+
<office:automatic-styles>
6+
<style:style style:master-page-name="Default" style:family="table" style:name="ta1">
7+
<style:table-properties table:display="true"/>
8+
</style:style>
9+
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">
10+
<style:table-cell-properties style:vertical-align="bottom" style:rotation-align="none"/>
11+
<style:paragraph-properties fo:text-align="start"/>
12+
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/>
13+
</style:style>
14+
</office:automatic-styles>
15+
<office:body>
16+
<office:spreadsheet>
17+
<table:calculation-settings/>
18+
<table:table table:name="Worksheet" table:style-name="ta1">
19+
<office:forms/>
20+
<table:table-row>
21+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="1">
22+
<text:p>1</text:p>
23+
</table:table-cell>
24+
<table:table-cell table:style-name="ce0" table:number-matrix-columns-spanned="1" table:number-matrix-rows-spanned="2" table:formula="of:=UNIQUE([.A1:.A3])" office:value-type="float" office:value="1">
25+
<text:p>1</text:p>
26+
</table:table-cell>
27+
<table:table-cell table:number-columns-repeated="1022"/>
28+
</table:table-row>
29+
<table:table-row>
30+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="1">
31+
<text:p>1</text:p>
32+
</table:table-cell>
33+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="3">
34+
<text:p>3</text:p>
35+
</table:table-cell>
36+
<table:table-cell table:number-columns-repeated="1022"/>
37+
</table:table-row>
38+
<table:table-row>
39+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="3">
40+
<text:p>3</text:p>
41+
</table:table-cell>
42+
<table:table-cell table:style-name="ce0"/>
43+
<table:table-cell table:number-columns-repeated="1022"/>
44+
</table:table-row>
45+
</table:table>
46+
<table:named-expressions/>
47+
</office:spreadsheet>
48+
</office:body>
49+
</office:document-content>

tests/data/Writer/Ods/content-empty.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<office:scripts />
44
<office:font-face-decls />
55
<office:automatic-styles>
6-
<style:style style:family="table" style:name="ta1">
6+
<style:style style:family="table" style:master-page-name="Default" style:name="ta1">
77
<style:table-properties table:display="true" />
88
</style:style>
99
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">

tests/data/Writer/Ods/content-hidden-worksheet.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<office:scripts />
44
<office:font-face-decls />
55
<office:automatic-styles>
6-
<style:style style:family="table" style:name="ta1">
6+
<style:style style:family="table" style:master-page-name="Default" style:name="ta1">
77
<style:table-properties table:display="true" />
88
</style:style>
9-
<style:style style:family="table" style:name="ta2">
9+
<style:style style:family="table" style:master-page-name="Default" style:name="ta2">
1010
<style:table-properties table:display="false"/>
1111
</style:style>
1212
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">

tests/data/Writer/Ods/content-with-data.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<office:scripts/>
44
<office:font-face-decls/>
55
<office:automatic-styles>
6-
<style:style style:family="table" style:name="ta1">
6+
<style:style style:family="table" style:master-page-name="Default" style:name="ta1">
77
<style:table-properties table:display="true" />
88
</style:style>
9-
<style:style style:family="table" style:name="ta2">
9+
<style:style style:family="table" style:master-page-name="Default" style:name="ta2">
1010
<style:table-properties table:display="true" />
1111
</style:style>
1212
<style:style style:family="table-cell" style:name="ce0" style:parent-style-name="Default">

0 commit comments

Comments
 (0)