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

Not just another custom column widths PR #715

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
82170a0
Add XLSX support for default column width/row height as well as custo…
aphofstede Dec 12, 2019
80487f1
Add support for setting column width by range
aphofstede Dec 13, 2019
4b9eb5b
PSR-2 code style
aphofstede Dec 13, 2019
09a624e
Avoid $previousValue if length is < 1
aphofstede Dec 16, 2019
6db9871
Extract common functionality to trait for easier reuse, fix default r…
aphofstede Dec 17, 2019
26ad590
Add support for default cell sizes for ODS files
aphofstede Dec 17, 2019
ceda150
Rename default column style
aphofstede Dec 19, 2019
db197de
Add support for custom column widths in ODS exports
aphofstede Dec 19, 2019
d2dadd4
Ignore generated test files in subdirectories
aphofstede Dec 19, 2019
49e621b
Merge remote-tracking branch 'remotes/origin/master' into custom-colu…
aphofstede Dec 19, 2019
aa5ec50
Set empty array as default for column widths
aphofstede Dec 19, 2019
bb83904
Remove null phpdoc where typehint is float
aphofstede Dec 20, 2019
ffec804
Attempt to satisfy php-cs-fixer
aphofstede Dec 20, 2019
c1757d2
Merge branch 'master' into custom-column-widths
aphofstede Dec 20, 2019
36573ea
Satisfy php-cs-fixer
aphofstede Dec 20, 2019
26e5f96
Use options manager for default row height and column width
mawi12345 Mar 29, 2020
de3241b
Delay sheetData and add sheetDataStarted flag to worksheet
mawi12345 Mar 29, 2020
d065f95
Merge pull request #1 from bizquiz/custom-column-widths
aphofstede May 26, 2020
1eee2ea
Satisfy php-cs-fixer
aphofstede May 26, 2020
9727bec
Fix ODS column widths
mawi12345 Jun 13, 2020
557b6fd
Merge pull request #2 from bizquiz/custom-column-widths
aphofstede Oct 29, 2020
73f2a52
Satisfy php-cs-fixer
aphofstede Oct 30, 2020
615c076
Merge branch 'master' into custom-column-widths
aphofstede Apr 5, 2021
77189d7
Merge branch 'master' into custom-column-widths
aphofstede May 26, 2021
5055121
Merge branch 'master' into custom-column-widths
aphofstede Jul 14, 2021
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/.idea
*.iml

/tests/resources/generated
/tests/**/resources/generated
/tests/coverage
/vendor
/composer.lock
Expand Down
5 changes: 5 additions & 0 deletions src/Spout/Writer/Common/Entity/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ abstract class Options

// XLSX specific options
const SHOULD_USE_INLINE_STRINGS = 'shouldUseInlineStrings';

// Cell size options
const DEFAULT_COLUMN_WIDTH = 'defaultColumnWidth';
const DEFAULT_ROW_HEIGHT = 'defaultRowHeight';
const COLUMN_WIDTHS = 'columnWidthDefinition';
}
20 changes: 20 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 has the sheet data header been written */
private $sheetDataStarted = false;

/**
* Worksheet constructor.
*
Expand All @@ -36,6 +39,7 @@ public function __construct($worksheetFilePath, Sheet $externalSheet)
$this->externalSheet = $externalSheet;
$this->maxNumColumns = 0;
$this->lastWrittenRowIndex = 0;
$this->sheetDataStarted = false;
}

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

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

/**
* @param bool $sheetDataStarted
*/
public function setSheetDataStarted($sheetDataStarted)
{
$this->sheetDataStarted = $sheetDataStarted;
}
}
63 changes: 63 additions & 0 deletions src/Spout/Writer/Common/Manager/ManagesCellSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Box\Spout\Writer\Common\Manager;

trait ManagesCellSize
{
/** @var float|null The default column width to use */
private $defaultColumnWidth;

/** @var float|null The default row height to use */
private $defaultRowHeight;

/** @var array Array of min-max-width arrays */
private $columnWidths = [];

/**
* @param float|null $width
*/
public function setDefaultColumnWidth($width)
{
$this->defaultColumnWidth = $width;
}

/**
* @param float|null $height
*/
public function setDefaultRowHeight($height)
{
$this->defaultRowHeight = $height;
}

/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $width, ...$columns)
{
// Gather sequences
$sequence = [];
foreach ($columns as $i) {
$sequenceLength = count($sequence);
if ($sequenceLength > 0) {
$previousValue = $sequence[$sequenceLength - 1];
if ($i !== $previousValue + 1) {
$this->setColumnWidthForRange($width, $sequence[0], $previousValue);
$sequence = [];
}
}
$sequence[] = $i;
}
$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];
}
}
56 changes: 51 additions & 5 deletions src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterException;

/**
* Class WorkbookManagerAbstract
Expand Down Expand Up @@ -103,7 +102,6 @@ public function getWorkbook()
* Creates a new sheet in the workbook and make it the current sheet.
* The writing will resume where it stopped (i.e. data won't be truncated).
*
* @throws IOException If unable to open the sheet for writing
* @return Worksheet The created sheet
*/
public function addNewSheetAndMakeItCurrent()
Expand All @@ -117,7 +115,7 @@ public function addNewSheetAndMakeItCurrent()
/**
* Creates a new sheet in the workbook. The current sheet remains unchanged.
*
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
* @throws IOException
* @return Worksheet The created sheet
*/
private function addNewSheet()
Expand Down Expand Up @@ -157,6 +155,16 @@ public function getCurrentWorksheet()
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).
Expand Down Expand Up @@ -210,8 +218,9 @@ private function getWorksheetFromExternalSheet($sheet)
* with the creation of new worksheets if one worksheet has reached its maximum capicity.
*
* @param Row $row The row to be added
*
* @throws IOException If trying to create a new sheet and unable to open the sheet for writing
* @throws WriterException If unable to write data
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @return void
*/
public function addRowToCurrentWorksheet(Row $row)
Expand Down Expand Up @@ -249,7 +258,9 @@ private function hasCurrentWorksheetReachedMaxRows()
*
* @param Worksheet $worksheet Worksheet to write the row to
* @param Row $row The row to be added
* @throws WriterException If unable to write data
*
* @throws IOException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @return void
*/
private function addRowToWorksheet(Worksheet $worksheet, Row $row)
Expand All @@ -276,6 +287,41 @@ private function applyDefaultRowStyle(Row $row)
}
}

/**
* @param float $width
*/
public function setDefaultColumnWidth(float $width)
{
$this->worksheetManager->setDefaultColumnWidth($width);
}

/**
* @param float $height
*/
public function setDefaultRowHeight(float $height)
{
$this->worksheetManager->setDefaultRowHeight($height);
}

/**
* @param float $width
* @param array $columns One or more columns with this width
*/
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.
Expand Down
15 changes: 15 additions & 0 deletions src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ public function getWorksheets();
*/
public function getCurrentWorksheet();

/**
* Starts the current sheet and opens its file pointer
*/
public function startCurrentSheet();

/**
* @param float $width
*/
public function setDefaultColumnWidth(float $width);

/**
* @param float $height
*/
public function setDefaultRowHeight(float $height);

/**
* 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).
Expand Down
23 changes: 23 additions & 0 deletions src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
*/
interface WorksheetManagerInterface
{
/**
* @param float|null $width
*/
public function setDefaultColumnWidth($width);

/**
* @param float|null $height
*/
public function setDefaultRowHeight($height);

/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $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);

/**
* Adds a row to the worksheet.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Writer/ODS/Creator/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function createStyleManager(OptionsManagerInterface $optionsManager)
{
$styleRegistry = $this->createStyleRegistry($optionsManager);

return new StyleManager($styleRegistry);
return new StyleManager($styleRegistry, $optionsManager);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Spout/Writer/ODS/Manager/OptionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ protected function getSupportedOptions()
Options::TEMP_FOLDER,
Options::DEFAULT_ROW_STYLE,
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY,
Options::DEFAULT_COLUMN_WIDTH,
Options::DEFAULT_ROW_HEIGHT,
Options::COLUMN_WIDTHS,
];
}

Expand Down
Loading