Add support for custom column widths in ODS exports

This commit is contained in:
Alexander Hofstede 2019-12-19 22:35:55 +01:00
parent ceda150aa3
commit db197de7f9
3 changed files with 127 additions and 1 deletions

View File

@ -188,6 +188,15 @@ EOD;
EOD;
}
// Sort column widths since ODS cares about order
usort($this->columnWidths, function($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});
$content .= $this->getTableColumnStylesXMLContent();
$content .= '</office:automatic-styles>';
return $content;
@ -340,4 +349,40 @@ EOD;
$style->getBackgroundColor()
);
}
public function getTableColumnStylesXMLContent(): string
{
if (empty($this->columnWidths)) {
return '';
}
$content = '';
foreach ($this->columnWidths as $styleIndex => $entry) {
$content .= <<<EOD
<style:style style:family="table-column" style:name="co{$styleIndex}">
<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="{$entry[2]}pt"/>
</style:style>
EOD;
}
return $content;
}
public function getStyledTableColumnXMLContent(int $maxNumColumns): string
{
if (empty($this->columnWidths)) {
return '';
}
$content = '';
foreach ($this->columnWidths as $styleIndex => $entry) {
$numCols = $entry[1] - $entry[0] + 1;
$content .= <<<EOD
<table:table-column table:default-cell-style-name='Default' table:style-name="co{$styleIndex}" table:number-columns-repeated="{$numCols}"/>
EOD;
}
// Note: This assumes the column widths are contiguous and default width is
// only applied to columns after the last custom column with a custom width
$content .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="default-column-style" table:number-columns-repeated="' . ($maxNumColumns - $entry[1]) . '"/>';
return $content;
}
}

View File

@ -103,7 +103,7 @@ class WorksheetManager implements WorksheetManagerInterface
$tableStyleName = 'ta' . ($externalSheet->getIndex() + 1);
$tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">';
$tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="default-column-style" table:number-columns-repeated="' . $worksheet->getMaxNumColumns() . '"/>';
$tableElement .= $this->styleManager->getStyledTableColumnXMLContent($worksheet->getMaxNumColumns());
return $tableElement;
}

View File

@ -127,6 +127,87 @@ class SheetTest extends TestCase
$this->assertContains(' style:use-optimal-row-height="false', $xmlContents, 'No optimal row height override found in sheet');
}
public function testWritesColumnWidths()
{
$fileName = 'test_column_widths.ods';
$writer = $this->writerForFile($fileName);
$writer->setColumnWidth(100.0, 1);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
$writer->close();
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style definition found in sheet');
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
$this->assertContains('table:number-columns-repeated="1"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
}
public function testWritesMultipleColumnWidths()
{
$fileName = 'test_multiple_column_widths.ods';
$writer = $this->writerForFile($fileName);
$writer->setColumnWidth(100.0, 1, 2, 3);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13']));
$writer->close();
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style definition found in sheet');
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
$this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
}
public function testWritesMultipleColumnWidthsInRanges()
{
$fileName = 'test_multiple_column_widths_in_ranges.ods';
$writer = $this->writerForFile($fileName);
$writer->setColumnWidth(50.0, 1, 3, 4, 6);
$writer->setColumnWidth(100.0, 2, 5);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13', 'ods--14', 'ods--15', 'ods--16']));
$writer->close();
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style 0 definition found in sheet');
$this->assertContains('<style:style style:family="table-column" style:name="co1">', $xmlContents, 'No matching custom col style 1 definition found in sheet');
$this->assertContains('<style:style style:family="table-column" style:name="co2">', $xmlContents, 'No matching custom col style 2 definition found in sheet');
$this->assertContains('<style:style style:family="table-column" style:name="co3">', $xmlContents, 'No matching custom col style 3 definition found in sheet');
$this->assertContains('<style:style style:family="table-column" style:name="co4">', $xmlContents, 'No matching custom col style 4 definition found in sheet');
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="50pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
$this->assertContains('<table:table-column table:default-cell-style-name=\'Default\' table:style-name="co0" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co1" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co2" table:number-columns-repeated="2"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co3" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co4" table:number-columns-repeated="1"/>', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
}
public function testCanTakeColumnWidthsAsRange()
{
$fileName = 'test_column_widths_as_ranges.ods';
$writer = $this->writerForFile($fileName);
$writer->setColumnWidthForRange(150.0, 1, 3);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13']));
$writer->close();
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style 0 definition found in sheet');
$this->assertContains('style:column-width="150pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
$this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
}
private function writerForFile($fileName)
{
$this->createGeneratedFolderIfNeeded($fileName);