diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index fa8218f..2658e5a 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -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); + } } /** diff --git a/src/Spout/Writer/Common/Helper/AbstractStyleHelper.php b/src/Spout/Writer/Common/Helper/AbstractStyleHelper.php index 70ee8c9..372ef62 100644 --- a/src/Spout/Writer/Common/Helper/AbstractStyleHelper.php +++ b/src/Spout/Writer/Common/Helper/AbstractStyleHelper.php @@ -34,17 +34,27 @@ abstract class AbstractStyleHelper */ public function registerStyle($style) { - $serializedStyle = $style->serialize(); - if (!$this->hasStyleAlreadyBeenRegistered($style)) { - $nextStyleId = count($this->serializedStyleToStyleIdMappingTable); - $style->setId($nextStyleId); + $styles = is_array($style) ? $style : [$style]; - $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; - $this->styleIdToStyleMappingTable[$nextStyleId] = $style; + foreach ($styles as $style) { + $serializedStyle = $style->serialize(); + + if (!$this->hasStyleAlreadyBeenRegistered($style)) { + $nextStyleId = count($this->serializedStyleToStyleIdMappingTable); + $style->setId($nextStyleId); + + $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; + $this->styleIdToStyleMappingTable[$nextStyleId] = $style; + } } - return $this->getStyleFromSerializedStyle($serializedStyle); + $return = []; + foreach ($this->serializedStyleToStyleIdMappingTable as $serializedStyle => $styleId) { + $return[$styleId] = $this->styleIdToStyleMappingTable[$styleId]; + } + + return $return; } /** @@ -120,13 +130,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; } } diff --git a/src/Spout/Writer/ODS/Helper/StyleHelper.php b/src/Spout/Writer/ODS/Helper/StyleHelper.php index f8b0c4d..30d723a 100644 --- a/src/Spout/Writer/ODS/Helper/StyleHelper.php +++ b/src/Spout/Writer/ODS/Helper/StyleHelper.php @@ -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); } /** diff --git a/src/Spout/Writer/ODS/Internal/Worksheet.php b/src/Spout/Writer/ODS/Internal/Worksheet.php index 19305f0..07c72b2 100644 --- a/src/Spout/Writer/ODS/Internal/Worksheet.php +++ b/src/Spout/Writer/ODS/Internal/Worksheet.php @@ -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); diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index be67e19..792faeb 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -133,10 +133,16 @@ EOD; $data = ''; - 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 .= 'getId() . '"'; + $data .= ' s="' . $styleIndex . '"'; if (CellHelper::isNonEmptyString($cellValue)) { if ($this->shouldUseInlineStrings) { diff --git a/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php b/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php index 763f904..4fd66fd 100644 --- a/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php +++ b/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php @@ -35,11 +35,11 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $this->assertNull($style2->getId()); $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style1); - $registeredStyle2 = $styleHelper->registerStyle($style2); + $registeredStyles = $styleHelper->registerStyle($style1); + $registeredStyles = $styleHelper->registerStyle($style2); - $this->assertEquals(1, $registeredStyle1->getId()); - $this->assertEquals(2, $registeredStyle2->getId()); + $this->assertEquals(1, $registeredStyles[1]->getId()); + $this->assertEquals(2, $registeredStyles[2]->getId()); } /** @@ -50,11 +50,12 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $style = (new StyleBuilder())->setFontBold()->build(); $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style); - $registeredStyle2 = $styleHelper->registerStyle($style); + $registeredStyles = $styleHelper->registerStyle($style); + $registeredStyles = $styleHelper->registerStyle($style); - $this->assertEquals(1, $registeredStyle1->getId()); - $this->assertEquals(1, $registeredStyle2->getId()); + $this->assertEquals(0, $registeredStyles[0]->getId()); + $this->assertEquals(1, $registeredStyles[1]->getId()); + $this->assertEquals(2, count($registeredStyles)); } /** diff --git a/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php b/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php index 333c1c2..806765a 100644 --- a/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php +++ b/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php @@ -35,11 +35,11 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $this->assertNull($style2->getId()); $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style1); - $registeredStyle2 = $styleHelper->registerStyle($style2); + $registeredStyles = $styleHelper->registerStyle($style1); + $registeredStyles = $styleHelper->registerStyle($style2); - $this->assertEquals(1, $registeredStyle1->getId()); - $this->assertEquals(2, $registeredStyle2->getId()); + $this->assertEquals(1, $registeredStyles[1]->getId()); + $this->assertEquals(2, $registeredStyles[2]->getId()); } /** @@ -50,11 +50,12 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $style = (new StyleBuilder())->setFontBold()->build(); $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style); - $registeredStyle2 = $styleHelper->registerStyle($style); + $registeredStyles = $styleHelper->registerStyle($style); + $registeredStyles = $styleHelper->registerStyle($style); - $this->assertEquals(1, $registeredStyle1->getId()); - $this->assertEquals(1, $registeredStyle2->getId()); + $this->assertEquals(0, $registeredStyles[0]->getId()); + $this->assertEquals(1, $registeredStyles[1]->getId()); + $this->assertEquals(2, count($registeredStyles)); } /**