Cell alignment
This PR adds support for cell alignment for XLSX and ODS files.
You can now align the content of the cells this way:
```
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
$style = (new StyleBuilder())
->setCellAlignment(CellAlignment::RIGHT)
->build();
...
```
Possible cell alignments are: LEFT, RIGHT, CENTER and JUSTIFY.
This commit is contained in:
1 parent
0a0b1f7196
commit
9f4c094fa0
12 files changed
+246
-46
No files matched your search
@@ -3,7 +3,9 @@
|
||||
namespace Box\Spout\Writer\Common\Creator\Style;
|
||||
|
||||
use Box\Spout\Common\Entity\Style\Border;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -45,20 +47,18 @@ class StyleBuilderTest extends TestCase
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStyleBuilderShouldMergeFormats()
|
||||
public function testStyleBuilderShouldApplyCellAlignment()
|
||||
{
|
||||
$baseStyle = (new StyleBuilder())
|
||||
->setFontBold()
|
||||
->setFormat('0.00')
|
||||
->build();
|
||||
$style = (new StyleBuilder())->setCellAlignment(CellAlignment::CENTER)->build();
|
||||
$this->assertTrue($style->shouldApplyCellAlignment());
|
||||
}
|
||||
|
||||
$currentStyle = (new StyleBuilder())->build();
|
||||
|
||||
$styleMerger = new StyleMerger();
|
||||
$mergedStyle = $styleMerger->merge($currentStyle, $baseStyle);
|
||||
|
||||
$this->assertNull($currentStyle->getFormat(), 'Current style has no border');
|
||||
$this->assertEquals('0.00', $baseStyle->getFormat(), 'Base style has a format 0.00');
|
||||
$this->assertEquals('0.00', $mergedStyle->getFormat(), 'Merged style has a format 0.00');
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testStyleBuilderShouldThrowOnInvalidCellAlignment()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
(new StyleBuilder())->setCellAlignment('invalid_cell_alignment')->build();
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,7 @@ class StyleMergerTest extends TestCase
|
||||
->setFontBold()
|
||||
->setFontColor(Color::YELLOW)
|
||||
->setBackgroundColor(Color::BLUE)
|
||||
->setFormat('0.00')
|
||||
->build();
|
||||
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
|
||||
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
|
||||
@@ -60,6 +61,7 @@ class StyleMergerTest extends TestCase
|
||||
$this->assertTrue($mergedStyle->isFontUnderline());
|
||||
$this->assertEquals(Color::YELLOW, $mergedStyle->getFontColor());
|
||||
$this->assertEquals(Color::BLUE, $mergedStyle->getBackgroundColor());
|
||||
$this->assertEquals('0.00', $mergedStyle->getFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS;
|
||||
|
||||
use Box\Spout\Common\Entity\Row;
|
||||
use Box\Spout\Common\Entity\Style\Border;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
use Box\Spout\Common\Entity\Style\Style;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
@@ -204,6 +205,25 @@ class WriterWithStyleTest extends TestCase
|
||||
$this->assertFirstChildHasAttributeEquals('wrap', $customStyleElement, 'table-cell-properties', 'fo:wrap-option');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldApplyCellAlignment()
|
||||
{
|
||||
$fileName = 'test_add_row_should_apply_cell_alignment.xlsx';
|
||||
|
||||
$rightAlignedStyle = (new StyleBuilder())->setCellAlignment(CellAlignment::RIGHT)->build();
|
||||
$dataRows = $this->createStyledRowsFromValues([['ods--11']], $rightAlignedStyle);
|
||||
|
||||
$this->writeToODSFile($dataRows, $fileName);
|
||||
|
||||
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
|
||||
$this->assertCount(2, $styleElements, 'There should be 2 styles (default and custom)');
|
||||
|
||||
$customStyleElement = $styleElements[1];
|
||||
$this->assertFirstChildHasAttributeEquals('end', $customStyleElement, 'paragraph-properties', 'fo:text-align');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\XLSX;
|
||||
|
||||
use Box\Spout\Common\Entity\Row;
|
||||
use Box\Spout\Common\Entity\Style\Border;
|
||||
use Box\Spout\Common\Entity\Style\CellAlignment;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
use Box\Spout\Common\Entity\Style\Style;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
@@ -287,6 +288,24 @@ class WriterWithStyleTest extends TestCase
|
||||
$this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldApplyCellAlignment()
|
||||
{
|
||||
$fileName = 'test_add_row_should_apply_cell_alignment.xlsx';
|
||||
|
||||
$rightAlignedStyle = (new StyleBuilder())->setCellAlignment(CellAlignment::RIGHT)->build();
|
||||
$dataRows = $this->createStyledRowsFromValues([['xlsx--11']], $rightAlignedStyle);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
|
||||
$this->assertFirstChildHasAttributeEquals(CellAlignment::RIGHT, $xfElement, 'alignment', 'horizontal');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user