Skip to content

Commit

Permalink
Writer ODS : Write Border Style for cells
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 29, 2023
1 parent 4c891c0 commit 1c1f029
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

- Split screens (Xlsx and Xml only, not 100% complete). [Issue #3601](https://github.com/PHPOffice/PhpSpreadsheet/issues/3601) [PR #3622](https://github.com/PHPOffice/PhpSpreadsheet/pull/3622)
- Permit Meta Viewport in Html. [Issue #3565](https://github.com/PHPOffice/PhpSpreadsheet/issues/3565) [PR #3623](https://github.com/PHPOffice/PhpSpreadsheet/pull/3623)
- Writer ODS : Write Border Style for cells [Issue #3690](https://github.com/PHPOffice/PhpSpreadsheet/issues/3690) [PR #3693](https://github.com/PHPOffice/PhpSpreadsheet/pull/3693)

### Changed

Expand Down
81 changes: 81 additions & 0 deletions src/PhpSpreadsheet/Writer/Ods/Cell/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use PhpOffice\PhpSpreadsheet\Helper\Dimension;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle;
Expand Down Expand Up @@ -78,6 +80,82 @@ private function writeFillStyle(Fill $fill): void
}
}

private function writeBordersStyle(Borders $borders): void
{
$this->writeBorderStyle('bottom', $borders->getBottom());
$this->writeBorderStyle('left', $borders->getLeft());
$this->writeBorderStyle('right', $borders->getRight());
$this->writeBorderStyle('top', $borders->getTop());
}

private function writeBorderStyle(string $direction, Border $border): void
{
if ($border->getBorderStyle() === Border::BORDER_NONE) {
$value = 'none';
} else {
$value = sprintf(
'%s %s #%s',
$this->mapBorderWidth($border),
$this->mapBorderStyle($border),
$border->getColor()->getRGB(),
);
}

$this->writer->writeAttribute('fo:border-' . $direction, $value);
}

protected function mapBorderWidth(Border $border): string
{
switch ($border->getBorderStyle()) {
case Border::BORDER_THIN:
case Border::BORDER_DASHED:
case Border::BORDER_DASHDOT:
case Border::BORDER_DASHDOTDOT:
case Border::BORDER_DOTTED:
case Border::BORDER_HAIR:
return '0.75pt';
case Border::BORDER_MEDIUM:
case Border::BORDER_MEDIUMDASHED:
case Border::BORDER_MEDIUMDASHDOT:
case Border::BORDER_MEDIUMDASHDOTDOT:
case Border::BORDER_SLANTDASHDOT:
return '1.75pt';
case Border::BORDER_DOUBLE:
case Border::BORDER_THICK:
return '2.5pt';
}

return '1pt';
}

protected function mapBorderStyle(Border $border): string
{
switch ($border->getBorderStyle()) {
case Border::BORDER_DOTTED:
case Border::BORDER_MEDIUMDASHDOTDOT:
return Border::BORDER_DOTTED;

case Border::BORDER_DASHED:
case Border::BORDER_DASHDOT:
case Border::BORDER_DASHDOTDOT:
case Border::BORDER_MEDIUMDASHDOT:
case Border::BORDER_MEDIUMDASHED:
case Border::BORDER_SLANTDASHDOT:
return Border::BORDER_DASHED;

case Border::BORDER_DOUBLE:
return Border::BORDER_DOUBLE;

case Border::BORDER_HAIR:
case Border::BORDER_MEDIUM:
case Border::BORDER_THICK:
case Border::BORDER_THIN:
return 'solid';
}

return 'solid';
}

private function writeCellProperties(CellStyle $style): void
{
// Align
Expand All @@ -100,6 +178,9 @@ private function writeCellProperties(CellStyle $style): void
// Fill
$this->writeFillStyle($style->getFill());

// Border
$this->writeBordersStyle($style->getBorders());

$this->writer->endElement();

if (!empty($hAlign)) {
Expand Down

0 comments on commit 1c1f029

Please sign in to comment.