Support for style per column for XLSX and ODS

This commit is contained in:
Jan 2015-10-09 13:31:07 +02:00
parent 45980195cd
commit 9320a10e8b
5 changed files with 61 additions and 22 deletions

View File

@ -209,11 +209,15 @@ abstract class AbstractWriter implements WriterInterface
*/ */
public function addRowWithStyle(array $dataRow, $style) public function addRowWithStyle(array $dataRow, $style)
{ {
if (!$style instanceof Style\Style) { $styles = is_array($style) ? $style : [$style];
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
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->addRow($dataRow);
$this->resetRowStyleToDefault(); $this->resetRowStyleToDefault();
@ -230,6 +234,7 @@ abstract class AbstractWriter implements WriterInterface
* ['data11', 12, , '', 'data13'], * ['data11', 12, , '', 'data13'],
* ['data21', 'data22', null, false], * ['data21', 'data22', null, false],
* ]; * ];
*
* @return AbstractWriter * @return AbstractWriter
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @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 * @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) public function addRowsWithStyle(array $dataRows, $style)
{ {
if (!$style instanceof Style\Style) { $styles = is_array($style) ? $style : [$style];
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
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->addRows($dataRows);
$this->resetRowStyleToDefault(); $this->resetRowStyleToDefault();
@ -296,7 +305,11 @@ abstract class AbstractWriter implements WriterInterface
private function setRowStyle($style) private function setRowStyle($style)
{ {
// Merge given style with the default one to inherit custom properties // 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);
}
} }
/** /**

View File

@ -34,17 +34,22 @@ abstract class AbstractStyleHelper
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
$serializedStyle = $style->serialize(); $return = [];
$styles = is_array($style) ? $style : [$style];
if (!$this->hasStyleAlreadyBeenRegistered($style)) { foreach ($styles as $style) {
$nextStyleId = count($this->serializedStyleToStyleIdMappingTable); $serializedStyle = $style->serialize();
$style->setId($nextStyleId);
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; if (!$this->hasStyleAlreadyBeenRegistered($style)) {
$this->styleIdToStyleMappingTable[$nextStyleId] = $style; $nextStyleId = count($this->serializedStyleToStyleIdMappingTable);
$style->setId($nextStyleId);
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
$this->styleIdToStyleMappingTable[$nextStyleId] = $style;
}
$return[] = $this->getStyleFromSerializedStyle($serializedStyle);
} }
return $this->getStyleFromSerializedStyle($serializedStyle); return $return;
} }
/** /**
@ -120,13 +125,19 @@ abstract class AbstractStyleHelper
protected function applyWrapTextIfCellContainsNewLine($style, $dataRow) protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
{ {
// if the "wrap text" option is already set, no-op // if the "wrap text" option is already set, no-op
if ($style->shouldWrapText()) { if (!is_array($style) && $style->shouldWrapText()) {
return $style; return $style;
} }
foreach ($dataRow as $cell) { foreach ($dataRow as $index => $cell) {
if (is_string($cell) && strpos($cell, "\n") !== false) { 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; break;
} }
} }

View File

@ -24,8 +24,11 @@ class StyleHelper extends AbstractStyleHelper
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
$this->usedFontsSet[$style->getFontName()] = true; $styles = is_array($style) ? $style : [$style];
return parent::registerStyle($style); foreach ($styles as $style ) {
$this->usedFontsSet[$style->getFontName()] = true;
}
return parent::registerStyle($styles);
} }
/** /**

View File

@ -134,7 +134,9 @@ class Worksheet implements WorksheetInterface
*/ */
public function addRow($dataRow, $style) public function addRow($dataRow, $style)
{ {
$styleIndex = ($style->getId() + 1); // 1-based if (!is_array($style)) {
$styleIndex = ($style->getId() + 1); // 1-based
}
$cellsCount = count($dataRow); $cellsCount = count($dataRow);
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount); $this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
@ -146,6 +148,10 @@ class Worksheet implements WorksheetInterface
for ($i = 0; $i < $cellsCount; $i++) { for ($i = 0; $i < $cellsCount; $i++) {
$currentCellValue = $dataRow[$currentCellIndex]; $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]) { if (!array_key_exists($nextCellIndex, $dataRow) || $currentCellValue !== $dataRow[$nextCellIndex]) {
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); $numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated); $data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);

View File

@ -133,10 +133,16 @@ EOD;
$data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">'; $data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
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); $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$data .= '<c r="' . $columnIndex . $rowIndex . '"'; $data .= '<c r="' . $columnIndex . $rowIndex . '"';
$data .= ' s="' . $style->getId() . '"'; $data .= ' s="' . $styleIndex . '"';
if (CellHelper::isNonEmptyString($cellValue)) { if (CellHelper::isNonEmptyString($cellValue)) {
if ($this->shouldUseInlineStrings) { if ($this->shouldUseInlineStrings) {