Add support for default cell sizes for ODS files
This commit is contained in:
parent
6db9871722
commit
26ad59033c
@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS\Manager\Style;
|
|||||||
|
|
||||||
use Box\Spout\Common\Entity\Style\BorderPart;
|
use Box\Spout\Common\Entity\Style\BorderPart;
|
||||||
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\ODS\Helper\BorderHelper;
|
use Box\Spout\Writer\ODS\Helper\BorderHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,6 +13,8 @@ use Box\Spout\Writer\ODS\Helper\BorderHelper;
|
|||||||
*/
|
*/
|
||||||
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
||||||
{
|
{
|
||||||
|
use ManagesCellSize;
|
||||||
|
|
||||||
/** @var StyleRegistry */
|
/** @var StyleRegistry */
|
||||||
protected $styleRegistry;
|
protected $styleRegistry;
|
||||||
|
|
||||||
@ -161,12 +164,16 @@ EOD;
|
|||||||
$content .= $this->getStyleSectionContent($style);
|
$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: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:style style:family="table-row" style:name="ro1">
|
<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>
|
</style:style>
|
||||||
EOD;
|
EOD;
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ use Box\Spout\Common\Exception\InvalidArgumentException;
|
|||||||
use Box\Spout\Common\Exception\IOException;
|
use Box\Spout\Common\Exception\IOException;
|
||||||
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
|
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
|
||||||
use Box\Spout\Common\Helper\StringHelper;
|
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\Entity\Worksheet;
|
||||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||||
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
|
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
|
||||||
@ -39,17 +40,25 @@ 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) ?? [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -229,4 +238,39 @@ class WorksheetManager implements WorksheetManagerInterface
|
|||||||
|
|
||||||
fclose($worksheetFilePointer);
|
fclose($worksheetFilePointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param float|null $width
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ use Box\Spout\TestUsingResource;
|
|||||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||||
use Box\Spout\Writer\Exception\InvalidSheetNameException;
|
use Box\Spout\Writer\Exception\InvalidSheetNameException;
|
||||||
|
use Box\Spout\Writer\Exception\WriterNotOpenedException;
|
||||||
use Box\Spout\Writer\RowCreationHelper;
|
use Box\Spout\Writer\RowCreationHelper;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@ -82,7 +83,7 @@ class SheetTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testSetSheetVisibilityShouldCreateSheetHidden()
|
public function testSetSheetVisibilityShouldCreateSheetHidden()
|
||||||
{
|
{
|
||||||
$fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx';
|
$fileName = 'test_set_visibility_should_create_sheet_hidden.ods';
|
||||||
$this->writeDataToHiddenSheet($fileName);
|
$this->writeDataToHiddenSheet($fileName);
|
||||||
|
|
||||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||||
@ -92,12 +93,41 @@ class SheetTest extends TestCase
|
|||||||
$this->assertContains(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
|
$this->assertContains(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function testThrowsIfWorkbookIsNotInitialized()
|
||||||
* @param string $fileName
|
{
|
||||||
* @param string $sheetName
|
$this->expectException(WriterNotOpenedException::class);
|
||||||
* @return Sheet
|
$writer = WriterEntityFactory::createODSWriter();
|
||||||
*/
|
|
||||||
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
|
$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);
|
$this->createGeneratedFolderIfNeeded($fileName);
|
||||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||||
@ -105,6 +135,18 @@ class SheetTest extends TestCase
|
|||||||
$writer = WriterEntityFactory::createODSWriter();
|
$writer = WriterEntityFactory::createODSWriter();
|
||||||
$writer->openToFile($resourcePath);
|
$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 = $writer->getCurrentSheet();
|
||||||
$sheet->setName($sheetName);
|
$sheet->setName($sheetName);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user