From 9e3f5c7ed71ee5e059be757b3e4edd0492e43973 Mon Sep 17 00:00:00 2001 From: Rares Marinescu Date: Fri, 17 Apr 2015 11:21:18 +0300 Subject: [PATCH 1/3] Sheets custom name. --- src/Spout/Writer/AbstractWriter.php | 8 +++--- .../Writer/Helper/XLSX/FileSystemHelper.php | 12 ++++++--- src/Spout/Writer/Internal/XLSX/Workbook.php | 9 +++++-- src/Spout/Writer/Sheet.php | 10 ++++++++ src/Spout/Writer/XLSX.php | 25 +++++++++++++++++-- 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 64f7440..57870f3 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -69,17 +69,18 @@ abstract class AbstractWriter implements WriterInterface * By using this method, the data will be written to a file. * * @param string $outputFilePath Path of the output file that will contain the data + * @param string $sheetName The custom name of the sheet * @return \Box\Spout\Writer\AbstractWriter * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable */ - public function openToFile($outputFilePath) + public function openToFile($outputFilePath, $sheetName = null) { $this->outputFilePath = $outputFilePath; $this->filePointer = $this->globalFunctionsHelper->fopen($this->outputFilePath, 'wb+'); $this->throwIfFilePointerIsNotAvailable(); - $this->openWriter(); + $this->openWriter($sheetName); $this->isWriterOpened = true; return $this; @@ -202,5 +203,4 @@ abstract class AbstractWriter implements WriterInterface $this->closeWriter(); $this->isWriterOpened = false; } -} - +} \ No newline at end of file diff --git a/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php b/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php index fddbb25..daaa0dd 100644 --- a/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php +++ b/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php @@ -249,10 +249,13 @@ EOD; EOD; + + $escaper = new \Box\Spout\Common\Escaper\XLSX(); /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { - $contentTypesXmlFileContents .= ' ' . PHP_EOL; + $worksheetName = $worksheet->getExternalSheet()->getName(); + $contentTypesXmlFileContents .= ' ' . PHP_EOL; } $contentTypesXmlFileContents .= <<getExternalSheet()->getName(); $worksheetId = $worksheet->getId(); - $workbookXmlFileContents .= ' ' . PHP_EOL; + $workbookXmlFileContents .= ' ' . PHP_EOL; } $workbookXmlFileContents .= <<getExternalSheet()->getName(); $worksheetId = $worksheet->getId(); - $workbookRelsXmlFileContents .= ' ' . PHP_EOL; + $workbookRelsXmlFileContents .= ' ' . PHP_EOL; } $workbookRelsXmlFileContents .= ''; diff --git a/src/Spout/Writer/Internal/XLSX/Workbook.php b/src/Spout/Writer/Internal/XLSX/Workbook.php index e6ca0db..060fbf8 100644 --- a/src/Spout/Writer/Internal/XLSX/Workbook.php +++ b/src/Spout/Writer/Internal/XLSX/Workbook.php @@ -62,13 +62,18 @@ class Workbook /** * Creates a new sheet in the workbook. The current sheet remains unchanged. * + * @param string $sheetName The custom name of the sheet * @return Worksheet The created sheet * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing */ - public function addNewSheet() + public function addNewSheet($sheetName = null) { $newSheetNumber = count($this->worksheets); $sheet = new Sheet($newSheetNumber); + if( !empty($sheetName) ) + { + $sheet->setName($sheetName); + } $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); @@ -132,7 +137,7 @@ class Workbook * @param Worksheet $worksheet * @return void */ - protected function setCurrentWorksheet($worksheet) + public function setCurrentWorksheet($worksheet) { $this->currentWorksheet = $worksheet; } diff --git a/src/Spout/Writer/Sheet.php b/src/Spout/Writer/Sheet.php index fd86c18..d2a27d5 100644 --- a/src/Spout/Writer/Sheet.php +++ b/src/Spout/Writer/Sheet.php @@ -27,6 +27,16 @@ class Sheet $this->name = self::DEFAULT_SHEET_NAME_PREFIX . ($sheetNumber + 1); } + /** + * @param string $sheetName The custom name of the sheet + * @return void + */ + public function setName($sheetName) + { + $sheetName = str_replace(' ', '_', $sheetName); + $this->name = $sheetName; + } + /** * @return int Number of the sheet, based on order of creation (zero-based) */ diff --git a/src/Spout/Writer/XLSX.php b/src/Spout/Writer/XLSX.php index 62e98f1..874ef06 100644 --- a/src/Spout/Writer/XLSX.php +++ b/src/Spout/Writer/XLSX.php @@ -66,15 +66,24 @@ class XLSX extends AbstractWriter /** * Configures the write and sets the current sheet pointer to a new sheet. * + * @param string $sheetName The custom name of the sheet * @return void * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing */ - protected function openWriter() + protected function openWriter($sheetName = null) { if (!$this->book) { $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically); - $this->book->addNewSheetAndMakeItCurrent(); + if( empty($sheetName) ) + { + $this->book->addNewSheetAndMakeItCurrent(); + } + else + { + $worksheet = $this->book->addNewSheet($sheetName); + $this->book->setCurrentWorksheet($worksheet); + } } } @@ -125,6 +134,18 @@ class XLSX extends AbstractWriter return $this->book->getCurrentWorksheet()->getExternalSheet(); } + /** + * Returns the the workbook for the XLSX file + * + * @return Book The workbook for the XLSX file + * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet + */ + public function getBook() + { + $this->throwIfBookIsNotAvailable(); + return $this->book; + } + /** * Sets the given sheet as the current one. New data will be written to this sheet. * The writing will resume where it stopped (i.e. data won't be truncated). From 57cace85a8caae1b4f2133321686e3f50ce73d60 Mon Sep 17 00:00:00 2001 From: Rares Marinescu Date: Wed, 22 Apr 2015 13:12:18 +0300 Subject: [PATCH 2/3] Sheets custom name - accordingly with the received feedback. --- src/Spout/Writer/AbstractWriter.php | 5 ++--- src/Spout/Writer/Helper/XLSX/FileSystemHelper.php | 12 +++--------- src/Spout/Writer/Internal/XLSX/Workbook.php | 9 ++------- src/Spout/Writer/Sheet.php | 1 - src/Spout/Writer/XLSX.php | 13 ++----------- 5 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/Spout/Writer/AbstractWriter.php b/src/Spout/Writer/AbstractWriter.php index 57870f3..a7e9b31 100644 --- a/src/Spout/Writer/AbstractWriter.php +++ b/src/Spout/Writer/AbstractWriter.php @@ -69,18 +69,17 @@ abstract class AbstractWriter implements WriterInterface * By using this method, the data will be written to a file. * * @param string $outputFilePath Path of the output file that will contain the data - * @param string $sheetName The custom name of the sheet * @return \Box\Spout\Writer\AbstractWriter * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable */ - public function openToFile($outputFilePath, $sheetName = null) + public function openToFile($outputFilePath) { $this->outputFilePath = $outputFilePath; $this->filePointer = $this->globalFunctionsHelper->fopen($this->outputFilePath, 'wb+'); $this->throwIfFilePointerIsNotAvailable(); - $this->openWriter($sheetName); + $this->openWriter(); $this->isWriterOpened = true; return $this; diff --git a/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php b/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php index daaa0dd..fddbb25 100644 --- a/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php +++ b/src/Spout/Writer/Helper/XLSX/FileSystemHelper.php @@ -249,13 +249,10 @@ EOD; EOD; - - $escaper = new \Box\Spout\Common\Escaper\XLSX(); /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { - $worksheetName = $worksheet->getExternalSheet()->getName(); - $contentTypesXmlFileContents .= ' ' . PHP_EOL; + $contentTypesXmlFileContents .= ' ' . PHP_EOL; } $contentTypesXmlFileContents .= <<getExternalSheet()->getName(); $worksheetId = $worksheet->getId(); - $workbookXmlFileContents .= ' ' . PHP_EOL; + $workbookXmlFileContents .= ' ' . PHP_EOL; } $workbookXmlFileContents .= <<getExternalSheet()->getName(); $worksheetId = $worksheet->getId(); - $workbookRelsXmlFileContents .= ' ' . PHP_EOL; + $workbookRelsXmlFileContents .= ' ' . PHP_EOL; } $workbookRelsXmlFileContents .= ''; diff --git a/src/Spout/Writer/Internal/XLSX/Workbook.php b/src/Spout/Writer/Internal/XLSX/Workbook.php index 060fbf8..1c521ea 100644 --- a/src/Spout/Writer/Internal/XLSX/Workbook.php +++ b/src/Spout/Writer/Internal/XLSX/Workbook.php @@ -62,23 +62,18 @@ class Workbook /** * Creates a new sheet in the workbook. The current sheet remains unchanged. * - * @param string $sheetName The custom name of the sheet * @return Worksheet The created sheet * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing */ - public function addNewSheet($sheetName = null) + public function addNewSheet() { $newSheetNumber = count($this->worksheets); $sheet = new Sheet($newSheetNumber); - if( !empty($sheetName) ) - { - $sheet->setName($sheetName); - } $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); $this->worksheets[] = $worksheet; - + return $worksheet; } diff --git a/src/Spout/Writer/Sheet.php b/src/Spout/Writer/Sheet.php index d2a27d5..e1c00d6 100644 --- a/src/Spout/Writer/Sheet.php +++ b/src/Spout/Writer/Sheet.php @@ -33,7 +33,6 @@ class Sheet */ public function setName($sheetName) { - $sheetName = str_replace(' ', '_', $sheetName); $this->name = $sheetName; } diff --git a/src/Spout/Writer/XLSX.php b/src/Spout/Writer/XLSX.php index 874ef06..1f4e44d 100644 --- a/src/Spout/Writer/XLSX.php +++ b/src/Spout/Writer/XLSX.php @@ -66,24 +66,15 @@ class XLSX extends AbstractWriter /** * Configures the write and sets the current sheet pointer to a new sheet. * - * @param string $sheetName The custom name of the sheet * @return void * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing */ - protected function openWriter($sheetName = null) + protected function openWriter() { if (!$this->book) { $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically); - if( empty($sheetName) ) - { - $this->book->addNewSheetAndMakeItCurrent(); - } - else - { - $worksheet = $this->book->addNewSheet($sheetName); - $this->book->setCurrentWorksheet($worksheet); - } + $this->book->addNewSheetAndMakeItCurrent(); } } From ed039465a70aae1feed692e87c85b67563c4e30f Mon Sep 17 00:00:00 2001 From: Rares Marinescu Date: Wed, 22 Apr 2015 13:19:28 +0300 Subject: [PATCH 3/3] Switched back the "setCurrentWorksheet" method to protected --- src/Spout/Writer/Internal/XLSX/Workbook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spout/Writer/Internal/XLSX/Workbook.php b/src/Spout/Writer/Internal/XLSX/Workbook.php index 1c521ea..83f17b0 100644 --- a/src/Spout/Writer/Internal/XLSX/Workbook.php +++ b/src/Spout/Writer/Internal/XLSX/Workbook.php @@ -132,7 +132,7 @@ class Workbook * @param Worksheet $worksheet * @return void */ - public function setCurrentWorksheet($worksheet) + protected function setCurrentWorksheet($worksheet) { $this->currentWorksheet = $worksheet; }