diff --git a/src/Spout/Writer/XLSX/Helper/StyleHelper.php b/src/Spout/Writer/XLSX/Helper/StyleHelper.php index e01f7ac..60dcd8d 100644 --- a/src/Spout/Writer/XLSX/Helper/StyleHelper.php +++ b/src/Spout/Writer/XLSX/Helper/StyleHelper.php @@ -69,23 +69,30 @@ class StyleHelper extends AbstractStyleHelper // so $backgroundColor is a scalar value (RGB Color) $backgroundColor = $style->getBackgroundColor(); - // We need to track the already registered background definitions - if (isset($backgroundColor) && !isset($this->registeredFills[$backgroundColor])) { - $this->registeredFills[$backgroundColor] = $styleId; - } + if ($backgroundColor) { + $isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]); + + // We need to track the already registered background definitions + if ($isBackgroundColorRegistered) { + $registeredStyleId = $this->registeredFills[$backgroundColor]; + $registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId]; + $this->styleIdToFillMappingTable[$styleId] = $registeredFillId; + } else { + $this->registeredFills[$backgroundColor] = $styleId; + $this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++; + } - if (!isset($this->styleIdToFillMappingTable[$styleId])) { + } else { // The fillId maps a style to a fill declaration // When there is no background color definition - we default to 0 - $fillId = $backgroundColor !== null ? $this->fillIndex++ : 0; - $this->styleIdToFillMappingTable[$styleId] = $fillId; + $this->styleIdToFillMappingTable[$styleId] = 0; } } /** * Register a border definition * - * @param $style + * @param \Box\Spout\Writer\Style\Style $style */ protected function registerBorder($style) { diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index cd209df..e18ac98 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -261,6 +261,45 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->assertEquals(2, (int)$customFillId, 'The custom fill id should have the index 2'); } + /** + * @return void + */ + public function testReuseBackgroundColorSharedDefinition() + { + $fileName = 'test_add_background_color_shared_definition.xlsx'; + $dataRows = [ + ["row-bold-background-red"], + ["row-background-red"], + ]; + + $styles = [ + (new StyleBuilder())->setBackgroundColor(Color::RED)->setFontBold()->build(), + (new StyleBuilder())->setBackgroundColor(Color::RED)->build() + ]; + + $this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles); + + $fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills'); + $this->assertEquals( + 3, + $fillsDomElement->getAttribute('count'), + 'There should be 3 fills, including the 2 default ones' + ); + + $styleXfsElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs'); + $this->assertEquals( + 3, + $styleXfsElements->getAttribute('count'), + '3 cell xfs present - a default one and two custom ones' + ); + + $firstCustomId = $styleXfsElements->childNodes->item(1)->getAttribute('fillId'); + $this->assertEquals(2, (int)$firstCustomId, 'The first custom fill id should have the index 2'); + + $secondCustomId = $styleXfsElements->childNodes->item(2)->getAttribute('fillId'); + $this->assertEquals(2, (int)$secondCustomId, 'The second custom fill id should have the index 2'); + } + /** * @return void */