Merge pull request #2 from bizquiz/custom-column-widths
Fix ODS column widths
This commit is contained in:
commit
557b6fd158
@ -93,7 +93,7 @@ class ManagerFactory implements ManagerFactoryInterface
|
||||
{
|
||||
$styleRegistry = $this->createStyleRegistry($optionsManager);
|
||||
|
||||
return new StyleManager($styleRegistry);
|
||||
return new StyleManager($styleRegistry, $optionsManager);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,6 +34,9 @@ class OptionsManager extends OptionsManagerAbstract
|
||||
Options::TEMP_FOLDER,
|
||||
Options::DEFAULT_ROW_STYLE,
|
||||
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY,
|
||||
Options::DEFAULT_COLUMN_WIDTH,
|
||||
Options::DEFAULT_ROW_HEIGHT,
|
||||
Options::COLUMN_WIDTHS,
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ namespace Box\Spout\Writer\ODS\Manager\Style;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\BorderPart;
|
||||
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\Manager\ManagesCellSize;
|
||||
use Box\Spout\Writer\ODS\Helper\BorderHelper;
|
||||
@ -19,6 +21,17 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
||||
/** @var 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.
|
||||
*
|
||||
|
@ -40,25 +40,17 @@ 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,
|
||||
$optionsManager = null
|
||||
StringHelper $stringHelper
|
||||
) {
|
||||
$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) ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,20 +101,18 @@ class SheetTest extends TestCase
|
||||
$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);
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createODSWriter();
|
||||
$writer->setDefaultColumnWidth(100.0);
|
||||
$writer->setDefaultRowHeight(20.0);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
|
||||
$writer->close();
|
||||
|
||||
|
@ -101,13 +101,6 @@ class SheetTest extends TestCase
|
||||
$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.xlsx';
|
||||
@ -115,9 +108,9 @@ class SheetTest extends TestCase
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setDefaultColumnWidth(10.0);
|
||||
$writer->setDefaultRowHeight(20.0);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
@ -137,8 +130,9 @@ class SheetTest extends TestCase
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setDefaultColumnWidth(10.0);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user