Skip to content

Generate Table Cell if Row Doesn't Have Any #2516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
### Bug fixes

- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)
- Html Reader : Process Titles as Headings not Paragraphs [@0b10011](https://github.com/0b10011) and [@oleibman](https://github.com/oleibman) Issue [#1692](https://github.com/PHPOffice/PHPWord/issues/1692) PR [#2533](https://github.com/PHPOffice/PHPWord/pull/2533)
- Generate Table Cell if Row Doesn't Have Any [@oleibman](https://github.com/oleibman) fixing [#2505](https://github.com/PHPOffice/PHPWord/issues/2505) in [#2516](https://github.com/PHPOffice/PHPWord/pull/2516)
- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)

- 32-bit Problem in PasswordEncoder [@oleibman](https://github.com/oleibman) fixing [#2550](https://github.com/PHPOffice/PHPWord/issues/2550) in [#2551](https://github.com/PHPOffice/PHPWord/pull/2551)

### Miscellaneous
Expand Down
10 changes: 8 additions & 2 deletions src/PhpWord/Writer/Word2007/Element/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ private function writeRow(XMLWriter $xmlWriter, RowElement $row): void
}

// Write cells
foreach ($row->getCells() as $cell) {
$this->writeCell($xmlWriter, $cell);
$cells = $row->getCells();
if (count($cells) === 0) {
// issue 2505 - Word treats doc as corrupt if row without cell
$this->writeCell($xmlWriter, new CellElement());
} else {
foreach ($cells as $cell) {
$this->writeCell($xmlWriter, $cell);
}
}

$xmlWriter->endElement(); // w:tr
Expand Down
147 changes: 147 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Element/TableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
declare(strict_types=1);

namespace PhpOffice\PhpWordTests\Writer\Word2007\Element;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\TblWidth;
use PhpOffice\PhpWordTests\TestHelperDOCX;

/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace.
*/
class TableTest extends \PHPUnit\Framework\TestCase
{
/**
* Executed after each method of the class.
*/
protected function tearDown(): void
{
TestHelperDOCX::clear();
}

public static function testTableNormal(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Before table (normal).');
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]);
$row = $table->addRow();
$tc = $table->addCell();
$tc->addText('R1C1');
$tc = $table->addCell();
$tc->addText('R1C2');
$row = $table->addRow();
$tc = $table->addCell();
$tc->addText('R2C1');
$tc = $table->addCell();
$tc->addText('R2C2');
$row = $table->addRow();
$tc = $table->addCell();
$tc->addText('R3C1');
$tc = $table->addCell();
$tc->addText('R3C2');
$section->addText('After table.');

$doc = TestHelperDOCX::getDocument($phpWord);
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'should be only 1 table');

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]'));

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[2]'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[3]'));

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[2]'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[3]'));
}

public static function testSomeRowWithNoCells(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Before table (row 2 has no cells).');
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]);
$row = $table->addRow();
$tc = $table->addCell();
$tc->addText('R1C1');
$tc = $table->addCell();
$tc->addText('R1C2');
$row = $table->addRow();
$row = $table->addRow();
$tc = $table->addCell();
$tc->addText('R3C1');
$tc = $table->addCell();
$tc->addText('R3C2');
$section->addText('After table.');

$doc = TestHelperDOCX::getDocument($phpWord);
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'should be only 1 table');

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]'));

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[2]'));

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[2]'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[3]'));
}

public static function testOnly1RowWithNoCells(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Before table (only 1 row and it has no cells).');
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]);
$row = $table->addRow();
$section->addText('After table.');

$doc = TestHelperDOCX::getDocument($phpWord);
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'only 1 table should be written');

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]'));
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc'));
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]'));

self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]'));
}

public static function testNoRows(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Before table (no rows therefore omitted).');
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]);
$section->addText('After table.');

$doc = TestHelperDOCX::getDocument($phpWord);
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[1]'), 'no table should be written');
}
}
Loading