Add support for custom column widths in ODS exports
This commit is contained in:
1 parent
ceda150aa3
commit
db197de7f9
3 files changed
+127
-1
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user