diff --git a/src/Spout/Writer/Common/Manager/ManagesCellSize.php b/src/Spout/Writer/Common/Manager/ManagesCellSize.php new file mode 100644 index 0000000..44cf2a1 --- /dev/null +++ b/src/Spout/Writer/Common/Manager/ManagesCellSize.php @@ -0,0 +1,66 @@ +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]; + } + +} diff --git a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php index bb54ee6..bb6758a 100644 --- a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php @@ -11,6 +11,29 @@ use Box\Spout\Writer\Common\Entity\Worksheet; */ 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. * diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index abfc476..3c7208d 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -14,6 +14,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; 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\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; @@ -25,6 +26,8 @@ use Box\Spout\Writer\XLSX\Manager\Style\StyleManager; */ class WorksheetManager implements WorksheetManagerInterface { + use ManagesCellSize; + /** * 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] @@ -62,18 +65,9 @@ EOD; /** @var InternalEntityFactory Factory to create entities */ 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 */ private $hasWrittenRows = false; - /** @var array Array of min-max-width arrays */ - private $columnWidths; - /** * WorksheetManager constructor. * @@ -117,54 +111,6 @@ EOD; 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} */ @@ -226,7 +172,8 @@ EOD; $rowIndex = $worksheet->getLastWrittenRowIndex() + 1; $numCells = $row->getNumCells(); - $rowXML = ''; + $hasCustomHeight = $this->defaultRowHeight > 0 ? '1' : '0'; + $rowXML = ""; foreach ($row->getCells() as $cell) { $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex); diff --git a/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index 7fa5f77..91c28f9 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -117,7 +117,7 @@ class SheetTest extends TestCase $writer = WriterEntityFactory::createXLSXWriter(); $writer->openToFile($resourcePath); $writer->setDefaultColumnWidth(10.0); - $writer->setDefaultRowHeight(10.0); + $writer->setDefaultRowHeight(20.0); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->close(); @@ -126,7 +126,8 @@ class SheetTest extends TestCase $this->assertContains('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()