API to set default row style (#290)
This commit is contained in:
parent
584121d478
commit
c94694cb60
13
README.md
13
README.md
@ -185,6 +185,19 @@ $writer->addRowWithStyle(['Border Bottom Green Thin Dashed'], $style);
|
|||||||
$writer->close();
|
$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:
|
Unfortunately, Spout does not support all the possible formatting options yet. But you can find the most important ones:
|
||||||
|
|
||||||
Category | Property | API
|
Category | Property | API
|
||||||
|
@ -71,6 +71,21 @@ abstract class AbstractWriter implements WriterInterface
|
|||||||
$this->resetRowStyleToDefault();
|
$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
|
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
||||||
* @return AbstractWriter
|
* @return AbstractWriter
|
||||||
|
@ -326,6 +326,22 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expectedThird, $actualThird);
|
$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
|
* @param array $allRows
|
||||||
@ -348,6 +364,28 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
|||||||
return $writer;
|
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 array $allRows
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
|
@ -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 array $allRows
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
@ -360,6 +381,29 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
|||||||
return $writer;
|
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 array $allRows
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
|
Loading…
x
Reference in New Issue
Block a user