Support for style per column for XLSX and ODS
This commit is contained in:
parent
45980195cd
commit
9320a10e8b
@ -209,11 +209,15 @@ abstract class AbstractWriter implements WriterInterface
|
||||
*/
|
||||
public function addRowWithStyle(array $dataRow, $style)
|
||||
{
|
||||
if (!$style instanceof Style\Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
$styles = is_array($style) ? $style : [$style];
|
||||
|
||||
foreach ($styles as $style) {
|
||||
if (!$style instanceof Style\Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
}
|
||||
}
|
||||
|
||||
$this->setRowStyle($style);
|
||||
$this->setRowStyle($styles);
|
||||
$this->addRow($dataRow);
|
||||
$this->resetRowStyleToDefault();
|
||||
|
||||
@ -230,6 +234,7 @@ abstract class AbstractWriter implements WriterInterface
|
||||
* ['data11', 12, , '', 'data13'],
|
||||
* ['data21', 'data22', null, false],
|
||||
* ];
|
||||
*
|
||||
* @return AbstractWriter
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
|
||||
@ -264,11 +269,15 @@ abstract class AbstractWriter implements WriterInterface
|
||||
*/
|
||||
public function addRowsWithStyle(array $dataRows, $style)
|
||||
{
|
||||
if (!$style instanceof Style\Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
$styles = is_array($style) ? $style : [$style];
|
||||
|
||||
foreach ($styles as $style) {
|
||||
if (!$style instanceof Style\Style) {
|
||||
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
|
||||
}
|
||||
}
|
||||
|
||||
$this->setRowStyle($style);
|
||||
$this->setRowStyle($styles);
|
||||
$this->addRows($dataRows);
|
||||
$this->resetRowStyleToDefault();
|
||||
|
||||
@ -296,7 +305,11 @@ abstract class AbstractWriter implements WriterInterface
|
||||
private function setRowStyle($style)
|
||||
{
|
||||
// Merge given style with the default one to inherit custom properties
|
||||
$this->rowStyle = $style->mergeWith($this->defaultRowStyle);
|
||||
$this->rowStyle = [];
|
||||
$styles = is_array($style) ? $style : [$style];
|
||||
foreach($styles as $style) {
|
||||
$this->rowStyle[] = $style->mergeWith($this->defaultRowStyle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,17 +34,22 @@ abstract class AbstractStyleHelper
|
||||
*/
|
||||
public function registerStyle($style)
|
||||
{
|
||||
$serializedStyle = $style->serialize();
|
||||
$return = [];
|
||||
$styles = is_array($style) ? $style : [$style];
|
||||
|
||||
if (!$this->hasStyleAlreadyBeenRegistered($style)) {
|
||||
$nextStyleId = count($this->serializedStyleToStyleIdMappingTable);
|
||||
$style->setId($nextStyleId);
|
||||
foreach ($styles as $style) {
|
||||
$serializedStyle = $style->serialize();
|
||||
|
||||
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
|
||||
$this->styleIdToStyleMappingTable[$nextStyleId] = $style;
|
||||
if (!$this->hasStyleAlreadyBeenRegistered($style)) {
|
||||
$nextStyleId = count($this->serializedStyleToStyleIdMappingTable);
|
||||
$style->setId($nextStyleId);
|
||||
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
|
||||
$this->styleIdToStyleMappingTable[$nextStyleId] = $style;
|
||||
}
|
||||
$return[] = $this->getStyleFromSerializedStyle($serializedStyle);
|
||||
}
|
||||
|
||||
return $this->getStyleFromSerializedStyle($serializedStyle);
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -120,13 +125,19 @@ abstract class AbstractStyleHelper
|
||||
protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
|
||||
{
|
||||
// if the "wrap text" option is already set, no-op
|
||||
if ($style->shouldWrapText()) {
|
||||
if (!is_array($style) && $style->shouldWrapText()) {
|
||||
return $style;
|
||||
}
|
||||
|
||||
foreach ($dataRow as $cell) {
|
||||
foreach ($dataRow as $index => $cell) {
|
||||
if (is_string($cell) && strpos($cell, "\n") !== false) {
|
||||
$style->setShouldWrapText();
|
||||
// if the "wrap text" option is already set, no-op
|
||||
if (!is_array($style)) {
|
||||
$style->setShouldWrapText();
|
||||
break;
|
||||
} else if (!$style[$index]->shouldWrapText()) {
|
||||
$style[$index]->setShouldWrapText();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -24,8 +24,11 @@ class StyleHelper extends AbstractStyleHelper
|
||||
*/
|
||||
public function registerStyle($style)
|
||||
{
|
||||
$this->usedFontsSet[$style->getFontName()] = true;
|
||||
return parent::registerStyle($style);
|
||||
$styles = is_array($style) ? $style : [$style];
|
||||
foreach ($styles as $style ) {
|
||||
$this->usedFontsSet[$style->getFontName()] = true;
|
||||
}
|
||||
return parent::registerStyle($styles);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,7 +134,9 @@ class Worksheet implements WorksheetInterface
|
||||
*/
|
||||
public function addRow($dataRow, $style)
|
||||
{
|
||||
$styleIndex = ($style->getId() + 1); // 1-based
|
||||
if (!is_array($style)) {
|
||||
$styleIndex = ($style->getId() + 1); // 1-based
|
||||
}
|
||||
$cellsCount = count($dataRow);
|
||||
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
|
||||
|
||||
@ -146,6 +148,10 @@ class Worksheet implements WorksheetInterface
|
||||
for ($i = 0; $i < $cellsCount; $i++) {
|
||||
$currentCellValue = $dataRow[$currentCellIndex];
|
||||
|
||||
if (is_array($style)) {
|
||||
$styleIndex = 1 + (isset($style[$i]) ? $style[$i]->getId() : $style[0]->getId());
|
||||
}
|
||||
|
||||
if (!array_key_exists($nextCellIndex, $dataRow) || $currentCellValue !== $dataRow[$nextCellIndex]) {
|
||||
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
||||
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
||||
|
@ -133,10 +133,16 @@ EOD;
|
||||
|
||||
$data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
||||
|
||||
foreach($dataRow as $cellValue) {
|
||||
if (!is_array($style)) {
|
||||
$style = [$style];
|
||||
}
|
||||
|
||||
foreach($dataRow as $index => $cellValue) {
|
||||
$styleIndex = isset($style[$index]) ? $style[$index]->getId() : $style[0]->getId();
|
||||
|
||||
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
|
||||
$data .= '<c r="' . $columnIndex . $rowIndex . '"';
|
||||
$data .= ' s="' . $style->getId() . '"';
|
||||
$data .= ' s="' . $styleIndex . '"';
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
if ($this->shouldUseInlineStrings) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user