Skip to content

Commit 8654e80

Browse files
committed
Ods Writer Eliminate Padding at End of Row
Ods Writer currently will write something like `<table:table-cell table:number-columns-repeated="1023" />` at the end of each row. This is not necessary. In eliminating that, I have made the code a bit more efficient and (hopefully) more readable.
1 parent ad02899 commit 8654e80

File tree

6 files changed

+113
-51
lines changed

6 files changed

+113
-51
lines changed

src/PhpSpreadsheet/Writer/Ods/Content.php

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
*/
2121
class Content extends WriterPart
2222
{
23-
const NUMBER_COLS_REPEATED_MAX = 1024;
24-
const NUMBER_ROWS_REPEATED_MAX = 1048576;
25-
2623
private Formula $formulaConvertor;
2724

2825
/**
@@ -142,7 +139,6 @@ private function writeSheets(XMLWriter $objWriter): void
142139
sprintf('%s_%d_%d', Style::COLUMN_STYLE_PREFIX, $sheetIndex, $columnDimension->getColumnNumeric())
143140
);
144141
$objWriter->writeAttribute('table:default-cell-style-name', 'ce0');
145-
// $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
146142
$objWriter->endElement();
147143
}
148144
$this->writeRows($objWriter, $spreadsheet->getSheet($sheetIndex), $sheetIndex);
@@ -155,34 +151,33 @@ private function writeSheets(XMLWriter $objWriter): void
155151
*/
156152
private function writeRows(XMLWriter $objWriter, Worksheet $sheet, int $sheetIndex): void
157153
{
158-
$numberRowsRepeated = self::NUMBER_ROWS_REPEATED_MAX;
159-
$span_row = 0;
154+
$spanRow = 0;
160155
$rows = $sheet->getRowIterator();
161156
foreach ($rows as $row) {
162-
$cellIterator = $row->getCellIterator();
163-
--$numberRowsRepeated;
164-
if ($cellIterator->valid()) {
165-
$objWriter->startElement('table:table-row');
166-
if ($span_row) {
167-
if ($span_row > 1) {
168-
$objWriter->writeAttribute('table:number-rows-repeated', (string) $span_row);
169-
}
170-
$objWriter->startElement('table:table-cell');
171-
$objWriter->writeAttribute('table:number-columns-repeated', (string) self::NUMBER_COLS_REPEATED_MAX);
157+
$cellIterator = $row->getCellIterator(iterateOnlyExistingCells: true);
158+
$cellIterator->rewind();
159+
$rowStyleExists = $sheet->rowDimensionExists($row->getRowIndex()) && $sheet->getRowDimension($row->getRowIndex())->getRowHeight() > 0;
160+
if ($cellIterator->valid() || $rowStyleExists) {
161+
if ($spanRow) {
162+
$objWriter->startElement('table:table-row');
163+
$objWriter->writeAttribute(
164+
'table:number-rows-repeated',
165+
(string) $spanRow
166+
);
172167
$objWriter->endElement();
173-
$span_row = 0;
174-
} else {
175-
if ($sheet->rowDimensionExists($row->getRowIndex()) && $sheet->getRowDimension($row->getRowIndex())->getRowHeight() > 0) {
176-
$objWriter->writeAttribute(
177-
'table:style-name',
178-
sprintf('%s_%d_%d', Style::ROW_STYLE_PREFIX, $sheetIndex, $row->getRowIndex())
179-
);
180-
}
181-
$this->writeCells($objWriter, $cellIterator);
168+
$spanRow = 0;
169+
}
170+
$objWriter->startElement('table:table-row');
171+
if ($rowStyleExists) {
172+
$objWriter->writeAttribute(
173+
'table:style-name',
174+
sprintf('%s_%d_%d', Style::ROW_STYLE_PREFIX, $sheetIndex, $row->getRowIndex())
175+
);
182176
}
177+
$this->writeCells($objWriter, $cellIterator);
183178
$objWriter->endElement();
184179
} else {
185-
++$span_row;
180+
++$spanRow;
186181
}
187182
}
188183
}
@@ -192,7 +187,6 @@ private function writeRows(XMLWriter $objWriter, Worksheet $sheet, int $sheetInd
192187
*/
193188
private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
194189
{
195-
$numberColsRepeated = self::NUMBER_COLS_REPEATED_MAX;
196190
$prevColumn = -1;
197191
foreach ($cells as $cell) {
198192
/** @var Cell $cell */
@@ -293,17 +287,6 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
293287
$objWriter->endElement();
294288
$prevColumn = $column;
295289
}
296-
297-
$numberColsRepeated = $numberColsRepeated - $prevColumn - 1;
298-
if ($numberColsRepeated > 0) {
299-
if ($numberColsRepeated > 1) {
300-
$objWriter->startElement('table:table-cell');
301-
$objWriter->writeAttribute('table:number-columns-repeated', (string) $numberColsRepeated);
302-
$objWriter->endElement();
303-
} else {
304-
$objWriter->writeElement('table:table-cell');
305-
}
306-
}
307290
}
308291

309292
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
9+
10+
class RepeatEmptyCellsAndRowsTest extends AbstractFunctional
11+
{
12+
public function testSaveAndLoadHyperlinks(): void
13+
{
14+
$spreadsheetOld = new Spreadsheet();
15+
$oldSheet = $spreadsheetOld->getActiveSheet();
16+
$oldSheet->setCellValue('C1', 'xx');
17+
$oldSheet->setCellValue('G1', 'aa');
18+
$oldSheet->setCellValue('BB1', 'bb');
19+
$oldSheet->setCellValue('A6', 'aaa');
20+
$oldSheet->setCellValue('B7', 'bbb');
21+
$oldSheet->getRowDimension(10)->setRowHeight(12);
22+
$oldSheet->setCellValue('A12', 'this is A12');
23+
$style = $oldSheet->getStyle('B14:D14');
24+
$style->getFont()->setBold(true);
25+
$oldSheet->getCell('E15')->setValue('X');
26+
$oldSheet->mergeCells('E15:G16');
27+
$oldSheet->getCell('J15')->setValue('j15');
28+
$oldSheet->getCell('J16')->setValue('j16');
29+
$oldSheet->getCell('A19')->setValue('lastrow');
30+
$spreadsheet = $this->writeAndReload($spreadsheetOld, 'Ods');
31+
$spreadsheetOld->disconnectWorksheets();
32+
33+
$sheet = $spreadsheet->getActiveSheet();
34+
self::assertSame('xx', $sheet->getCell('C1')->getValue());
35+
self::assertSame('aa', $sheet->getCell('G1')->getValue());
36+
self::assertSame('bb', $sheet->getCell('BB1')->getValue());
37+
self::assertSame('aaa', $sheet->getCell('A6')->getValue());
38+
self::assertSame('bbb', $sheet->getCell('B7')->getValue());
39+
self::assertSame('this is A12', $sheet->getCell('A12')->getValue());
40+
// Read styles, including row height, not yet implemented for ODS
41+
self::assertSame('j15', $sheet->getCell('J15')->getValue());
42+
self::assertSame('j16', $sheet->getCell('J16')->getValue());
43+
self::assertSame(['E15:G16' => 'E15:G16'], $sheet->getMergeCells());
44+
self::assertSame('lastrow', $sheet->getCell('A19')->getValue());
45+
46+
$spreadsheet->disconnectWorksheets();
47+
}
48+
}
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
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: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-row>
28+
<table:table-row>
29+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="1">
30+
<text:p>1</text:p>
31+
</table:table-cell>
32+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="3">
33+
<text:p>3</text:p>
34+
</table:table-cell>
35+
</table:table-row>
36+
<table:table-row>
37+
<table:table-cell table:style-name="ce0" office:value-type="float" office:value="3">
38+
<text:p>3</text:p>
39+
</table:table-cell>
40+
</table:table-row>
41+
</table:table>
42+
<table:named-expressions/>
43+
</office:spreadsheet>
44+
</office:body>
45+
</office:document-content>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
<table:calculation-settings />
1818
<table:table table:name="Worksheet" table:style-name="ta1">
1919
<office:forms />
20-
<table:table-row>
21-
<table:table-cell table:style-name="ce0" />
22-
<table:table-cell table:number-columns-repeated="1023" />
23-
</table:table-row>
2420
</table:table>
2521
<table:named-expressions />
2622
</office:spreadsheet>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<table:table-cell office:value="1" office:value-type="float" table:style-name="ce0">
2525
<text:p>1</text:p>
2626
</table:table-cell>
27-
<table:table-cell table:number-columns-repeated="1023"/>
2827
</table:table-row>
2928
</table:table>
3029
<table:table table:name="New Worksheet" table:style-name="ta2">
@@ -33,7 +32,6 @@
3332
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">
3433
<text:p>2</text:p>
3534
</table:table-cell>
36-
<table:table-cell table:number-columns-repeated="1023" />
3735
</table:table-row>
3836
</table:table>
3937
<table:named-expressions />

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
<table:table-cell office:value-type="string" table:style-name="ce0">
9393
<text:p>Lorem ipsum</text:p>
9494
</table:table-cell>
95-
<table:table-cell table:number-columns-repeated="1017"/>
9695
</table:table-row>
9796
<table:table-row>
9897
<table:table-cell office:boolean-value="true" office:value-type="boolean" table:style-name="ce0">
@@ -107,10 +106,6 @@
107106
<table:table-cell office:value="42798.572060185" office:value-type="float" table:style-name="ce1">
108107
<text:p>42798.572060185</text:p>
109108
</table:table-cell>
110-
<table:table-cell table:style-name="ce0"/>
111-
<table:table-cell table:style-name="ce0"/>
112-
<table:table-cell table:style-name="ce0"/>
113-
<table:table-cell table:number-columns-repeated="1017"/>
114109
</table:table-row>
115110
</table:table>
116111
<table:table table:name="New Worksheet" table:style-name="ta2">
@@ -119,7 +114,6 @@
119114
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">
120115
<text:p>2</text:p>
121116
</table:table-cell>
122-
<table:table-cell table:number-columns-repeated="1023"/>
123117
</table:table-row>
124118
</table:table>
125119
<table:named-expressions/>

0 commit comments

Comments
 (0)