Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writer ODS : Writer Border Style for cells #3693

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- ListWorksheetInfo/Names for Html/Csv/Slk. [Issue #3706](https://github.com/PHPOffice/PhpSpreadsheet/issues/3706) [PR #3709](https://github.com/PHPOffice/PhpSpreadsheet/pull/3709)
- Methods to determine if cell is actually locked, or hidden on formula bar. [PR #3722](https://github.com/PHPOffice/PhpSpreadsheet/pull/3722)
- Add iterateOnlyExistingCells to Constructors. [Issue #3721](https://github.com/PHPOffice/PhpSpreadsheet/issues/3721) [PR #3727](https://github.com/PHPOffice/PhpSpreadsheet/pull/3727)
- 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
79 changes: 79 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 @@ -65,6 +67,80 @@ 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) {
return;
}

$this->writer->writeAttribute('fo:border-' . $direction, sprintf(
'%s %s #%s',
$this->mapBorderWidth($border),
$this->mapBorderStyle($border),
$border->getColor()->getRGB(),
));
}

private 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';
}

private 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 @@ -87,6 +163,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
40 changes: 40 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods;

use DOMDocument;
use DOMXPath;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
Expand Down Expand Up @@ -124,4 +127,41 @@ public function testWriteWithHiddenWorksheet(): void

self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-hidden-worksheet.xml', $xml);
}

public function testWriteBorderStyle(): void
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->getStyle('A1:B2')->applyFromArray([
'borders' => [
'outline' => [
'borderStyle' => Border::BORDER_THICK,
'color' => ['argb' => 'AA22DD00'],
],
],
]);

$content = new Content(new Ods($spreadsheet));
$xml = $content->write();

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$xmlPath = new DOMXPath($xmlDoc);

foreach (['top', 'bottom'] as $keyRow => $row) {
foreach (['left', 'right'] as $keyCell => $cell) {
$styles = ['top' => '', 'bottom' => '', 'left' => '', 'right' => ''];
$styles[$row] = '2.5pt solid #22DD00';
$styles[$cell] = '2.5pt solid #22DD00';

$query = 'string(//office:document-content/office:body/office:spreadsheet/table:table/table:table-row[position()=' . ($keyRow + 1) . ']/table:table-cell[position()=' . ($keyCell + 1) . ']/@table:style-name)';
$idStyle = $xmlPath->evaluate($query);

foreach ($styles as $direction => $value) {
$query = 'string(//office:document-content/office:automatic-styles/style:style[@style:name="' . $idStyle . '"]/style:table-cell-properties/@fo:border-' . $direction . ')';
$style = $xmlPath->evaluate($query);
self::assertEquals($style, $value);
}
}
}
}
}