From a6a8b9e80f8707ee0b29dd51b1ddfd14c82dd0b8 Mon Sep 17 00:00:00 2001 From: willkensonh Date: Thu, 11 Oct 2018 12:21:51 +1100 Subject: [PATCH] add column widths and merged cell functionality to spout --- src/Spout/Writer/Common/Entity/Worksheet.php | 126 ++++++++++++++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 3 + 2 files changed, 129 insertions(+) diff --git a/src/Spout/Writer/Common/Entity/Worksheet.php b/src/Spout/Writer/Common/Entity/Worksheet.php index 74c4976..e1c8aaa 100644 --- a/src/Spout/Writer/Common/Entity/Worksheet.php +++ b/src/Spout/Writer/Common/Entity/Worksheet.php @@ -2,6 +2,8 @@ namespace Box\Spout\Writer\Common\Entity; +use Box\Spout\Common\Exception\IOException; + /** * Class Worksheet * Entity describing a Worksheet @@ -23,6 +25,14 @@ class Worksheet /** @var int Index of the last written row */ private $lastWrittenRowIndex; + private $colWidths; + + private $defaultColWidth; + + private $defaultRowHeight; + + private $merges; + /** * Worksheet constructor. * @@ -110,4 +120,120 @@ class Worksheet // sheet index is zero-based, while ID is 1-based return $this->externalSheet->getIndex() + 1; } + + /** + * sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet + * @param string $col in letter format eg A or AC + * @param float $width + * @throws IOException + */ + public function setColWidth(string $col, float $width) + { + $this->throwIfSheetFilePointerIsAlreadyCreated(); + $this->colWidths[$col] = $width; + } + + /** + * sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet + * @param float $width + * @throws IOException + */ + public function setDefaultColWidth(float $width) + { + $this->throwIfSheetFilePointerIsAlreadyCreated(); + $this->defaultColWidth = $width; + } + + /** + * sets default row height --- Must be set before WorksheetManager->startSheet() is called on this sheet + * @param float $height + * @throws IOException + */ + public function setDefaultRowHeight(float $height) + { + $this->throwIfSheetFilePointerIsAlreadyCreated(); + $this->defaultRowHeight = $height; + } + + /** + * merge cells params should be letter and number cell reference eg A3, A5 + * @param string $leftCell + * @param string $rightCell + */ + public function mergeCells(string $leftCell, string $rightCell) + { + $this->merges[] = $leftCell . ':' . $rightCell; + } + + /** + * remove merged cell reference. + * @param string $leftCell + * @param string $rightCell + */ + public function unMergeCells(string $leftCell, string $rightCell) + { + $this->merges = array_diff($this->merges,[$leftCell . ':' . $rightCell]); + } + + /** + * used by WorksheetManager to get default row height and width xml to inject into worksheet xml file + * @return string + */ + public function getDefaultXML() : string + { + if (empty($this->defaultColWidth) && empty($this->defaultRowHeight)) { + return ''; + } + return 'defaultColWidth) ? '' : ' defaultColWidth="'.$this->defaultColWidth.'"') . + (empty($this->defaultRowHeight) ? '' : ' defaultRowHeight="'.$this->defaultRowHeight.'"') . + '/>'; + } + + /** + * used by WorksheetManager to get column width references xml to inject into worksheet xml file + * @return string + */ + public function getColWidthXML() + { + if (empty($colWidths)) { + return ''; + } + $xml = ''; + foreach ($this->colWidths as $col => $width) { + $xml .= ''; //style and customWidth may be unnecessary ?? + } + $xml .= ''; + return $xml; + } + + /** + * used by WorksheetManager to get merged cell references xml to inject into worksheet xml file + * @return string + */ + public function getMergeXML() : string + { + if (empty($this->merges)) { + return ''; + } + $xml = ''; + foreach ($this->merges as $merge) { + $xml .= ''; + } + $xml .= ''; + return $xml; + } + + /** + * Checks if the sheet has already been started - throws exception + * + * @throws IOException If the sheet data file is already opened + * @return void + */ + private function throwIfSheetFilePointerIsAlreadyCreated() + { + if (!empty($this->filePointer)) { + throw new IOException('Trying to add default or column width settings after sheet is created'); + } + } } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index b0e458f..30a99e1 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -113,6 +113,8 @@ EOD; $worksheet->setFilePointer($sheetFilePointer); fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); + fwrite($sheetFilePointer, $worksheet->getDefaultXML()); + fwrite($sheetFilePointer, $worksheet->getColWidthXML()); fwrite($sheetFilePointer, ''); } @@ -268,6 +270,7 @@ EOD; } fwrite($worksheetFilePointer, ''); + fwrite($worksheetFilePointer, $worksheet->getMergeXML()); fwrite($worksheetFilePointer, ''); fclose($worksheetFilePointer); }