Apply custom style to empty cells if needed (#307)

Fixes #295

If a row should be written with a custom style, the handling of empty cells should change.
Instead of being skipped entirely, empty cells will be applied the custom style, if this style has custom background color or borders.
If not, then the cell definition can still be skipped.
This commit is contained in:
Adrien Loison authored and GitHub committed 2016-09-07 17:04:31 -07:00
1 parent d4e57b1f0d
commit 3e0afd858f
6 files changed
+110 -15

No files matched your search

@@ -176,6 +176,53 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('0', $cellDomElements[2]->getAttribute('s'));
}
/**
* @return void
*/
public function testAddRowWithStyleShouldApplyStyleToEmptyCellsIfNeeded()
{
$fileName = 'test_add_row_with_style_should_apply_style_to_empty_cells_if_needed.xlsx';
$dataRows = [
['xlsx--11', '', 'xlsx--13'],
['xlsx--21', '', 'xlsx--23'],
['xlsx--31', '', 'xlsx--33'],
['xlsx--41', '', 'xlsx--43'],
];
$styleWithFont = (new StyleBuilder())->setFontBold()->build();
$styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->build();
$border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build();
$styleWithBorder = (new StyleBuilder())->setBorder($border)->build();
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [null, $styleWithFont, $styleWithBackground, $styleWithBorder]);
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
// The first and second rows should not have a reference to the empty cell
// The other rows should have the reference because style should be applied to them
// So that's: 2 + 2 + 3 + 3 = 10 cells
$this->assertEquals(10, count($cellDomElements));
// First row has 2 styled cells
$this->assertEquals('0', $cellDomElements[0]->getAttribute('s'));
$this->assertEquals('0', $cellDomElements[1]->getAttribute('s'));
// Second row has 2 styled cells
$this->assertEquals('1', $cellDomElements[2]->getAttribute('s'));
$this->assertEquals('1', $cellDomElements[3]->getAttribute('s'));
// Third row has 3 styled cells
$this->assertEquals('2', $cellDomElements[4]->getAttribute('s'));
$this->assertEquals('2', $cellDomElements[5]->getAttribute('s'));
$this->assertEquals('2', $cellDomElements[6]->getAttribute('s'));
// Third row has 3 styled cells
$this->assertEquals('3', $cellDomElements[7]->getAttribute('s'));
$this->assertEquals('3', $cellDomElements[8]->getAttribute('s'));
$this->assertEquals('3', $cellDomElements[9]->getAttribute('s'));
}
/**
* @return void
*/
@@ -403,17 +450,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
*/
public function testReUseBorders()
{
$fileName = 'test_reuse_borders.xlsx';
$borderLeft = (new BorderBuilder())
->setBorderLeft()
->build();
$borderLeft = (new BorderBuilder())->setBorderLeft()->build();
$borderLeftStyle = (new StyleBuilder())->setBorder($borderLeft)->build();
$borderRight = (new BorderBuilder())
->setBorderRight(Color::RED, Border::WIDTH_THICK)
->build();
$borderRight = (new BorderBuilder())->setBorderRight(Color::RED, Border::WIDTH_THICK)->build();
$borderRightStyle = (new StyleBuilder())->setBorder($borderRight)->build();
$fontStyle = (new StyleBuilder())->setFontBold()->build();
@@ -436,7 +478,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$borderRightStyle,
$borderRightFontBoldStyle
];
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');