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

Added support for mergeCells, cell height, shrink to fit #738

Open
wants to merge 6 commits 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
27 changes: 27 additions & 0 deletions src/Spout/Common/Entity/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Row
*/
protected $style;

/**
* Row height (default is 15)
* @var string
*/
protected $height = '15';

/**
* Row constructor.
* @param Cell[] $cells
Expand Down Expand Up @@ -126,4 +132,25 @@ public function toArray()
return $cell->getValue();
}, $this->cells);
}

/**
* Set row height
* @param string $height
* @return Row
*/
public function setHeight($height)
{
$this->height = $height;

return $this;
}

/**
* Returns row height
* @return string
*/
public function getHeight()
{
return $this->height;
}
}
34 changes: 34 additions & 0 deletions src/Spout/Common/Entity/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class Style
/** @var bool Whether the wrap text property was set */
private $hasSetWrapText = false;

/** @var bool Whether the cell should shrink to fit to content */
private $shouldShrinkToFit = false;
/** @var bool Whether the shouldShrinkToFit text property was set */
private $hasSetShrinkToFit = false;

/** @var Border */
private $border;

Expand Down Expand Up @@ -463,4 +468,33 @@ public function shouldApplyFormat()
{
return $this->hasSetFormat;
}

/**
* Sets should shrink to fit
* @param bool $shrinkToFit
* @return Style
*/
public function setShouldShrinkToFit($shrinkToFit = true)
{
$this->hasSetShrinkToFit = true;
$this->shouldShrinkToFit = $shrinkToFit;

return $this;
}

/**
* @return bool Whether format should be applied
*/
public function shouldShrinkToFit()
{
return $this->shouldShrinkToFit;
}

/**
* @return bool
*/
public function hasSetShrinkToFit()
{
return $this->hasSetShrinkToFit;
}
}
17 changes: 17 additions & 0 deletions src/Spout/Common/Manager/OptionsManagerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ public function setOption($optionName, $optionValue)
}
}

/**
* Add an option to the internal list of options
* Used only for mergeCells() for now
* @param mixed $optionName
* @param mixed $optionValue
* @return void
*/
public function addOption($optionName, $optionValue)
{
if (\in_array($optionName, $this->supportedOptions)) {
if (!isset($this->options[$optionName])) {
$this->options[$optionName] = [];
}
$this->options[$optionName][] = $optionValue;
}
}

/**
* @param string $optionName
* @return mixed|null The set option or NULL if no option with given name found
Expand Down
9 changes: 9 additions & 0 deletions src/Spout/Common/Manager/OptionsManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public function setOption($optionName, $optionValue);
* @return mixed|null The set option or NULL if no option with given name found
*/
public function getOption($optionName);

/**
* Add an option to the internal list of options
* Used only for mergeCells() for now
* @param mixed $optionName
* @param mixed $optionValue
* @return void
*/
public function addOption($optionName, $optionValue);
}
4 changes: 2 additions & 2 deletions src/Spout/Reader/ODS/SheetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class SheetIterator implements IteratorInterface
const XML_ATTRIBUTE_TABLE_STYLE_NAME = 'table:style-name';
const XML_ATTRIBUTE_TABLE_DISPLAY = 'table:display';

/** @var string $filePath Path of the file to be read */
/** @var string Path of the file to be read */
protected $filePath;

/** @var \Box\Spout\Common\Manager\OptionsManagerInterface Reader's options manager */
protected $optionsManager;

/** @var InternalEntityFactory $entityFactory Factory to create entities */
/** @var InternalEntityFactory Factory to create entities */
protected $entityFactory;

/** @var XMLReader The XMLReader object that will help read sheet's XML data */
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Reader/XLSX/Manager/SharedStringsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SharedStringsManager
/** @var InternalEntityFactory Factory to create entities */
protected $entityFactory;

/** @var HelperFactory $helperFactory Factory to create helpers */
/** @var HelperFactory Factory to create helpers */
protected $helperFactory;

/** @var CachingStrategyFactory Factory to create shared strings caching strategies */
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Reader/XLSX/RowIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RowIterator implements IteratorInterface
/** @var string Path of the XLSX file being read */
protected $filePath;

/** @var string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml */
/** @var string Path of the sheet data XML file as in [Content_Types].xml */
protected $sheetDataXMLFilePath;

/** @var \Box\Spout\Reader\Wrapper\XMLReader The XMLReader object that will help read sheet's XML data */
Expand Down
13 changes: 13 additions & 0 deletions src/Spout/Writer/Common/Creator/Style/StyleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ public function setFormat($format)
return $this;
}

/**
* Set should shrink to fit
* @param bool $shrinkToFit
* @return StyleBuilder
* @api
*/
public function setShouldShrinkToFit($shrinkToFit = true)
{
$this->style->setShouldShrinkToFit($shrinkToFit);

return $this;
}

/**
* Returns the configured style. The style is cached and can be reused.
*
Expand Down
6 changes: 6 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,10 @@ abstract class Options

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

// XLSX column widths
const COLUMN_WIDTHS = 'columnWidths';

// XLSX merge cells
const MERGE_CELLS = 'mergeCells';
}
22 changes: 22 additions & 0 deletions src/Spout/Writer/Common/Entity/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Sheet
/** @var SheetManager Sheet manager */
private $sheetManager;

/** @var bool Sheet is started */
private $isSheetStarted = false;

/**
* @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 +111,23 @@ public function setIsVisible($isVisible)

return $this;
}

/**
* @return bool isSheetStarted Sheet was started
*/
public function isSheetStarted()
{
return $this->isSheetStarted;
}

/**
* @param bool $isSheetStarted Set if sheet was started
* @return Sheet
*/
public function setIsSheetStarted($isSheetStarted)
{
$this->isSheetStarted = $isSheetStarted;

return $this;
}
}
3 changes: 3 additions & 0 deletions src/Spout/Writer/Common/Manager/Style/StyleMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $
if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
$styleToUpdate->setShouldWrapText();
}
if (!$style->hasSetShrinkToFit() && $baseStyle->shouldShrinkToFit()) {
$styleToUpdate->setShouldShrinkToFit();
}
if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
$styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
/** @var InternalEntityFactory Factory to create entities */
protected $entityFactory;

/** @var ManagerFactoryInterface $managerFactory Factory to create managers */
/** @var ManagerFactoryInterface Factory to create managers */
protected $managerFactory;

/** @var Worksheet The worksheet where data will be written to */
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 @@ -22,7 +22,7 @@ class ManagerFactory implements ManagerFactoryInterface
/** @var InternalEntityFactory */
protected $entityFactory;

/** @var HelperFactory $helperFactory */
/** @var HelperFactory */
protected $helperFactory;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Writer/WriterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class WriterAbstract implements WriterInterface
/** @var GlobalFunctionsHelper Helper to work with global functions */
protected $globalFunctionsHelper;

/** @var HelperFactory $helperFactory */
/** @var HelperFactory */
protected $helperFactory;

/** @var OptionsManagerInterface Writer options manager */
Expand Down
2 changes: 2 additions & 0 deletions src/Spout/Writer/WriterMultiSheetsAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
Expand Down Expand Up @@ -97,6 +98,7 @@ public function getSheets()
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet.
*
* @throws WriterNotOpenedException If the writer has not been opened yet
* @throws IOException If unable to open the sheet for writing
* @return Sheet The created sheet
*/
public function addNewSheetAndMakeItCurrent()
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Writer/XLSX/Creator/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ManagerFactory implements ManagerFactoryInterface
/** @var InternalEntityFactory */
protected $entityFactory;

/** @var HelperFactory $helperFactory */
/** @var HelperFactory */
protected $helperFactory;

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Spout/Writer/XLSX/Manager/OptionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ protected function getSupportedOptions()
Options::DEFAULT_ROW_STYLE,
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY,
Options::SHOULD_USE_INLINE_STRINGS,
Options::COLUMN_WIDTHS,
Options::MERGE_CELLS,
];
}

Expand All @@ -56,5 +58,7 @@ protected function setDefaultOptions()
$this->setOption(Options::DEFAULT_ROW_STYLE, $defaultRowStyle);
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
$this->setOption(Options::SHOULD_USE_INLINE_STRINGS, true);
$this->setOption(Options::COLUMN_WIDTHS, []);
$this->setOption(Options::MERGE_CELLS, []);
}
}
6 changes: 5 additions & 1 deletion src/Spout/Writer/XLSX/Manager/Style/StyleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function getCellXfsSectionContent()

$content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);

if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
if ($style->shouldApplyCellAlignment() || $style->shouldWrapText() || $style->shouldShrinkToFit()) {
$content .= ' applyAlignment="1">';
$content .= '<alignment';
if ($style->shouldApplyCellAlignment()) {
Expand All @@ -258,6 +258,10 @@ protected function getCellXfsSectionContent()
if ($style->shouldWrapText()) {
$content .= ' wrapText="1"';
}
if ($style->shouldShrinkToFit()) {
$content .= ' shrinkToFit="true"';
}

$content .= '/>';
$content .= '</xf>';
} else {
Expand Down
Loading