Skip to content

Commit ded651d

Browse files
committed
#149: Ability to add table inside a cell (nested table)
1 parent 17e2f02 commit ded651d

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1313
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
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
16+
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
1617

1718
### Bugfixes
1819

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private function checkValidity($method)
354354
'Object' => $allContainers,
355355
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
356356
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
357-
'Table' => array('section', 'header', 'footer', 'textbox'),
357+
'Table' => array('section', 'header', 'footer', 'cell', 'textbox'),
358358
'CheckBox' => array('section', 'header', 'footer', 'cell'),
359359
'TextBox' => array('section', 'header', 'footer', 'cell'),
360360
'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')) ? 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)