Extract common functionality to trait for easier reuse, fix default row height for XLSX

This commit is contained in:
Alexander Hofstede 2019-12-17 20:31:26 +01:00
parent 09a624ef8e
commit 6db9871722
4 changed files with 97 additions and 60 deletions

View File

@ -0,0 +1,66 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
trait ManagesCellSize
{
/** @var float|null The default column width to use */
private $defaultColumnWidth;
/** @var float|null The default row height to use */
private $defaultRowHeight;
/** @var array Array of min-max-width arrays */
private $columnWidths;
/**
* @param float|null $width
*/
public function setDefaultColumnWidth($width)
{
$this->defaultColumnWidth = $width;
}
/**
* @param float|null $height
*/
public function setDefaultRowHeight($height)
{
$this->defaultRowHeight = $height;
}
/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $width, ...$columns)
{
// Gather sequences
$sequence = [];
foreach ($columns as $i) {
$sequenceLength = count($sequence);
if ($sequenceLength > 0) {
$previousValue = $sequence[$sequenceLength - 1];
if ($i !== $previousValue + 1) {
$this->setColumnWidthForRange($width, $sequence[0], $previousValue);
$sequence = [];
}
}
$sequence[] = $i;
}
$this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]);
}
/**
* @param float $width The width to set
* @param int $start First column index of the range
* @param int $end Last column index of the range
*/
public function setColumnWidthForRange(float $width, int $start, int $end)
{
$this->columnWidths[] = [$start, $end, $width];
}
}

View File

@ -11,6 +11,29 @@ use Box\Spout\Writer\Common\Entity\Worksheet;
*/ */
interface WorksheetManagerInterface interface WorksheetManagerInterface
{ {
/**
* @param float|null $width
*/
public function setDefaultColumnWidth($width);
/**
* @param float|null $height
*/
public function setDefaultRowHeight($height);
/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $width, ...$columns);
/**
* @param float $width The width to set
* @param int $start First column index of the range
* @param int $end Last column index of the range
*/
public function setColumnWidthForRange(float $width, int $start, int $end);
/** /**
* Adds a row to the worksheet. * Adds a row to the worksheet.
* *

View File

@ -14,6 +14,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Manager\ManagesCellSize;
use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
@ -25,6 +26,8 @@ use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
*/ */
class WorksheetManager implements WorksheetManagerInterface class WorksheetManager implements WorksheetManagerInterface
{ {
use ManagesCellSize;
/** /**
* Maximum number of characters a cell can contain * Maximum number of characters a cell can contain
* @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007]
@ -62,18 +65,9 @@ EOD;
/** @var InternalEntityFactory Factory to create entities */ /** @var InternalEntityFactory Factory to create entities */
private $entityFactory; private $entityFactory;
/** @var float|null The default column width to use */
private $defaultColumnWidth;
/** @var float|null The default row height to use */
private $defaultRowHeight;
/** @var bool Whether rows have been written */ /** @var bool Whether rows have been written */
private $hasWrittenRows = false; private $hasWrittenRows = false;
/** @var array Array of min-max-width arrays */
private $columnWidths;
/** /**
* WorksheetManager constructor. * WorksheetManager constructor.
* *
@ -117,54 +111,6 @@ EOD;
return $this->sharedStringsManager; return $this->sharedStringsManager;
} }
/**
* @param float|null $width
*/
public function setDefaultColumnWidth($width)
{
$this->defaultColumnWidth = $width;
}
/**
* @param float|null $height
*/
public function setDefaultRowHeight($height)
{
$this->defaultRowHeight = $height;
}
/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $width, ...$columns)
{
// Gather sequences
$sequence = [];
foreach ($columns as $i) {
$sequenceLength = count($sequence);
if ($sequenceLength > 0) {
$previousValue = $sequence[$sequenceLength - 1];
if ($i !== $previousValue + 1) {
$this->setColumnWidthForRange($width, $sequence[0], $previousValue);
$sequence = [];
}
}
$sequence[] = $i;
}
$this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]);
}
/**
* @param float $width The width to set
* @param int $start First column index of the range
* @param int $end Last column index of the range
*/
public function setColumnWidthForRange(float $width, int $start, int $end)
{
$this->columnWidths[] = [$start, $end, $width];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -226,7 +172,8 @@ EOD;
$rowIndex = $worksheet->getLastWrittenRowIndex() + 1; $rowIndex = $worksheet->getLastWrittenRowIndex() + 1;
$numCells = $row->getNumCells(); $numCells = $row->getNumCells();
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">'; $hasCustomHeight = $this->defaultRowHeight > 0 ? '1' : '0';
$rowXML = "<row r=\"{$rowIndex}\" spans=\"1:{$numCells}\" customHeight=\"{$hasCustomHeight}\">";
foreach ($row->getCells() as $cell) { foreach ($row->getCells() as $cell) {
$rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex); $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex);

View File

@ -117,7 +117,7 @@ class SheetTest extends TestCase
$writer = WriterEntityFactory::createXLSXWriter(); $writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->setDefaultColumnWidth(10.0); $writer->setDefaultColumnWidth(10.0);
$writer->setDefaultRowHeight(10.0); $writer->setDefaultRowHeight(20.0);
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
$writer->close(); $writer->close();
@ -126,7 +126,8 @@ class SheetTest extends TestCase
$this->assertContains('<sheetFormatPr', $xmlContents, 'No sheetFormatPr tag found in sheet'); $this->assertContains('<sheetFormatPr', $xmlContents, 'No sheetFormatPr tag found in sheet');
$this->assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet'); $this->assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet');
$this->assertContains(' defaultRowHeight="10', $xmlContents, 'No default row height found in sheet'); $this->assertContains(' defaultRowHeight="20', $xmlContents, 'No default row height found in sheet');
$this->assertContains(' customHeight="1"', $xmlContents, 'No row height override flag found in row');
} }
public function testWritesDefaultRequiredRowHeightIfOmitted() public function testWritesDefaultRequiredRowHeightIfOmitted()