Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for clear old column widths #3

Open
wants to merge 17 commits into
base: custom-column-widths
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ after_script:
if [[ "$WITH_COVERAGE" == "true" ]]; then
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover build/logs/coverage.clover
fi
fi
44 changes: 44 additions & 0 deletions src/Spout/Common/Entity/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Row
{
public const DEFAULT_HEIGHT = 15;

/**
* The cells in this row
* @var Cell[]
Expand All @@ -18,6 +20,18 @@ class Row
*/
protected $style;

/**
* Row height
* @var float
*/
protected $height = self::DEFAULT_HEIGHT;

/**
* Whether the height property was set
* @var bool
*/
protected $hasSetHeight = false;

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

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

return $this;
}

/**
* Returns row height
* @return float
*/
public function getHeight()
{
return $this->height;
}

/**
* @return bool
*/
public function hasSetHeight(): bool
{
return $this->hasSetHeight;
}
}
34 changes: 34 additions & 0 deletions src/Spout/Common/Entity/Style/CellVerticalAlignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Box\Spout\Common\Entity\Style;

/**
* Class Vertical Alignment
* This class provides constants to work with text vertical alignment.
*/
abstract class CellVerticalAlignment
{
const TOP = 'top';
const CENTER = 'center';
const BOTTOM = 'bottom';
const JUSTIFY = 'justify';
const DISTRIBUTED = 'distributed';

private static $VALID_VERTICAL_ALIGNMENTS = [
self::TOP => 1,
self::CENTER => 1,
self::BOTTOM => 1,
self::JUSTIFY => 1,
self::DISTRIBUTED => 1,
];

/**
* @param string $cellVerticalAlignment
*
* @return bool Whether the given cell vertical alignment is valid
*/
public static function isValid($cellVerticalAlignment)
{
return isset(self::$VALID_VERTICAL_ALIGNMENTS[$cellVerticalAlignment]);
}
}
81 changes: 81 additions & 0 deletions src/Spout/Common/Entity/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ class Style
/** @var bool Whether the cell alignment property was set */
private $hasSetCellAlignment = false;

/** @var bool Whether specific cell alignment should be applied */
private $shouldApplyCellVerticalAlignment = false;
/** @var string Cell alignment */
private $cellVerticalAlignment;
/** @var bool Whether the cell alignment property was set */
private $hasSetCellVerticalAlignment = false;

/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
private $shouldWrapText = false;
/** @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 @@ -385,6 +397,45 @@ public function shouldApplyCellAlignment()
return $this->shouldApplyCellAlignment;
}

/**
* @return string
*/
public function getCellVerticalAlignment()
{
return $this->cellVerticalAlignment;
}

/**
* @param string $cellVerticalAlignment The cell vertical alignment
*
* @return Style
*/
public function setCellVerticalAlignment($cellVerticalAlignment)
{
$this->cellVerticalAlignment = $cellVerticalAlignment;
$this->hasSetCellVerticalAlignment = true;
$this->shouldApplyCellVerticalAlignment = true;
$this->isEmpty = false;

return $this;
}

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

/**
* @return bool Whether specific cell vertical alignment should be applied
*/
public function shouldApplyCellVerticalAlignment()
{
return $this->shouldApplyCellVerticalAlignment;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -482,6 +533,36 @@ 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;
$this->isEmpty = false;

return $this;
}

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

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

/**
* @return bool
*/
Expand Down
33 changes: 33 additions & 0 deletions src/Spout/Writer/Common/Creator/Style/StyleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\CellVerticalAlignment;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Common\Exception\InvalidArgumentException;

Expand Down Expand Up @@ -143,6 +144,25 @@ public function setCellAlignment($cellAlignment)
return $this;
}

/**
* Sets the cell vertical alignment.
*
* @param string $cellVerticalAlignment The cell vertical alignment
*
* @throws InvalidArgumentException If the given cell vertical alignment is not valid
* @return StyleBuilder
*/
public function setCellVerticalAlignment($cellVerticalAlignment)
{
if (!CellVerticalAlignment::isValid($cellVerticalAlignment)) {
throw new InvalidArgumentException('Invalid cell vertical alignment value');
}

$this->style->setCellVerticalAlignment($cellVerticalAlignment);

return $this;
}

/**
* Set a border
*
Expand Down Expand Up @@ -183,6 +203,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
48 changes: 48 additions & 0 deletions src/Spout/Writer/Common/Entity/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Box\Spout\Writer\Common\Entity;

use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Manager\SheetManager;

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

/** @var array merge cell */
private array $mergeRanges = [];

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

return $this;
}

/**
* @return array
*/
public function getMergeRanges()
{
return $this->mergeRanges;
}

/**
* @param array $mergeRanges
* @return array
*/
public function setMergeRanges(array $mergeRanges)
{
return $this->mergeRanges = $mergeRanges;
}

/**
* @param $mergeRanges
* @return array
*/
public function addMergeRanges(string ...$mergeRanges)
{
return $this->mergeRanges = array_merge($this->mergeRanges, $mergeRanges);
}

/**
* @param int $fromColumnIndexZeroBased
* @param int $fromRowIndexOneBased
* @param int $toColumnIndexZeroBased
* @param int $toRowIndexOneBased
* @return array
*/
public function addMergeRangeByIndexes(
int $fromColumnIndexZeroBased,
int $fromRowIndexOneBased,
int $toColumnIndexZeroBased,
int $toRowIndexOneBased
) {
$fromLetter = CellHelper::getColumnLettersFromColumnIndex($fromColumnIndexZeroBased);
$toLetter = CellHelper::getColumnLettersFromColumnIndex($toColumnIndexZeroBased);
return $this->addMergeRanges("{$fromLetter}{$fromRowIndexOneBased}:{$toLetter}{$toRowIndexOneBased}");
}
}
8 changes: 8 additions & 0 deletions src/Spout/Writer/Common/Manager/ManagesCellSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function setDefaultRowHeight($height)
$this->defaultRowHeight = $height;
}

/**
* Clear old column widths when we want to start new sheet
*/
public function clearColumnWidths()
{
$this->columnWidths = [];
}

/**
* @param float $width
* @param array $columns One or more columns with this width
Expand Down
6 changes: 6 additions & 0 deletions src/Spout/Writer/Common/Manager/Style/StyleMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ 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());
}
if (!$style->hasSetCellVerticalAlignment() && $baseStyle->shouldApplyCellVerticalAlignment()) {
$styleToUpdate->setCellVerticalAlignment($baseStyle->getCellVerticalAlignment());
}
if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) {
$styleToUpdate->setBorder($baseStyle->getBorder());
}
Expand Down
8 changes: 8 additions & 0 deletions src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ public function setDefaultRowHeight(float $height)
$this->worksheetManager->setDefaultRowHeight($height);
}

/**
* Clear old column widths when we want to start new sheet
*/
public function clearColumnWidths()
{
$this->worksheetManager->clearColumnWidths();
}

/**
* @param float $width
* @param array $columns One or more columns with this width
Expand Down
5 changes: 5 additions & 0 deletions src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function setDefaultColumnWidth($width);
*/
public function setDefaultRowHeight($height);

/**
* Clear old column widths when we want to start new sheet
*/
public function clearColumnWidths();

/**
* @param float $width
* @param array $columns One or more columns with this width
Expand Down
Loading