Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

feat: add sheet view support to XLSX writer #744

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/_pages/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ $writer->setShouldCreateNewSheetsAutomatically(true); // default value
$writer->setShouldCreateNewSheetsAutomatically(false); // will stop writing new data when limit is reached
```

### Sheet view (XLSX writer)

Sheet view settings must be configured before any rows are added to the sheet.

```php
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\XLSX\Entity\SheetView;

$sheetView = (new SheetView())
->setFreezeRow(2) // First row will be fixed
->setFreezeColumn('D') // Columns A to C will be fixed
->setZoomScale(150); // And other options

$writer = WriterEntityFactory::createXLSXWriter();
$writer->getCurrentSheet()->setSheetView($sheetView);
```

### Using a custom temporary folder

Processing XLSX and ODS files requires temporary files to be created. By default, {{ site.spout_html }} will use the system default temporary folder (as returned by `sys_get_temp_dir()`). It is possible to override this by explicitly setting it on the reader or writer:
Expand Down
31 changes: 31 additions & 0 deletions src/Spout/Writer/Common/Entity/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Entity;

use Box\Spout\Writer\Common\Manager\SheetManager;
use Box\Spout\Writer\XLSX\Entity\SheetView;

/**
* Class Sheet
Expand All @@ -27,6 +28,9 @@ class Sheet
/** @var SheetManager Sheet manager */
private $sheetManager;

/** @var SheetView */
private $sheetView;

/**
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $associatedWorkbookId ID of the sheet's associated workbook
Expand Down Expand Up @@ -108,4 +112,31 @@ public function setIsVisible($isVisible)

return $this;
}

/**
* @return SheetView|null
*/
public function getSheetView(): ?SheetView
{
return $this->sheetView;
}

/**
* @param SheetView $sheetView
* @return $this
*/
public function setSheetView(SheetView $sheetView)
{
$this->sheetView = $sheetView;

return $this;
}

/**
* @return bool
*/
public function hasSheetView(): bool
{
return $this->sheetView instanceof SheetView;
}
}
19 changes: 19 additions & 0 deletions src/Spout/Writer/Common/Entity/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Worksheet
/** @var int Index of the last written row */
private $lastWrittenRowIndex;

/** @var bool */
private $hasStarted = false;

/**
* Worksheet constructor.
*
Expand Down Expand Up @@ -110,4 +113,20 @@ public function getId()
// sheet index is zero-based, while ID is 1-based
return $this->externalSheet->getIndex() + 1;
}

/**
* @return bool
*/
public function hasStarted(): bool
{
return $this->hasStarted;
}

/**
* @param bool $hasStarted
*/
public function setHasStarted(bool $hasStarted = true): void
{
$this->hasStarted = $hasStarted;
}
}
9 changes: 7 additions & 2 deletions src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ private function addNewSheet()
$worksheetFilePath = $this->getWorksheetFilePath($sheet);
$worksheet = $this->entityFactory->createWorksheet($worksheetFilePath, $sheet);

$this->worksheetManager->startSheet($worksheet);

$worksheets[] = $worksheet;
$this->workbook->setWorksheets($worksheets);

Expand Down Expand Up @@ -223,8 +221,15 @@ public function addRowToCurrentWorksheet(Row $row)
if ($hasReachedMaxRows) {
// ... continue writing in a new sheet if option set
if ($this->optionsManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
$previousSheet = $currentWorksheet->getExternalSheet();
$currentWorksheet = $this->addNewSheetAndMakeItCurrent();

// Use the same sheet view if set
$sheet = $currentWorksheet->getExternalSheet();
if ($previousSheet->hasSheetView()) {
$sheet->setSheetView($previousSheet->getSheetView());
}

$this->addRowToWorksheet($currentWorksheet, $row);
} else {
// otherwise, do nothing as the data won't be written anyways
Expand Down
Loading