Add column width options
This commit is contained in:
1 parent
e95e0eeefd
commit
3a0ce0e059
16 files changed
+664
-16
No files matched your search
@@ -6,6 +6,7 @@ use Box\Spout\TestUsingResource;
|
||||
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;
|
||||
|
||||
@@ -82,7 +83,7 @@ class SheetTest extends TestCase
|
||||
*/
|
||||
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);
|
||||
@@ -92,6 +93,130 @@ class SheetTest extends TestCase
|
||||
$this->assertStringContainsString(' table:display="false"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
|
||||
}
|
||||
|
||||
public function testThrowsIfWorkbookIsNotInitialized()
|
||||
{
|
||||
$this->expectException(WriterNotOpenedException::class);
|
||||
$writer = WriterEntityFactory::createODSWriter();
|
||||
|
||||
$writer->addRow($this->createRowFromValues([]));
|
||||
}
|
||||
|
||||
public function testWritesDefaultCellSizesIfSet()
|
||||
{
|
||||
$fileName = 'test_writes_default_cell_sizes_if_set.ods';
|
||||
|
||||
$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();
|
||||
|
||||
$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');
|
||||
}
|
||||
|
||||
public function testWritesColumnWidths()
|
||||
{
|
||||
$fileName = 'test_column_widths.ods';
|
||||
$writer = $this->writerForFile($fileName);
|
||||
|
||||
$writer->setColumnWidth(100.0, 1);
|
||||
$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:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style definition found in sheet');
|
||||
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
|
||||
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
|
||||
$this->assertContains('table:number-columns-repeated="1"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
|
||||
}
|
||||
|
||||
public function testWritesMultipleColumnWidths()
|
||||
{
|
||||
$fileName = 'test_multiple_column_widths.ods';
|
||||
$writer = $this->writerForFile($fileName);
|
||||
|
||||
$writer->setColumnWidth(100.0, 1, 2, 3);
|
||||
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13']));
|
||||
$writer->close();
|
||||
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToWorkbookFile = $resourcePath . '#content.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style definition found in sheet');
|
||||
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
|
||||
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
|
||||
$this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
|
||||
}
|
||||
|
||||
public function testWritesMultipleColumnWidthsInRanges()
|
||||
{
|
||||
$fileName = 'test_multiple_column_widths_in_ranges.ods';
|
||||
$writer = $this->writerForFile($fileName);
|
||||
|
||||
$writer->setColumnWidth(50.0, 1, 3, 4, 6);
|
||||
$writer->setColumnWidth(100.0, 2, 5);
|
||||
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13', 'ods--14', 'ods--15', 'ods--16']));
|
||||
$writer->close();
|
||||
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToWorkbookFile = $resourcePath . '#content.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style 0 definition found in sheet');
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co1">', $xmlContents, 'No matching custom col style 1 definition found in sheet');
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co2">', $xmlContents, 'No matching custom col style 2 definition found in sheet');
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co3">', $xmlContents, 'No matching custom col style 3 definition found in sheet');
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co4">', $xmlContents, 'No matching custom col style 4 definition found in sheet');
|
||||
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="100pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
|
||||
$this->assertContains('<style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width="50pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
|
||||
$this->assertContains('<table:table-column table:default-cell-style-name=\'Default\' table:style-name="co0" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co1" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co2" table:number-columns-repeated="2"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co3" table:number-columns-repeated="1"/><table:table-column table:default-cell-style-name=\'Default\' table:style-name="co4" table:number-columns-repeated="1"/>', $xmlContents, 'No matching table:number-columns-repeated count found in sheet');
|
||||
}
|
||||
|
||||
public function testCanTakeColumnWidthsAsRange()
|
||||
{
|
||||
$fileName = 'test_column_widths_as_ranges.ods';
|
||||
$writer = $this->writerForFile($fileName);
|
||||
|
||||
$writer->setColumnWidthForRange(150.0, 1, 3);
|
||||
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12', 'ods--13']));
|
||||
$writer->close();
|
||||
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToWorkbookFile = $resourcePath . '#content.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<style:style style:family="table-column" style:name="co0">', $xmlContents, 'No matching custom col style 0 definition found in sheet');
|
||||
$this->assertContains('style:column-width="150pt"/>', $xmlContents, 'No matching table-column-properties found in sheet');
|
||||
$this->assertContains('table:style-name="co0"', $xmlContents, 'No matching table:style-name found in sheet');
|
||||
$this->assertContains('table:number-columns-repeated="3"', $xmlContents, 'No matching table:number-columns-repeated count 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
|
||||
@@ -99,11 +224,7 @@ class SheetTest extends TestCase
|
||||
*/
|
||||
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName)
|
||||
{
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createODSWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer = $this->writerForFile($fileName);
|
||||
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setName($sheetName);
|
||||
|
||||
@@ -6,6 +6,7 @@ use Box\Spout\TestUsingResource;
|
||||
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;
|
||||
|
||||
@@ -92,6 +93,138 @@ class SheetTest extends TestCase
|
||||
$this->assertStringContainsString(' state="hidden"', $xmlContents, 'The sheet visibility should have been changed to "hidden"');
|
||||
}
|
||||
|
||||
public function testThrowsIfWorkbookIsNotInitialized()
|
||||
{
|
||||
$this->expectException(WriterNotOpenedException::class);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
|
||||
$writer->addRow($this->createRowFromValues([]));
|
||||
}
|
||||
|
||||
public function testWritesDefaultCellSizesIfSet()
|
||||
{
|
||||
$fileName = 'test_writes_default_cell_sizes_if_set.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setDefaultColumnWidth(10.0);
|
||||
$writer->setDefaultRowHeight(20.0);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<sheetFormatPr', $xmlContents, 'No sheetFormatPr tag found in sheet');
|
||||
$this->assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet');
|
||||
$this->assertContains(' defaultRowHeight="20', $xmlContents, 'No default row height found in sheet');
|
||||
$this->assertContains(' customHeight="1"', $xmlContents, 'No row height override flag found in row');
|
||||
}
|
||||
|
||||
public function testWritesDefaultRequiredRowHeightIfOmitted()
|
||||
{
|
||||
$fileName = 'test_writes_default_required_row_height_if_omitted.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setDefaultColumnWidth(10.0);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<sheetFormatPr', $xmlContents, 'No sheetFormatPr tag found in sheet');
|
||||
$this->assertContains(' defaultColWidth="10', $xmlContents, 'No default column width found in sheet');
|
||||
$this->assertContains(' defaultRowHeight="0', $xmlContents, 'No default row height found in sheet');
|
||||
}
|
||||
|
||||
public function testWritesColumnWidths()
|
||||
{
|
||||
$fileName = 'test_column_widths.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setColumnWidth(100.0, 1);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<cols', $xmlContents, 'No cols tag found in sheet');
|
||||
$this->assertContains('<col min="1" max="1" width="100" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
public function testWritesMultipleColumnWidths()
|
||||
{
|
||||
$fileName = 'test_multiple_column_widths.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setColumnWidth(100.0, 1, 2, 3);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<cols', $xmlContents, 'No cols tag found in sheet');
|
||||
$this->assertContains('<col min="1" max="3" width="100" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
public function testWritesMultipleColumnWidthsInRanges()
|
||||
{
|
||||
$fileName = 'test_multiple_column_widths_in_ranges.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setColumnWidth(50.0, 1, 3, 4, 6);
|
||||
$writer->setColumnWidth(100.0, 2, 5);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13', 'xlsx--14', 'xlsx--15', 'xlsx--16']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<cols', $xmlContents, 'No cols tag found in sheet');
|
||||
$this->assertContains('<col min="1" max="1" width="50" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
$this->assertContains('<col min="3" max="4" width="50" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
$this->assertContains('<col min="6" max="6" width="50" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
$this->assertContains('<col min="2" max="2" width="100" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
$this->assertContains('<col min="5" max="5" width="100" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
public function testCanTakeColumnWidthsAsRange()
|
||||
{
|
||||
$fileName = 'test_column_widths_as_ranges.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->setColumnWidthForRange(50.0, 1, 3);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12', 'xlsx--13']));
|
||||
$writer->close();
|
||||
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/worksheets/sheet1.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<cols', $xmlContents, 'No cols tag found in sheet');
|
||||
$this->assertContains('<col min="1" max="3" width="50" customWidth="true"', $xmlContents, 'No expected column width definition found in sheet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param string $sheetName
|
||||
|
||||
Reference in new issue
Block a user