This commit is contained in:
madflow 2016-08-30 19:56:52 +02:00 committed by Adrien Loison
parent ff2d54cc8d
commit 277c353984
2 changed files with 54 additions and 8 deletions

View File

@ -69,23 +69,30 @@ class StyleHelper extends AbstractStyleHelper
// so $backgroundColor is a scalar value (RGB Color) // so $backgroundColor is a scalar value (RGB Color)
$backgroundColor = $style->getBackgroundColor(); $backgroundColor = $style->getBackgroundColor();
if ($backgroundColor) {
$isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]);
// We need to track the already registered background definitions // We need to track the already registered background definitions
if (isset($backgroundColor) && !isset($this->registeredFills[$backgroundColor])) { if ($isBackgroundColorRegistered) {
$registeredStyleId = $this->registeredFills[$backgroundColor];
$registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId];
$this->styleIdToFillMappingTable[$styleId] = $registeredFillId;
} else {
$this->registeredFills[$backgroundColor] = $styleId; $this->registeredFills[$backgroundColor] = $styleId;
$this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++;
} }
if (!isset($this->styleIdToFillMappingTable[$styleId])) { } else {
// The fillId maps a style to a fill declaration // The fillId maps a style to a fill declaration
// When there is no background color definition - we default to 0 // When there is no background color definition - we default to 0
$fillId = $backgroundColor !== null ? $this->fillIndex++ : 0; $this->styleIdToFillMappingTable[$styleId] = 0;
$this->styleIdToFillMappingTable[$styleId] = $fillId;
} }
} }
/** /**
* Register a border definition * Register a border definition
* *
* @param $style * @param \Box\Spout\Writer\Style\Style $style
*/ */
protected function registerBorder($style) protected function registerBorder($style)
{ {

View File

@ -261,6 +261,45 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, (int)$customFillId, 'The custom fill id should have the index 2'); $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 * @return void
*/ */