Default row style should only apply to existing cells

This commit is contained in:
Adrien Loison 2017-11-05 11:53:32 +01:00
parent 727a90fd06
commit ca5962271e
5 changed files with 48 additions and 17 deletions

View File

@ -49,11 +49,12 @@ class EntityFactory
/** /**
* @param mixed $cellValue * @param mixed $cellValue
* @param Style|null $cellStyle
* @return Cell * @return Cell
*/ */
public static function createCell($cellValue) public static function createCell($cellValue, Style $cellStyle = null)
{ {
return new Cell($cellValue); return new Cell($cellValue, $cellStyle);
} }
/** /**
@ -66,14 +67,14 @@ class EntityFactory
/** /**
* @param array $cells * @param array $cells
* @param Style|null $style * @param Style|null $rowStyle
* @return Row * @return Row
*/ */
public static function createRow(array $cells, Style $style = null) public static function createRow(array $cells = [], Style $rowStyle = null)
{ {
$styleMerger = new StyleMerger(); $styleMerger = new StyleMerger();
$rowManager = new RowManager($styleMerger); $rowManager = new RowManager($styleMerger);
return new Row($cells, $style, $rowManager); return new Row($cells, $rowStyle, $rowManager);
} }
} }

View File

@ -153,8 +153,8 @@ class WorksheetManager implements WorksheetManagerInterface
{ {
// Apply styles - the row style is merged at this point // Apply styles - the row style is merged at this point
$cell->applyStyle($rowStyle); $cell->applyStyle($rowStyle);
$this->styleManager->applyExtraStylesIfNeeded($cell); $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle()); $registeredStyle = $this->styleManager->registerStyle($newCellStyle);
$styleIndex = $registeredStyle->getId() + 1; // 1-based $styleIndex = $registeredStyle->getId() + 1; // 1-based
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); $numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);

View File

@ -227,17 +227,15 @@ abstract class WriterAbstract implements WriterInterface
* @TODO: Move this into styleMerger * @TODO: Move this into styleMerger
* *
* @param Row $row * @param Row $row
* @return $this
*/ */
private function applyDefaultRowStyle(Row $row) private function applyDefaultRowStyle(Row $row)
{ {
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
if ($defaultRowStyle === null) {
return $this;
}
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle); if ($defaultRowStyle !== null) {
$row->setStyle($mergedStyle); $mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
$row->setStyle($mergedStyle);
}
} }
/** /**

View File

@ -204,8 +204,8 @@ EOD;
foreach ($registeredStyles as $style) { foreach ($registeredStyles as $style) {
$styleId = $style->getId(); $styleId = $style->getId();
$fillId = $this->styleRegistry->getFillIdForStyleId($styleId); $fillId = $this->getFillIdForStyleId($styleId);
$borderId = $this->styleRegistry->getBorderIdForStyleId($styleId); $borderId = $this->getBorderIdForStyleId($styleId);
$content .= '<xf numFmtId="0" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"'; $content .= '<xf numFmtId="0" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
@ -229,6 +229,38 @@ EOD;
return $content; return $content;
} }
/**
* Returns the fill ID associated to the given style ID.
* For the default style, we don't a fill.
*
* @param int $styleId
* @return int
*/
private function getFillIdForStyleId($styleId)
{
// For the default style (ID = 0), we don't want to override the fill.
// Otherwise all cells of the spreadsheet will have a background color.
$isDefaultStyle = ($styleId === 0);
return $isDefaultStyle ? 0 : ($this->styleRegistry->getFillIdForStyleId($styleId) ?: 0);
}
/**
* Returns the fill ID associated to the given style ID.
* For the default style, we don't a border.
*
* @param int $styleId
* @return int
*/
private function getBorderIdForStyleId($styleId)
{
// For the default style (ID = 0), we don't want to override the border.
// Otherwise all cells of the spreadsheet will have a border.
$isDefaultStyle = ($styleId === 0);
return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0);
}
/** /**
* Returns the content of the "<cellStyles>" section. * Returns the content of the "<cellStyles>" section.
* *

View File

@ -174,8 +174,8 @@ EOD;
{ {
// Apply styles - the row style is merged at this point // Apply styles - the row style is merged at this point
$cell->applyStyle($rowStyle); $cell->applyStyle($rowStyle);
$this->styleManager->applyExtraStylesIfNeeded($cell); $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle()); $registeredStyle = $this->styleManager->registerStyle($newCellStyle);
return $this->getCellXML($rowIndex, $cellIndex, $cell, $registeredStyle->getId()); return $this->getCellXML($rowIndex, $cellIndex, $cell, $registeredStyle->getId());
} }