From 80487f1ac1279ccda77256d0952dd0472bc51a17 Mon Sep 17 00:00:00 2001 From: Alexander Hofstede Date: Fri, 13 Dec 2019 12:38:00 +0100 Subject: [PATCH] Add support for setting column width by range --- .../Manager/WorkbookManagerAbstract.php | 13 +++++++++++-- .../Writer/WriterMultiSheetsAbstract.php | 11 +++++++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 17 +++++++++++++---- tests/Spout/Writer/XLSX/SheetTest.php | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 12c6523..721acb4 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -303,13 +303,22 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface } /** - * @param float|null $width + * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth($width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) { $this->worksheetManager->setColumnWidth($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) { + $this->worksheetManager->setColumnWidthForRange($width, $start, $end); + } + /** * Closes the workbook and all its associated sheets. * All the necessary files are written to disk and zipped together to create the final file. diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 6148738..c0519ea 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -167,6 +167,17 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract $this->workbookManager->setColumnWidth($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 + * @throws WriterNotOpenedException + */ + public function setColumnWidthForRange(float $width, int $start, int $end) { + $this->throwIfWorkbookIsNotAvailable(); + $this->workbookManager->setColumnWidthForRange($width, $start, $end); + } + /** * Checks if the workbook has been created. Throws an exception if not created yet. * diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index f6ee831..bcf75cb 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -132,22 +132,31 @@ EOD; } /** - * @param float|null $width + * @param float $width * @param array $columns One or more columns with this width */ - public function setColumnWidth($width, ...$columns) { + public function setColumnWidth(float $width, ...$columns) { // Gather sequences $sequence = []; foreach ($columns as $i) { $sequenceLength = count($sequence); $previousValue = $sequence[$sequenceLength - 1]; if ($sequenceLength > 0 && $i !== $previousValue + 1) { - $this->columnWidths[] = [$sequence[0], $previousValue, $width]; + $this->setColumnWidthForRange($width, $sequence[0], $previousValue); $sequence = []; } $sequence[] = $i; } - $this->columnWidths[] = [$sequence[0], $sequence[count($sequence) - 1], $width]; + $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/tests/Spout/Writer/XLSX/SheetTest.php b/tests/Spout/Writer/XLSX/SheetTest.php index bf1bbb8..7fa5f77 100644 --- a/tests/Spout/Writer/XLSX/SheetTest.php +++ b/tests/Spout/Writer/XLSX/SheetTest.php @@ -211,6 +211,25 @@ class SheetTest extends TestCase $this->assertContains('createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterEntityFactory::createXLSXWriter(); + $writer->openToFile($resourcePath); + $writer->setColumnWidthForRange(50.0, 1, 3); + $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13'])); + $writer->close(); + + $pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml'; + $xmlContents = file_get_contents('zip://' . $pathToWorkbookFile); + + $this->assertContains('assertContains('