Skip to content

Commit 04d0c02

Browse files
dox07troosan
authored andcommitted
Add support for cellSpacing for tables (#1040)
* Add cellSpacing into table * add word 2007 reader * add tests * add documentation
1 parent ba03518 commit 04d0c02

File tree

10 files changed

+168
-32
lines changed

10 files changed

+168
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
v0.15.0 (?? ??? 2018)
77
----------------------
88
### Added
9-
- Parsing of "align" HTML attribute - @troosan #1231
9+
- Parsing of `align` HTML attribute - @troosan #1231
1010
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508
1111
- Parsing of CSS `direction` instruction, HTML `lang` attribute, formatting inside table cell - @troosan #1273 #1252 #1254
1212
- Add support for Track changes @Cip @troosan #354 #1262
1313
- Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276
14+
- Add support for Cell Spacing @dox07 @troosan #1040
1415

1516
### Fixed
1617
- Fix reading of docx default style - @troosan #1238

docs/styles.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ Available Table style options:
103103
- ``border(Top|Right|Bottom|Left)Size``. Border size in *twip*.
104104
- ``cellMargin(Top|Right|Bottom|Left)``. Cell margin in *twip*.
105105
- ``width``. Table width in percent.
106+
- ``unit``. The unit to use for the width. One of ``\PhpOffice\PhpWord\SimpleType\TblWidth``. Defaults to *auto*.
106107
- ``layout``. Table layout, either *fixed* or *autofit* See ``\PhpOffice\PhpWord\Style\Table`` for constants.
108+
- ``cellSpacing`` Cell spacing in *twip*
107109

108110
Available Row style options:
109111

samples/Sample_09_Tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$section->addText('Fancy table', $header);
2828

2929
$fancyTableStyleName = 'Fancy Table';
30-
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);
30+
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50);
3131
$fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
3232
$fancyTableCellStyle = array('valign' => 'center');
3333
$fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR);

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
426426
$styleDefs["border{$ucfSide}Style"] = array(self::READ_VALUE, "w:tblBorders/w:$side", 'w:val');
427427
}
428428
$styleDefs['layout'] = array(self::READ_VALUE, 'w:tblLayout', 'w:type');
429+
$styleDefs['cellSpacing'] = array(self::READ_VALUE, 'w:tblCellSpacing', 'w:w');
429430
$style = $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
430431
}
431432
}

src/PhpWord/SimpleType/TblWidth.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2017 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\SimpleType;
19+
20+
use PhpOffice\PhpWord\Shared\AbstractEnum;
21+
22+
/**
23+
* Table Width Units
24+
*
25+
* @since 0.15.0
26+
*
27+
* @see http://www.datypic.com/sc/ooxml/t-w_ST_TblWidth.html
28+
*/
29+
final class TblWidth extends AbstractEnum
30+
{
31+
//No Width
32+
const NIL = 'nil';
33+
34+
//Automatically Determined Width
35+
const AUTO = 'auto';
36+
37+
//Width in Fiftieths of a Percent
38+
const PERCENT = 'pct';
39+
40+
//Width in Twentieths of a Point
41+
const TWIP = 'dxa';
42+
}

src/PhpWord/Style/Table.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919

2020
use PhpOffice\PhpWord\SimpleType\Jc;
2121
use PhpOffice\PhpWord\SimpleType\JcTable;
22+
use PhpOffice\PhpWord\SimpleType\TblWidth;
2223

2324
class Table extends Border
2425
{
2526
/**
26-
* @const string Table width units http://www.schemacentral.com/sc/ooxml/t-w_ST_TblWidth.html
27+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO instead
2728
*/
2829
const WIDTH_AUTO = 'auto'; // Automatically determined width
30+
/**
31+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT instead
32+
*/
2933
const WIDTH_PERCENT = 'pct'; // Width in fiftieths (1/50) of a percent (1% = 50 unit)
34+
/**
35+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP instead
36+
*/
3037
const WIDTH_TWIP = 'dxa'; // Width in twentieths (1/20) of a point (twip)
3138

3239
//values for http://www.datypic.com/sc/ooxml/t-w_ST_TblLayoutType.html
@@ -133,7 +140,12 @@ class Table extends Border
133140
/**
134141
* @var string Width unit
135142
*/
136-
private $unit = self::WIDTH_AUTO;
143+
private $unit = TblWidth::AUTO;
144+
145+
/**
146+
* @var int|float cell spacing value
147+
*/
148+
protected $cellSpacing = null;
137149

138150
/**
139151
* @var string Table Layout
@@ -152,7 +164,7 @@ public function __construct($tableStyle = null, $firstRowStyle = null)
152164
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
153165
$this->firstRowStyle = clone $this;
154166
$this->firstRowStyle->isFirstRow = true;
155-
unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom);
167+
unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom, $this->firstRowStyle->cellSpacing);
156168
$this->firstRowStyle->setStyleByArray($firstRowStyle);
157169
}
158170

@@ -161,6 +173,22 @@ public function __construct($tableStyle = null, $firstRowStyle = null)
161173
}
162174
}
163175

176+
/**
177+
* @param float|int $cellSpacing
178+
*/
179+
public function setCellSpacing($cellSpacing = null)
180+
{
181+
$this->cellSpacing = $cellSpacing;
182+
}
183+
184+
/**
185+
* @return float|int
186+
*/
187+
public function getCellSpacing()
188+
{
189+
return $this->cellSpacing;
190+
}
191+
164192
/**
165193
* Set first row
166194
*
@@ -595,8 +623,8 @@ public function getUnit()
595623
*/
596624
public function setUnit($value = null)
597625
{
598-
$enum = array(self::WIDTH_AUTO, self::WIDTH_PERCENT, self::WIDTH_TWIP);
599-
$this->unit = $this->setEnumVal($value, $enum, $this->unit);
626+
TblWidth::validate($value);
627+
$this->unit = $value;
600628

601629
return $this;
602630
}

src/PhpWord/Writer/Word2007/Style/Table.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
1919

2020
use PhpOffice\Common\XMLWriter;
21+
use PhpOffice\PhpWord\SimpleType\TblWidth;
2122
use PhpOffice\PhpWord\Style\Table as TableStyle;
2223
use PhpOffice\PhpWord\Writer\Word2007\Element\TableAlignment;
2324

@@ -49,7 +50,7 @@ public function write()
4950
$xmlWriter->writeAttribute('w:val', $style);
5051
$xmlWriter->endElement();
5152
if (null !== $this->width) {
52-
$this->writeWidth($xmlWriter, $this->width, 'pct');
53+
$this->writeTblWidth($xmlWriter, 'w:tblW', TblWidth::PERCENT, $this->width);
5354
}
5455
$xmlWriter->endElement();
5556
}
@@ -76,7 +77,8 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7677
$xmlWriter->endElement();
7778
}
7879

79-
$this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
80+
$this->writeTblWidth($xmlWriter, 'w:tblW', $style->getUnit(), $style->getWidth());
81+
$this->writeTblWidth($xmlWriter, 'w:tblCellSpacing', TblWidth::TWIP, $style->getCellSpacing());
8082
$this->writeLayout($xmlWriter, $style->getLayout());
8183
$this->writeMargin($xmlWriter, $style);
8284
$this->writeBorder($xmlWriter, $style);
@@ -92,21 +94,6 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
9294
}
9395
}
9496

95-
/**
96-
* Write width.
97-
*
98-
* @param \PhpOffice\Common\XMLWriter $xmlWriter
99-
* @param int $width
100-
* @param string $unit
101-
*/
102-
private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
103-
{
104-
$xmlWriter->startElement('w:tblW');
105-
$xmlWriter->writeAttribute('w:w', $width);
106-
$xmlWriter->writeAttribute('w:type', $unit);
107-
$xmlWriter->endElement(); // w:tblW
108-
}
109-
11097
/**
11198
* Enable/Disable automatic resizing of the table
11299
*
@@ -159,6 +146,25 @@ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
159146
}
160147
}
161148

149+
/**
150+
* Writes a table width
151+
*
152+
* @param \PhpOffice\Common\XMLWriter $xmlWriter
153+
* @param string $elementName
154+
* @param string $unit
155+
* @param int|float $width
156+
*/
157+
private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null)
158+
{
159+
if (null === $width) {
160+
return;
161+
}
162+
$xmlWriter->startElement($elementName);
163+
$xmlWriter->writeAttributeIf(null !== $width, 'w:w', $width);
164+
$xmlWriter->writeAttribute('w:type', $unit);
165+
$xmlWriter->endElement();
166+
}
167+
162168
/**
163169
* Write row style.
164170
*

tests/PhpWord/Reader/Word2007/StyleTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Reader\Word2007;
1919

2020
use PhpOffice\PhpWord\AbstractTestReader;
21+
use PhpOffice\PhpWord\SimpleType\TblWidth;
2122
use PhpOffice\PhpWord\Style\Table;
2223

2324
/**
@@ -43,4 +44,26 @@ public function testReadTableLayout()
4344
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
4445
$this->assertEquals(Table::LAYOUT_FIXED, $elements[0]->getStyle()->getLayout());
4546
}
47+
48+
/**
49+
* Test reading of cell spacing
50+
*/
51+
public function testReadCellSpacing()
52+
{
53+
$documentXml = '<w:tbl>
54+
<w:tblPr>
55+
<w:tblCellSpacing w:w="10.5" w:type="dxa"/>
56+
</w:tblPr>
57+
</w:tbl>';
58+
59+
$phpWord = $this->getDocumentFromString($documentXml);
60+
61+
$elements = $this->get($phpWord->getSections(), 0)->getElements();
62+
$this->assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
63+
$this->assertInstanceOf('PhpOffice\PhpWord\Style\Table', $elements[0]->getStyle());
64+
$this->assertEquals(TblWidth::AUTO, $elements[0]->getStyle()->getUnit());
65+
/** @var \PhpOffice\PhpWord\Style\Table $tableStyle */
66+
$tableStyle = $elements[0]->getStyle();
67+
$this->assertEquals(10.5, $tableStyle->getCellSpacing());
68+
}
4669
}

tests/PhpWord/Style/TableTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Style;
1919

2020
use PhpOffice\PhpWord\SimpleType\JcTable;
21+
use PhpOffice\PhpWord\SimpleType\TblWidth;
2122

2223
/**
2324
* Test class for PhpOffice\PhpWord\Style\Table
@@ -38,9 +39,6 @@ public function testConstruct()
3839
$styleTable = array('bgColor' => 'FF0000');
3940
$styleFirstRow = array('borderBottomSize' => 3);
4041

41-
$object = new Table();
42-
$this->assertNull($object->getBgColor());
43-
4442
$object = new Table($styleTable, $styleFirstRow);
4543
$this->assertEquals('FF0000', $object->getBgColor());
4644

@@ -49,6 +47,18 @@ public function testConstruct()
4947
$this->assertEquals(3, $firstRow->getBorderBottomSize());
5048
}
5149

50+
/**
51+
* Test default values when passing no style
52+
*/
53+
public function testDefaultValues()
54+
{
55+
$object = new Table();
56+
57+
$this->assertNull($object->getBgColor());
58+
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
59+
$this->assertEquals(TblWidth::AUTO, $object->getUnit());
60+
}
61+
5262
/**
5363
* Test setting style with normal value
5464
*/
@@ -77,6 +87,7 @@ public function testSetGetNormal()
7787
'alignment' => JcTable::CENTER,
7888
'width' => 100,
7989
'unit' => 'pct',
90+
'layout' => Table::LAYOUT_FIXED,
8091
);
8192
foreach ($attributes as $key => $value) {
8293
$set = "set{$key}";
@@ -174,13 +185,14 @@ public function testSetStyleValue()
174185
}
175186

176187
/**
177-
* Tests table layout
188+
* Tests table cell spacing
178189
*/
179-
public function testTableLayout()
190+
public function testTableCellSpacing()
180191
{
181192
$object = new Table();
182-
$this->assertEquals(Table::LAYOUT_AUTO, $object->getLayout());
183-
$object->setLayout(Table::LAYOUT_FIXED);
184-
$this->assertEquals(Table::LAYOUT_FIXED, $object->getLayout());
193+
$this->assertNull($object->getCellSpacing());
194+
195+
$object = new Table(array('cellSpacing' => 20));
196+
$this->assertEquals(20, $object->getCellSpacing());
185197
}
186198
}

tests/PhpWord/Writer/Word2007/Style/TableTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,25 @@ public function testTableLayout()
5555
$this->assertTrue($doc->elementExists($path));
5656
$this->assertEquals(Table::LAYOUT_FIXED, $doc->getElementAttribute($path, 'w:type'));
5757
}
58+
59+
/**
60+
* Test write styles
61+
*/
62+
public function testCellSpacing()
63+
{
64+
$tableStyle = new Table();
65+
$tableStyle->setCellSpacing(10.3);
66+
67+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
68+
$section = $phpWord->addSection();
69+
$table = $section->addTable($tableStyle);
70+
$table->addRow();
71+
72+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
73+
74+
$path = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
75+
$this->assertTrue($doc->elementExists($path));
76+
$this->assertEquals(10.3, $doc->getElementAttribute($path, 'w:w'));
77+
$this->assertEquals(\PhpOffice\PhpWord\SimpleType\TblWidth::TWIP, $doc->getElementAttribute($path, 'w:type'));
78+
}
5879
}

0 commit comments

Comments
 (0)