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

Commit 26ad590

Browse files
committed
Add support for default cell sizes for ODS files
1 parent 6db9871 commit 26ad590

File tree

3 files changed

+104
-11
lines changed

3 files changed

+104
-11
lines changed

src/Spout/Writer/ODS/Manager/Style/StyleManager.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Box\Spout\Common\Entity\Style\BorderPart;
66
use Box\Spout\Writer\Common\Entity\Worksheet;
7+
use Box\Spout\Writer\Common\Manager\ManagesCellSize;
78
use Box\Spout\Writer\ODS\Helper\BorderHelper;
89

910
/**
@@ -12,6 +13,8 @@
1213
*/
1314
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
1415
{
16+
use ManagesCellSize;
17+
1518
/** @var StyleRegistry */
1619
protected $styleRegistry;
1720

@@ -161,12 +164,16 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets)
161164
$content .= $this->getStyleSectionContent($style);
162165
}
163166

164-
$content .= <<<'EOD'
167+
$useOptimalRowHeight = empty($this->defaultRowHeight) ? 'true' : 'false';
168+
$defaultRowHeight = empty($this->defaultRowHeight) ? '15pt' : "{$this->defaultRowHeight}pt";
169+
$defaultColumnWidth = empty($this->defaultColumnWidth) ? '' : "style:column-width=\"{$this->defaultColumnWidth}pt\"";
170+
171+
$content .= <<<EOD
165172
<style:style style:family="table-column" style:name="co1">
166-
<style:table-column-properties fo:break-before="auto"/>
173+
<style:table-column-properties fo:break-before="auto" {$defaultColumnWidth}/>
167174
</style:style>
168175
<style:style style:family="table-row" style:name="ro1">
169-
<style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/>
176+
<style:table-row-properties fo:break-before="auto" style:row-height="{$defaultRowHeight}" style:use-optimal-row-height="{$useOptimalRowHeight}"/>
170177
</style:style>
171178
EOD;
172179

src/Spout/Writer/ODS/Manager/WorksheetManager.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Box\Spout\Common\Exception\IOException;
1010
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
1111
use Box\Spout\Common\Helper\StringHelper;
12+
use Box\Spout\Writer\Common\Entity\Options;
1213
use Box\Spout\Writer\Common\Entity\Worksheet;
1314
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
1415
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
@@ -39,17 +40,25 @@ class WorksheetManager implements WorksheetManagerInterface
3940
* @param StyleMerger $styleMerger
4041
* @param ODSEscaper $stringsEscaper
4142
* @param StringHelper $stringHelper
43+
* @param OptionsManager|null $optionsManager
4244
*/
4345
public function __construct(
4446
StyleManager $styleManager,
4547
StyleMerger $styleMerger,
4648
ODSEscaper $stringsEscaper,
47-
StringHelper $stringHelper
49+
StringHelper $stringHelper,
50+
$optionsManager = null
4851
) {
4952
$this->styleManager = $styleManager;
5053
$this->styleMerger = $styleMerger;
5154
$this->stringsEscaper = $stringsEscaper;
5255
$this->stringHelper = $stringHelper;
56+
57+
if ($optionsManager) {
58+
$this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH));
59+
$this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT));
60+
$this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? [];
61+
}
5362
}
5463

5564
/**
@@ -229,4 +238,39 @@ public function close(Worksheet $worksheet)
229238

230239
fclose($worksheetFilePointer);
231240
}
241+
242+
/**
243+
* @param float|null $width
244+
*/
245+
public function setDefaultColumnWidth($width)
246+
{
247+
$this->styleManager->setDefaultColumnWidth($width);
248+
}
249+
250+
/**
251+
* @param float|null $height
252+
*/
253+
public function setDefaultRowHeight($height)
254+
{
255+
$this->styleManager->setDefaultRowHeight($height);
256+
}
257+
258+
/**
259+
* @param float $width
260+
* @param array $columns One or more columns with this width
261+
*/
262+
public function setColumnWidth(float $width, ...$columns)
263+
{
264+
$this->styleManager->setColumnWidth($width, ...$columns);
265+
}
266+
267+
/**
268+
* @param float $width The width to set
269+
* @param int $start First column index of the range
270+
* @param int $end Last column index of the range
271+
*/
272+
public function setColumnWidthForRange(float $width, int $start, int $end)
273+
{
274+
$this->styleManager->setColumnWidthForRange($width, $start, $end);
275+
}
232276
}

tests/Spout/Writer/ODS/SheetTest.php

+49-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
77
use Box\Spout\Writer\Common\Entity\Sheet;
88
use Box\Spout\Writer\Exception\InvalidSheetNameException;
9+
use Box\Spout\Writer\Exception\WriterNotOpenedException;
910
use Box\Spout\Writer\RowCreationHelper;
1011
use PHPUnit\Framework\TestCase;
1112

@@ -82,7 +83,7 @@ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed()
8283
*/
8384
public function testSetSheetVisibilityShouldCreateSheetHidden()
8485
{
85-
$fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx';
86+
$fileName = 'test_set_visibility_should_create_sheet_hidden.ods';
8687
$this->writeDataToHiddenSheet($fileName);
8788

8889
$resourcePath = $this->getGeneratedResourcePath($fileName);
@@ -92,19 +93,60 @@ public function testSetSheetVisibilityShouldCreateSheetHidden()
9293
$this->assertContains(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
9394
}
9495

95-
/**
96-
* @param string $fileName
97-
* @param string $sheetName
98-
* @return Sheet
99-
*/
100-
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
96+
function testThrowsIfWorkbookIsNotInitialized()
97+
{
98+
$this->expectException(WriterNotOpenedException::class);
99+
$writer = WriterEntityFactory::createODSWriter();
100+
101+
$writer->addRow($this->createRowFromValues([]));
102+
}
103+
104+
public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded()
105+
{
106+
$this->expectException(WriterNotOpenedException::class);
107+
$writer = WriterEntityFactory::createXLSXWriter();
108+
$writer->setDefaultColumnWidth(10.0);
109+
}
110+
111+
public function testWritesDefaultCellSizesIfSet()
112+
{
113+
$fileName = 'test_writes_default_cell_sizes_if_set.ods';
114+
$writer = $this->writerForFile($fileName);
115+
116+
$writer->setDefaultColumnWidth(100.0);
117+
$writer->setDefaultRowHeight(20.0);
118+
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
119+
$writer->close();
120+
121+
$resourcePath = $this->getGeneratedResourcePath($fileName);
122+
$pathToWorkbookFile = $resourcePath . '#content.xml';
123+
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
124+
125+
$this->assertContains(' style:column-width="100pt"', $xmlContents, 'No default col width found in sheet');
126+
$this->assertContains(' style:row-height="20pt"', $xmlContents, 'No default row height found in sheet');
127+
$this->assertContains(' style:use-optimal-row-height="false', $xmlContents, 'No optimal row height override found in sheet');
128+
}
129+
130+
private function writerForFile($fileName)
101131
{
102132
$this->createGeneratedFolderIfNeeded($fileName);
103133
$resourcePath = $this->getGeneratedResourcePath($fileName);
104134

105135
$writer = WriterEntityFactory::createODSWriter();
106136
$writer->openToFile($resourcePath);
107137

138+
return $writer;
139+
}
140+
141+
/**
142+
* @param string $fileName
143+
* @param string $sheetName
144+
* @return void
145+
*/
146+
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
147+
{
148+
$writer = $this->writerForFile($fileName);
149+
108150
$sheet = $writer->getCurrentSheet();
109151
$sheet->setName($sheetName);
110152

0 commit comments

Comments
 (0)