Skip to content

Commit 6700502

Browse files
quamisSlamdunk
authored andcommitted
Added support for mergeCells, cell height, shrink to fit
Added support for mergeCells: // mergeCells (B2:G2), you may use CellHelper::getColumnLettersFromColumnIndex() to convert from "B2" to "[1,2]" $writer->mergeCells([1,2], [6, 2]); cell height: $row->setHeight(30); shouldShrinkToFit: $style->setShouldShrinkToFit(); These changes are implemented for XLSX as that's what I need and test spout on.
1 parent bb9aedb commit 6700502

File tree

10 files changed

+145
-0
lines changed

10 files changed

+145
-0
lines changed

src/Common/Entity/Row.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Row
2020
*/
2121
protected $style;
2222

23+
/**
24+
* Row height (default is 15)
25+
* @var string
26+
*/
27+
protected $height = "15";
28+
2329
/**
2430
* Row constructor.
2531
*
@@ -132,4 +138,24 @@ public function toArray()
132138
return $cell->getValue();
133139
}, $this->cells);
134140
}
141+
142+
/**
143+
* Set row height
144+
* @param String $height
145+
* @return Row
146+
*/
147+
public function setHeight($height)
148+
{
149+
$this->height = $height;
150+
return $this;
151+
}
152+
153+
/**
154+
* Returns row height
155+
* @return String
156+
*/
157+
public function getHeight()
158+
{
159+
return $this->height;
160+
}
135161
}

src/Common/Entity/Style/Style.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class Style
6565
/** @var bool Whether the wrap text property was set */
6666
private $hasSetWrapText = false;
6767

68+
private $shrinkToFit = false;
69+
private $shouldShrinkToFit = false;
70+
6871
/** @var null|Border */
6972
private $border;
7073

@@ -510,4 +513,24 @@ public function isEmpty(): bool
510513
{
511514
return $this->isEmpty;
512515
}
516+
517+
/**
518+
* Sets should shrink to fit
519+
* @param bool $shrinkToFit
520+
* @return Style
521+
*/
522+
public function setShouldShrinkToFit($shrinkToFit = true)
523+
{
524+
$this->shrinkToFit = $shrinkToFit;
525+
$this->shouldShrinkToFit = $shrinkToFit;
526+
return $this;
527+
}
528+
529+
/**
530+
* @return bool Whether format should be applied
531+
*/
532+
public function shouldShrinkToFit()
533+
{
534+
return $this->shouldShrinkToFit;
535+
}
513536
}

src/Common/Manager/OptionsManagerAbstract.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public function setOption($optionName, $optionValue)
3434
}
3535
}
3636

37+
/**
38+
* Add an option to the internal list of options
39+
* Used only for mergeCells() for now
40+
* @return void
41+
*/
42+
public function addOption($optionName, $optionValue)
43+
{
44+
if (\in_array($optionName, $this->supportedOptions)) {
45+
if (!isset($this->options[$optionName])) {
46+
$this->options[$optionName] = [];
47+
}
48+
$this->options[$optionName][] = $optionValue;
49+
}
50+
}
51+
3752
/**
3853
* @param string $optionName
3954
*

src/Writer/Common/Creator/Style/StyleBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ public function setFormat($format)
186186
return $this;
187187
}
188188

189+
/**
190+
* Set should shrink to fit
191+
* @param boolean $shrinkToFit
192+
* @return void
193+
*/
194+
public function setShouldShrinkToFit($shrinkToFit = true)
195+
{
196+
$this->style->setShouldShrinkToFit($shrinkToFit);
197+
return $this;
198+
}
199+
189200
/**
190201
* Returns the configured style. The style is cached and can be reused.
191202
*

src/Writer/Common/Entity/Options.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ abstract class Options
2424
public const DEFAULT_COLUMN_WIDTH = 'defaultColumnWidth';
2525
public const DEFAULT_ROW_HEIGHT = 'defaultRowHeight';
2626
public const COLUMN_WIDTHS = 'columnWidthDefinition';
27+
28+
// XLSX merge cells
29+
const MERGE_CELLS = 'mergeCells';
2730
}

src/Writer/Common/Entity/Sheet.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ public function setIsVisible($isVisible)
115115
return $this;
116116
}
117117

118+
/**
119+
* @return bool isSheetStarted Sheet was started
120+
*/
121+
public function isSheetStarted()
122+
{
123+
return $this->isSheetStarted;
124+
}
125+
126+
/**
127+
* @param bool $isSheetStarted Set if sheet was started
128+
* @return Sheet
129+
*/
130+
public function setIsSheetStarted($isSheetStarted)
131+
{
132+
$this->isSheetStarted = $isSheetStarted;
133+
134+
return $this;
135+
}
136+
118137
public function getSheetView(): ?SheetView
119138
{
120139
return $this->sheetView;

src/Writer/WriterMultiSheetsAbstract.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAuto
5555
return $this;
5656
}
5757

58+
/**
59+
* Set columns widths as list. If value is null will set column with default width (8.43)
60+
* @param array $columnWidths
61+
* @return WriterMultiSheetsAbstract
62+
*/
63+
public function setColumnWidths(array $columnWidths)
64+
{
65+
$this->optionsManager->setOption(Options::COLUMN_WIDTHS, $columnWidths);
66+
return $this;
67+
}
68+
69+
/**
70+
* Undocumented function
71+
*
72+
* @param array $range
73+
* @return void
74+
*/
75+
public function mergeCells(array $range1, array $range2)
76+
{
77+
$this->optionsManager->addOption(Options::MERGE_CELLS, [$range1, $range2]);
78+
return $this;
79+
}
80+
5881
/**
5982
* Returns all the workbook's sheets.
6083
*

src/Writer/XLSX/Manager/OptionsManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function getSupportedOptions()
4040
Options::DEFAULT_COLUMN_WIDTH,
4141
Options::DEFAULT_ROW_HEIGHT,
4242
Options::COLUMN_WIDTHS,
43+
Options::MERGE_CELLS,
4344
];
4445
}
4546

@@ -58,5 +59,7 @@ protected function setDefaultOptions()
5859
$this->setOption(Options::DEFAULT_ROW_STYLE, $defaultRowStyle);
5960
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
6061
$this->setOption(Options::SHOULD_USE_INLINE_STRINGS, true);
62+
$this->setOption(Options::COLUMN_WIDTHS, []);
63+
$this->setOption(Options::MERGE_CELLS, []);
6164
}
6265
}

src/Writer/XLSX/Manager/Style/StyleManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ protected function getCellXfsSectionContent()
259259
if ($style->shouldWrapText()) {
260260
$content .= ' wrapText="1"';
261261
}
262+
if ($style->shouldShrinkToFit()) {
263+
$content .= ' shrinkToFit="true"';
264+
}
265+
262266
$content .= '/>';
263267
$content .= '</xf>';
264268
} else {

src/Writer/XLSX/Manager/WorksheetManager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class WorksheetManager implements WorksheetManagerInterface
4242
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
4343
EOD;
4444

45+
/** @var OptionsManagerInterface */
46+
private $optionsManager;
47+
4548
/** @var bool Whether inline or shared strings should be used */
4649
protected $shouldUseInlineStrings;
4750

@@ -75,6 +78,7 @@ public function __construct(
7578
XLSXEscaper $stringsEscaper,
7679
StringHelper $stringHelper
7780
) {
81+
$this->optionsManager = $optionsManager;
7882
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
7983
$this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH));
8084
$this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT));
@@ -169,6 +173,20 @@ public function close(Worksheet $worksheet)
169173
}
170174
$this->ensureSheetDataStated($worksheet);
171175
fwrite($worksheetFilePointer, '</sheetData>');
176+
177+
// create nodes for merge cells
178+
if ($this->optionsManager->getOption(Options::MERGE_CELLS)) {
179+
$mergeCellString = '<mergeCells count="'.count($this->optionsManager->getOption(Options::MERGE_CELLS)).'">';
180+
foreach ($this->optionsManager->getOption(Options::MERGE_CELLS) as $values) {
181+
$output = array_map(function($value){
182+
return CellHelper::getColumnLettersFromColumnIndex($value[0]) . $value[1];
183+
}, $values);
184+
$mergeCellString.= '<mergeCell ref="'.implode(':', $output).'"/>';
185+
}
186+
$mergeCellString.= '</mergeCells>';
187+
\fwrite($worksheet->getFilePointer(), $mergeCellString);
188+
}
189+
172190
fwrite($worksheetFilePointer, '</worksheet>');
173191
fclose($worksheetFilePointer);
174192
}

0 commit comments

Comments
 (0)