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

Commit

Permalink
Add support for default cell sizes for ODS files
Browse files Browse the repository at this point in the history
  • Loading branch information
aphofstede committed Dec 17, 2019
1 parent 6db9871 commit 26ad590
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/Spout/Writer/ODS/Manager/Style/StyleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Box\Spout\Common\Entity\Style\BorderPart;
use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\ManagesCellSize;
use Box\Spout\Writer\ODS\Helper\BorderHelper;

/**
Expand All @@ -12,6 +13,8 @@
*/
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
{
use ManagesCellSize;

/** @var StyleRegistry */
protected $styleRegistry;

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

$content .= <<<'EOD'
$useOptimalRowHeight = empty($this->defaultRowHeight) ? 'true' : 'false';
$defaultRowHeight = empty($this->defaultRowHeight) ? '15pt' : "{$this->defaultRowHeight}pt";
$defaultColumnWidth = empty($this->defaultColumnWidth) ? '' : "style:column-width=\"{$this->defaultColumnWidth}pt\"";

$content .= <<<EOD
<style:style style:family="table-column" style:name="co1">
<style:table-column-properties fo:break-before="auto"/>
<style:table-column-properties fo:break-before="auto" {$defaultColumnWidth}/>
</style:style>
<style:style style:family="table-row" style:name="ro1">
<style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/>
<style:table-row-properties fo:break-before="auto" style:row-height="{$defaultRowHeight}" style:use-optimal-row-height="{$useOptimalRowHeight}"/>
</style:style>
EOD;

Expand Down
46 changes: 45 additions & 1 deletion src/Spout/Writer/ODS/Manager/WorksheetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
Expand Down Expand Up @@ -39,17 +40,25 @@ class WorksheetManager implements WorksheetManagerInterface
* @param StyleMerger $styleMerger
* @param ODSEscaper $stringsEscaper
* @param StringHelper $stringHelper
* @param OptionsManager|null $optionsManager
*/
public function __construct(
StyleManager $styleManager,
StyleMerger $styleMerger,
ODSEscaper $stringsEscaper,
StringHelper $stringHelper
StringHelper $stringHelper,
$optionsManager = null
) {
$this->styleManager = $styleManager;
$this->styleMerger = $styleMerger;
$this->stringsEscaper = $stringsEscaper;
$this->stringHelper = $stringHelper;

if ($optionsManager) {
$this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH));
$this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT));
$this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? [];
}
}

/**
Expand Down Expand Up @@ -229,4 +238,39 @@ public function close(Worksheet $worksheet)

fclose($worksheetFilePointer);
}

/**
* @param float|null $width

This comment has been minimized.

Copy link
@joseraul

joseraul Dec 20, 2019

You have some @params like this float|null $width here width public function setDefaultColumnWidth(float $width): https://github.com/box/spout/compare/master...aphofstede:custom-column-widths?expand=1#diff-3ebea09d8780d7e83288263911c1e386R294 I guess the PHPSTORM added the null

This comment has been minimized.

Copy link
@aphofstede

aphofstede Dec 20, 2019

Author

Good point, added the typehint because you shouldn't be actively setting it to null here. Removed the null from phpdoc for these ones.

*/
public function setDefaultColumnWidth($width)
{
$this->styleManager->setDefaultColumnWidth($width);
}

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

/**
* @param float $width
* @param array $columns One or more columns with this width
*/
public function setColumnWidth(float $width, ...$columns)
{
$this->styleManager->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->styleManager->setColumnWidthForRange($width, $start, $end);
}
}
56 changes: 49 additions & 7 deletions tests/Spout/Writer/ODS/SheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Exception\InvalidSheetNameException;
use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -82,7 +83,7 @@ public function testSetSheetNameShouldThrowWhenNameIsAlreadyUsed()
*/
public function testSetSheetVisibilityShouldCreateSheetHidden()
{
$fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx';
$fileName = 'test_set_visibility_should_create_sheet_hidden.ods';
$this->writeDataToHiddenSheet($fileName);

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

/**
* @param string $fileName
* @param string $sheetName
* @return Sheet
*/
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
function testThrowsIfWorkbookIsNotInitialized()
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createODSWriter();

$writer->addRow($this->createRowFromValues([]));
}

public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded()
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setDefaultColumnWidth(10.0);
}

public function testWritesDefaultCellSizesIfSet()
{
$fileName = 'test_writes_default_cell_sizes_if_set.ods';
$writer = $this->writerForFile($fileName);

$writer->setDefaultColumnWidth(100.0);
$writer->setDefaultRowHeight(20.0);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
$writer->close();

$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#content.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);

$this->assertContains(' style:column-width="100pt"', $xmlContents, 'No default col width found in sheet');
$this->assertContains(' style:row-height="20pt"', $xmlContents, 'No default row height found in sheet');
$this->assertContains(' style:use-optimal-row-height="false', $xmlContents, 'No optimal row height override found in sheet');
}

private function writerForFile($fileName)
{
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);

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

return $writer;
}

/**
* @param string $fileName
* @param string $sheetName
* @return void
*/
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
{
$writer = $this->writerForFile($fileName);

$sheet = $writer->getCurrentSheet();
$sheet->setName($sheetName);

Expand Down

0 comments on commit 26ad590

Please sign in to comment.