Cell alignment for ods Writer (#1819)

* Cell alignment for ods Writer
This commit is contained in:
Mark Baker 2021-01-31 23:39:37 +01:00 committed by GitHub
parent 8b94a37e23
commit 5f761b6274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 39 deletions

View File

@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Added ### Added
- Alignment for ODS Writer [#1796](https://github.com/PHPOffice/PhpSpreadsheet/issues/1796)
- CSV Reader - Best Guess for Encoding, and Handle Null-string Escape [#1647](https://github.com/PHPOffice/PhpSpreadsheet/issues/1647) - CSV Reader - Best Guess for Encoding, and Handle Null-string Escape [#1647](https://github.com/PHPOffice/PhpSpreadsheet/issues/1647)
### Changed ### Changed

View File

@ -7,6 +7,7 @@ use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Worksheet\Row; use PhpOffice\PhpSpreadsheet\Worksheet\Row;
@ -270,6 +271,38 @@ class Content extends WriterPart
} }
} }
private function mapHorizontalAlignment(string $horizontalAlignment): string
{
switch ($horizontalAlignment) {
case Alignment::HORIZONTAL_CENTER:
case Alignment::HORIZONTAL_CENTER_CONTINUOUS:
case Alignment::HORIZONTAL_DISTRIBUTED:
return 'center';
case Alignment::HORIZONTAL_RIGHT:
return 'end';
case Alignment::HORIZONTAL_FILL:
case Alignment::HORIZONTAL_JUSTIFY:
return 'justify';
}
return 'start';
}
private function mapVerticalAlignment(string $verticalAlignment): string
{
switch ($verticalAlignment) {
case Alignment::VERTICAL_TOP:
return 'top';
case Alignment::VERTICAL_CENTER:
return 'middle';
case Alignment::VERTICAL_DISTRIBUTED:
case Alignment::VERTICAL_JUSTIFY:
return 'automatic';
}
return 'bottom';
}
/** /**
* Write XF cell styles. * Write XF cell styles.
*/ */
@ -281,6 +314,51 @@ class Content extends WriterPart
$writer->writeAttribute('style:family', 'table-cell'); $writer->writeAttribute('style:family', 'table-cell');
$writer->writeAttribute('style:parent-style-name', 'Default'); $writer->writeAttribute('style:parent-style-name', 'Default');
// Align
$hAlign = $style->getAlignment()->getHorizontal();
$vAlign = $style->getAlignment()->getVertical();
$wrap = $style->getAlignment()->getWrapText();
$writer->startElement('style:table-cell-properties');
if (!empty($vAlign) || $wrap) {
if (!empty($vAlign)) {
$vAlign = $this->mapVerticalAlignment($vAlign);
$writer->writeAttribute('style:vertical-align', $vAlign);
}
if ($wrap) {
$writer->writeAttribute('fo:wrap-option', 'wrap');
}
}
$writer->writeAttribute('style:rotation-align', 'none');
// Fill
if ($fill = $style->getFill()) {
switch ($fill->getFillType()) {
case Fill::FILL_SOLID:
$writer->writeAttribute('fo:background-color', sprintf(
'#%s',
strtolower($fill->getStartColor()->getRGB())
));
break;
case Fill::FILL_GRADIENT_LINEAR:
case Fill::FILL_GRADIENT_PATH:
/// TODO :: To be implemented
break;
case Fill::FILL_NONE:
default:
}
}
$writer->endElement();
if (!empty($hAlign)) {
$hAlign = $this->mapHorizontalAlignment($hAlign);
$writer->startElement('style:paragraph-properties');
$writer->writeAttribute('fo:text-align', $hAlign);
$writer->endElement();
}
// style:text-properties // style:text-properties
// Font // Font
@ -329,34 +407,7 @@ class Content extends WriterPart
$writer->endElement(); // Close style:text-properties $writer->endElement(); // Close style:text-properties
// style:table-cell-properties
$writer->startElement('style:table-cell-properties');
$writer->writeAttribute('style:rotation-align', 'none');
// Fill
if ($fill = $style->getFill()) {
switch ($fill->getFillType()) {
case Fill::FILL_SOLID:
$writer->writeAttribute('fo:background-color', sprintf(
'#%s',
strtolower($fill->getStartColor()->getRGB())
));
break;
case Fill::FILL_GRADIENT_LINEAR:
case Fill::FILL_GRADIENT_PATH:
/// TODO :: To be implemented
break;
case Fill::FILL_NONE:
default:
}
}
$writer->endElement(); // Close style:table-cell-properties
// End // End
$writer->endElement(); // Close style:style $writer->endElement(); // Close style:style
} }
} }

View File

@ -4,8 +4,9 @@
<office:font-face-decls /> <office:font-face-decls />
<office:automatic-styles> <office:automatic-styles>
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default"> <style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" /> <style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" />
<style:table-cell-properties style:rotation-align="none" />
</style:style> </style:style>
</office:automatic-styles> </office:automatic-styles>
<office:body> <office:body>

View File

@ -4,48 +4,59 @@
<office:font-face-decls/> <office:font-face-decls/>
<office:automatic-styles> <office:automatic-styles>
<style:style style:family="table-cell" style:name="ce0" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce0" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/> <style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce1" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce1" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/> <style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce2" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce2" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties style:font-weight-asian="bold" style:font-weight-complex="bold" fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" fo:font-weight="bold"/> <style:text-properties style:font-weight-asian="bold" style:font-weight-complex="bold" fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" fo:font-weight="bold"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce3" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce3" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" fo:font-style="italic"/> <style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" fo:font-style="italic"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce4" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce4" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Courier" fo:font-size="11.0pt"/> <style:text-properties fo:color="#000000" fo:font-family="Courier" fo:font-size="11.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce5" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce5" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Courier" fo:font-size="14.0pt"/> <style:text-properties fo:color="#000000" fo:font-family="Courier" fo:font-size="14.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce6" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce6" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/> <style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce7" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce7" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" fo:background-color="#ffffff"/>
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/> <style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/>
<style:table-cell-properties style:rotation-align="none" fo:background-color="#ffffff"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce8" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce8" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" fo:background-color="#ff0000"/>
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/> <style:text-properties fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/>
<style:table-cell-properties style:rotation-align="none" fo:background-color="#ff0000"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce9" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce9" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" fo:background-color="#ff0000"/>
<style:paragraph-properties fo:text-align="start" />
<style:text-properties style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-type="single" style:text-underline-width="auto" fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/> <style:text-properties style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-type="single" style:text-underline-width="auto" fo:color="#0000FF" fo:font-family="Courier" fo:font-size="14.0pt"/>
<style:table-cell-properties style:rotation-align="none" fo:background-color="#ff0000"/>
</style:style> </style:style>
<style:style style:family="table-cell" style:name="ce10" style:parent-style-name="Default"> <style:style style:family="table-cell" style:name="ce10" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-type="double" style:text-underline-width="auto" fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/> <style:text-properties style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-type="double" style:text-underline-width="auto" fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt"/>
<style:table-cell-properties style:rotation-align="none"/>
</style:style> </style:style>
</office:automatic-styles> </office:automatic-styles>
<office:body> <office:body>