From c3d3c5ffbffb52853a09e93dc81bfcc1d5d509cb Mon Sep 17 00:00:00 2001 From: willkensonh Date: Thu, 11 Oct 2018 16:30:56 +1100 Subject: [PATCH] fix to not start fwrites on sheet when it is initially created --- .../Manager/WorkbookManagerAbstract.php | 12 ++++++++++-- .../Manager/WorkbookManagerInterface.php | 5 +++++ .../Writer/WriterMultiSheetsAbstract.php | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 7be5c6e..773f0aa 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -131,8 +131,6 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface $worksheetFilePath = $this->getWorksheetFilePath($sheet); $worksheet = $this->entityFactory->createWorksheet($worksheetFilePath, $sheet); - $this->worksheetManager->startSheet($worksheet); - $worksheets[] = $worksheet; $this->workbook->setWorksheets($worksheets); @@ -157,6 +155,16 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface return $this->currentWorksheet; } + /** + * starts the current sheet and opens the file pointer + * + * @throws IOException + */ + public function startCurrentSheet() + { + $this->worksheetManager->startSheet($this->getCurrentWorksheet()); + } + /** * 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). diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php index aed304a..50467c4 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php @@ -30,6 +30,11 @@ interface WorkbookManagerInterface */ public function addNewSheetAndMakeItCurrent(); + /** + * @return void starts the current sheet and opens its file pointer + */ + public function startCurrentSheet(); + /** * @return Worksheet[] All the workbook's sheets */ diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 8170b67..4d7411a 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -4,6 +4,7 @@ namespace Box\Spout\Writer; use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Entity\Row; +use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; @@ -107,6 +108,24 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract return $worksheet->getExternalSheet(); } + public function startCurrentSheet() + { + $this->workbookManager->startCurrentSheet(); + } + + /** + * @param Row $row + * @throws WriterNotOpenedException|SpoutException + * @return WriterAbstract + */ + public function addRow(Row $row) + { + if (!$this->workbookManager->getCurrentWorksheet()->getFilePointer()){ + $this->startCurrentSheet(); + } + return parent::addRow($row); + } + /** * Returns the current sheet *