From c94694cb60412844389d85864e2305845325954d Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 16 Aug 2016 21:18:44 -0700 Subject: [PATCH] API to set default row style (#290) --- README.md | 13 ++++++ src/Spout/Writer/AbstractWriter.php | 15 +++++++ .../Spout/Writer/ODS/WriterWithStyleTest.php | 42 +++++++++++++++++- .../Spout/Writer/XLSX/WriterWithStyleTest.php | 44 +++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e48a176..7c77f44 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,19 @@ $writer->addRowWithStyle(['Border Bottom Green Thin Dashed'], $style); $writer->close(); ``` +Spout will use a default style for all created rows. This style can be overridden this way: + +```php +$defaultStyle = (new StyleBuilder()) + ->setFontName('Arial') + ->setFontSize(11) + ->build(); + +$writer = WriterFactory::create(Type::XLSX); +$writer->setDefaultRowStyle($defaultStyle) + ->openToFile($filePath); +``` + Unfortunately, Spout does not support all the possible formatting options yet. But you can find the most important ones: Category | Property | API diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 46f47cc..49939ee 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -71,6 +71,21 @@ abstract class AbstractWriter implements WriterInterface $this->resetRowStyleToDefault(); } + /** + * Sets the default styles for all rows added with "addRow". + * Overriding the default style instead of using "addRowWithStyle" improves performance by 20%. + * @see https://github.com/box/spout/issues/272 + * + * @param Style\Style $defaultStyle + * @return AbstractWriter + */ + public function setDefaultRowStyle($defaultStyle) + { + $this->defaultRowStyle = $defaultStyle; + $this->resetRowStyleToDefault(); + return $this; + } + /** * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper * @return AbstractWriter diff --git a/tests/Spout/Writer/ODS/WriterWithStyleTest.php b/tests/Spout/Writer/ODS/WriterWithStyleTest.php index 157b7f0..f6af4a1 100644 --- a/tests/Spout/Writer/ODS/WriterWithStyleTest.php +++ b/tests/Spout/Writer/ODS/WriterWithStyleTest.php @@ -326,6 +326,22 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedThird, $actualThird); } + /** + * @return void + */ + public function testSetDefaultRowStyle() + { + $fileName = 'test_set_default_row_style.ods'; + $dataRows = [['ods--11']]; + + $defaultFontSize = 50; + $defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build(); + + $this->writeToODSFileWithDefaultStyle($dataRows, $fileName, $defaultStyle); + + $textPropertiesElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'style:text-properties'); + $this->assertEquals($defaultFontSize . 'pt', $textPropertiesElement->getAttribute('fo:font-size')); + } /** * @param array $allRows @@ -348,6 +364,28 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase return $writer; } + /** + * @param array $allRows + * @param string $fileName + * @param \Box\Spout\Writer\Style\Style|null $defaultStyle + * @return Writer + */ + private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle) + { + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::ODS); + $writer->setDefaultRowStyle($defaultStyle); + + $writer->openToFile($resourcePath); + $writer->addRows($allRows); + $writer->close(); + + return $writer; + } + /** * @param array $allRows * @param string $fileName @@ -398,7 +436,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase } $xmlReader->close(); - + return $cellElements; } @@ -422,7 +460,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase } $xmlReader->close(); - + return $cellStyleElements; } diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index 19a11ad..47a3c22 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -338,6 +338,27 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase }; } + /** + * @return void + */ + public function testSetDefaultRowStyle() + { + $fileName = 'test_set_default_row_style.xlsx'; + $dataRows = [['xlsx--11']]; + + $defaultFontSize = 50; + $defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build(); + + $this->writeToXLSXFileWithDefaultStyle($dataRows, $fileName, $defaultStyle); + + $fontsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fonts'); + $fontElements = $fontsDomElement->getElementsByTagName('font'); + $this->assertEquals(1, $fontElements->length, 'There should only be the default font.'); + + $defaultFontElement = $fontElements->item(0); + $this->assertFirstChildHasAttributeEquals((string) $defaultFontSize, $defaultFontElement, 'sz', 'val'); + } + /** * @param array $allRows * @param string $fileName @@ -360,6 +381,29 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase return $writer; } + /** + * @param array $allRows + * @param string $fileName + * @param \Box\Spout\Writer\Style\Style|null $defaultStyle + * @return Writer + */ + private function writeToXLSXFileWithDefaultStyle($allRows, $fileName, $defaultStyle) + { + $this->createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + /** @var \Box\Spout\Writer\XLSX\Writer $writer */ + $writer = WriterFactory::create(Type::XLSX); + $writer->setShouldUseInlineStrings(true); + $writer->setDefaultRowStyle($defaultStyle); + + $writer->openToFile($resourcePath); + $writer->addRows($allRows); + $writer->close(); + + return $writer; + } + /** * @param array $allRows * @param string $fileName