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.
This commit is contained in:
oleibman
2024-12-05 20:14:12 -08:00
parent ad0289977b
commit 8654e805f4
6 changed files with 113 additions and 51 deletions
+21 -38
View File
@@ -20,9 +20,6 @@ use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Style;
*/
class Content extends WriterPart
{
const NUMBER_COLS_REPEATED_MAX = 1024;
const NUMBER_ROWS_REPEATED_MAX = 1048576;
private Formula $formulaConvertor;
/**
@@ -142,7 +139,6 @@ class Content extends WriterPart
sprintf('%s_%d_%d', Style::COLUMN_STYLE_PREFIX, $sheetIndex, $columnDimension->getColumnNumeric())
);
$objWriter->writeAttribute('table:default-cell-style-name', 'ce0');
// $objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
$objWriter->endElement();
}
$this->writeRows($objWriter, $spreadsheet->getSheet($sheetIndex), $sheetIndex);
@@ -155,34 +151,33 @@ class Content extends WriterPart
*/
private function writeRows(XMLWriter $objWriter, Worksheet $sheet, int $sheetIndex): void
{
$numberRowsRepeated = self::NUMBER_ROWS_REPEATED_MAX;
$span_row = 0;
$spanRow = 0;
$rows = $sheet->getRowIterator();
foreach ($rows as $row) {
$cellIterator = $row->getCellIterator();
--$numberRowsRepeated;
if ($cellIterator->valid()) {
$objWriter->startElement('table:table-row');
if ($span_row) {
if ($span_row > 1) {
$objWriter->writeAttribute('table:number-rows-repeated', (string) $span_row);
}
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', (string) self::NUMBER_COLS_REPEATED_MAX);
$cellIterator = $row->getCellIterator(iterateOnlyExistingCells: true);
$cellIterator->rewind();
$rowStyleExists = $sheet->rowDimensionExists($row->getRowIndex()) && $sheet->getRowDimension($row->getRowIndex())->getRowHeight() > 0;
if ($cellIterator->valid() || $rowStyleExists) {
if ($spanRow) {
$objWriter->startElement('table:table-row');
$objWriter->writeAttribute(
'table:number-rows-repeated',
(string) $spanRow
);
$objWriter->endElement();
$span_row = 0;
} else {
if ($sheet->rowDimensionExists($row->getRowIndex()) && $sheet->getRowDimension($row->getRowIndex())->getRowHeight() > 0) {
$objWriter->writeAttribute(
'table:style-name',
sprintf('%s_%d_%d', Style::ROW_STYLE_PREFIX, $sheetIndex, $row->getRowIndex())
);
}
$this->writeCells($objWriter, $cellIterator);
$spanRow = 0;
}
$objWriter->startElement('table:table-row');
if ($rowStyleExists) {
$objWriter->writeAttribute(
'table:style-name',
sprintf('%s_%d_%d', Style::ROW_STYLE_PREFIX, $sheetIndex, $row->getRowIndex())
);
}
$this->writeCells($objWriter, $cellIterator);
$objWriter->endElement();
} else {
++$span_row;
++$spanRow;
}
}
}
@@ -192,7 +187,6 @@ class Content extends WriterPart
*/
private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
{
$numberColsRepeated = self::NUMBER_COLS_REPEATED_MAX;
$prevColumn = -1;
foreach ($cells as $cell) {
/** @var Cell $cell */
@@ -293,17 +287,6 @@ class Content extends WriterPart
$objWriter->endElement();
$prevColumn = $column;
}
$numberColsRepeated = $numberColsRepeated - $prevColumn - 1;
if ($numberColsRepeated > 0) {
if ($numberColsRepeated > 1) {
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', (string) $numberColsRepeated);
$objWriter->endElement();
} else {
$objWriter->writeElement('table:table-cell');
}
}
}
/**
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class RepeatEmptyCellsAndRowsTest extends AbstractFunctional
{
public function testSaveAndLoadHyperlinks(): void
{
$spreadsheetOld = new Spreadsheet();
$oldSheet = $spreadsheetOld->getActiveSheet();
$oldSheet->setCellValue('C1', 'xx');
$oldSheet->setCellValue('G1', 'aa');
$oldSheet->setCellValue('BB1', 'bb');
$oldSheet->setCellValue('A6', 'aaa');
$oldSheet->setCellValue('B7', 'bbb');
$oldSheet->getRowDimension(10)->setRowHeight(12);
$oldSheet->setCellValue('A12', 'this is A12');
$style = $oldSheet->getStyle('B14:D14');
$style->getFont()->setBold(true);
$oldSheet->getCell('E15')->setValue('X');
$oldSheet->mergeCells('E15:G16');
$oldSheet->getCell('J15')->setValue('j15');
$oldSheet->getCell('J16')->setValue('j16');
$oldSheet->getCell('A19')->setValue('lastrow');
$spreadsheet = $this->writeAndReload($spreadsheetOld, 'Ods');
$spreadsheetOld->disconnectWorksheets();
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('xx', $sheet->getCell('C1')->getValue());
self::assertSame('aa', $sheet->getCell('G1')->getValue());
self::assertSame('bb', $sheet->getCell('BB1')->getValue());
self::assertSame('aaa', $sheet->getCell('A6')->getValue());
self::assertSame('bbb', $sheet->getCell('B7')->getValue());
self::assertSame('this is A12', $sheet->getCell('A12')->getValue());
// Read styles, including row height, not yet implemented for ODS
self::assertSame('j15', $sheet->getCell('J15')->getValue());
self::assertSame('j16', $sheet->getCell('J16')->getValue());
self::assertSame(['E15:G16' => 'E15:G16'], $sheet->getMergeCells());
self::assertSame('lastrow', $sheet->getCell('A19')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
+44 -1
View File
@@ -1,2 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<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-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-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-row>
</table:table>
<table:named-expressions/>
</office:spreadsheet>
</office:body>
</office:document-content>
-4
View File
@@ -17,10 +17,6 @@
<table:calculation-settings />
<table:table table:name="Worksheet" table:style-name="ta1">
<office:forms />
<table:table-row>
<table:table-cell table:style-name="ce0" />
<table:table-cell table:number-columns-repeated="1023" />
</table:table-row>
</table:table>
<table:named-expressions />
</office:spreadsheet>
@@ -24,7 +24,6 @@
<table:table-cell office:value="1" office:value-type="float" table:style-name="ce0">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023"/>
</table:table-row>
</table:table>
<table:table table:name="New Worksheet" table:style-name="ta2">
@@ -33,7 +32,6 @@
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">
<text:p>2</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023" />
</table:table-row>
</table:table>
<table:named-expressions />
@@ -92,7 +92,6 @@
<table:table-cell office:value-type="string" table:style-name="ce0">
<text:p>Lorem ipsum</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1017"/>
</table:table-row>
<table:table-row>
<table:table-cell office:boolean-value="true" office:value-type="boolean" table:style-name="ce0">
@@ -107,10 +106,6 @@
<table:table-cell office:value="42798.572060185" office:value-type="float" table:style-name="ce1">
<text:p>42798.572060185</text:p>
</table:table-cell>
<table:table-cell table:style-name="ce0"/>
<table:table-cell table:style-name="ce0"/>
<table:table-cell table:style-name="ce0"/>
<table:table-cell table:number-columns-repeated="1017"/>
</table:table-row>
</table:table>
<table:table table:name="New Worksheet" table:style-name="ta2">
@@ -119,7 +114,6 @@
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">
<text:p>2</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023"/>
</table:table-row>
</table:table>
<table:named-expressions/>