Fix ODS column widths

This commit is contained in:
Martin Wind 2020-06-13 20:43:55 +02:00
parent 1eee2ea5db
commit 9727bec850
6 changed files with 27 additions and 27 deletions

View File

@ -93,7 +93,7 @@ class ManagerFactory implements ManagerFactoryInterface
{ {
$styleRegistry = $this->createStyleRegistry($optionsManager); $styleRegistry = $this->createStyleRegistry($optionsManager);
return new StyleManager($styleRegistry); return new StyleManager($styleRegistry, $optionsManager);
} }
/** /**

View File

@ -34,6 +34,9 @@ class OptionsManager extends OptionsManagerAbstract
Options::TEMP_FOLDER, Options::TEMP_FOLDER,
Options::DEFAULT_ROW_STYLE, Options::DEFAULT_ROW_STYLE,
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY,
Options::DEFAULT_COLUMN_WIDTH,
Options::DEFAULT_ROW_HEIGHT,
Options::COLUMN_WIDTHS,
]; ];
} }

View File

@ -4,6 +4,8 @@ namespace Box\Spout\Writer\ODS\Manager\Style;
use Box\Spout\Common\Entity\Style\BorderPart; use Box\Spout\Common\Entity\Style\BorderPart;
use Box\Spout\Common\Entity\Style\CellAlignment; use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\ManagesCellSize; use Box\Spout\Writer\Common\Manager\ManagesCellSize;
use Box\Spout\Writer\ODS\Helper\BorderHelper; use Box\Spout\Writer\ODS\Helper\BorderHelper;
@ -19,6 +21,17 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
/** @var StyleRegistry */ /** @var StyleRegistry */
protected $styleRegistry; protected $styleRegistry;
/**
* @param StyleRegistry $styleRegistry
*/
public function __construct(StyleRegistry $styleRegistry, OptionsManagerInterface $optionsManager)
{
parent::__construct($styleRegistry);
$this->setDefaultColumnWidth($optionsManager->getOption(Options::DEFAULT_COLUMN_WIDTH));
$this->setDefaultRowHeight($optionsManager->getOption(Options::DEFAULT_ROW_HEIGHT));
$this->columnWidths = $optionsManager->getOption(Options::COLUMN_WIDTHS) ?? [];
}
/** /**
* Returns the content of the "styles.xml" file, given a list of styles. * Returns the content of the "styles.xml" file, given a list of styles.
* *

View File

@ -40,25 +40,17 @@ class WorksheetManager implements WorksheetManagerInterface
* @param StyleMerger $styleMerger * @param StyleMerger $styleMerger
* @param ODSEscaper $stringsEscaper * @param ODSEscaper $stringsEscaper
* @param StringHelper $stringHelper * @param StringHelper $stringHelper
* @param OptionsManager|null $optionsManager
*/ */
public function __construct( public function __construct(
StyleManager $styleManager, StyleManager $styleManager,
StyleMerger $styleMerger, StyleMerger $styleMerger,
ODSEscaper $stringsEscaper, ODSEscaper $stringsEscaper,
StringHelper $stringHelper, StringHelper $stringHelper
$optionsManager = null
) { ) {
$this->styleManager = $styleManager; $this->styleManager = $styleManager;
$this->styleMerger = $styleMerger; $this->styleMerger = $styleMerger;
$this->stringsEscaper = $stringsEscaper; $this->stringsEscaper = $stringsEscaper;
$this->stringHelper = $stringHelper; $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) ?? [];
}
} }
/** /**

View File

@ -101,20 +101,18 @@ class SheetTest extends TestCase
$writer->addRow($this->createRowFromValues([])); $writer->addRow($this->createRowFromValues([]));
} }
public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded()
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setDefaultColumnWidth(10.0);
}
public function testWritesDefaultCellSizesIfSet() public function testWritesDefaultCellSizesIfSet()
{ {
$fileName = 'test_writes_default_cell_sizes_if_set.ods'; $fileName = 'test_writes_default_cell_sizes_if_set.ods';
$writer = $this->writerForFile($fileName);
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterEntityFactory::createODSWriter();
$writer->setDefaultColumnWidth(100.0); $writer->setDefaultColumnWidth(100.0);
$writer->setDefaultRowHeight(20.0); $writer->setDefaultRowHeight(20.0);
$writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
$writer->close(); $writer->close();

View File

@ -101,13 +101,6 @@ class SheetTest extends TestCase
$writer->addRow($this->createRowFromValues([])); $writer->addRow($this->createRowFromValues([]));
} }
public function testThrowsWhenTryingToSetDefaultsBeforeWorkbookLoaded()
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setDefaultColumnWidth(10.0);
}
public function testWritesDefaultCellSizesIfSet() public function testWritesDefaultCellSizesIfSet()
{ {
$fileName = 'test_writes_default_cell_sizes_if_set.xlsx'; $fileName = 'test_writes_default_cell_sizes_if_set.xlsx';
@ -115,9 +108,9 @@ class SheetTest extends TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterEntityFactory::createXLSXWriter(); $writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$writer->setDefaultColumnWidth(10.0); $writer->setDefaultColumnWidth(10.0);
$writer->setDefaultRowHeight(20.0); $writer->setDefaultRowHeight(20.0);
$writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
$writer->close(); $writer->close();
@ -137,8 +130,9 @@ class SheetTest extends TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterEntityFactory::createXLSXWriter(); $writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$writer->setDefaultColumnWidth(10.0); $writer->setDefaultColumnWidth(10.0);
$writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
$writer->close(); $writer->close();