Skip to content

Commit 7d5c62a

Browse files
committed
Merge branch 'nestedtable' into develop
2 parents 27d18fd + ded651d commit 7d5c62a

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1414
- TextBox: Ability to add table inside textbox - @basjan GH-231
1515
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
1616
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
17+
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
1718

1819
### Bugfixes
1920

samples/Sample_09_Tables.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@
8484
$table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered);
8585
$table->addCell(null, $cellRowContinue);
8686

87+
// 4. Nested table
88+
89+
$section->addTextBreak(2);
90+
$section->addText('Nested table', $header);
91+
92+
$cell = $section->addTable()->addRow()->addCell();
93+
$cell->addText('This cell contains nested table.');
94+
$innerCell = $cell->addTable()->addRow()->addCell();
95+
$innerCell->addText('Inside nested table');
96+
8797
// Save file
8898
echo write($phpWord, basename(__FILE__, '.php'), $writers);
8999
if (!CLI) {

src/PhpWord/Element/AbstractContainer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n
217217
public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
218218
{
219219
$this->checkValidity('ListItemRun');
220-
220+
221221
$element = new ListItemRun($depth, $fontStyle, $listStyle, $paragraphStyle);
222222
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
223223
$this->addElement($element);
224-
224+
225225
return $element;
226226
}
227-
227+
228228
/**
229229
* Add table element
230230
*
@@ -374,8 +374,8 @@ private function checkValidity($method)
374374
'Object' => $allContainers,
375375
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
376376
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
377-
'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
378-
'Table' => array('section', 'header', 'footer', 'textbox'),
377+
'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
378+
'Table' => array('section', 'header', 'footer', 'cell', 'textbox'),
379379
'CheckBox' => array('section', 'header', 'footer', 'cell'),
380380
'TextBox' => array('section', 'header', 'footer', 'cell'),
381381
'Footnote' => array('section', 'textrun', 'cell'),

src/PhpWord/Writer/Word2007/Element/Container.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,23 @@ public function write()
4343
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
4444
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;
4545

46-
// Loop through subelements
47-
$subelements = $container->getElements();
48-
if (count($subelements) > 0) {
49-
foreach ($subelements as $subelement) {
50-
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement));
46+
// Loop through elements
47+
$elements = $container->getElements();
48+
$elementClass = '';
49+
if (count($elements) > 0) {
50+
foreach ($elements as $element) {
51+
$elementClass = get_class($element);
52+
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
5153
if (class_exists($writerClass)) {
52-
$writer = new $writerClass($xmlWriter, $subelement, $withoutP);
54+
$writer = new $writerClass($xmlWriter, $element, $withoutP);
5355
$writer->write();
5456
}
5557
}
56-
} else {
57-
// Special case for Cell: They have to contain a TextBreak at least
58-
if ($containerClass == 'Cell') {
58+
}
59+
60+
// Special case for Cell: They have to contain a w:p element at the end
61+
if ($containerClass == 'Cell') {
62+
if ($elementClass == '' || $elementClass == 'PhpOffice\\PhpWord\\Element\\Table') {
5963
$writerClass = "{$this->namespace}\\TextBreak";
6064
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
6165
$writer->write();

0 commit comments

Comments
 (0)