Skip to content

Commit a95c3f8

Browse files
committed
Fix colspan and rowspan for tables in HTML Writer. Syntax improved.
1 parent 557af99 commit a95c3f8

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,22 @@ public function write()
4040
$rowCount = count($rows);
4141
if ($rowCount > 0) {
4242
$content .= '<table>' . PHP_EOL;
43-
for ($i = 0; $i < count($rows); $i++) {
43+
for ($i = 0; $i < $rowCount; $i++) {
4444
/** @var $row \PhpOffice\PhpWord\Element\Row Type hint */
4545
$rowStyle = $rows[$i]->getStyle();
4646
// $height = $row->getHeight();
4747
$tblHeader = $rowStyle->isTblHeader();
4848
$content .= '<tr>' . PHP_EOL;
4949
$rowCells = $rows[$i]->getCells();
50-
for ($j = 0; $j < count($rowCells); $j++) {
50+
$rowCellCount = count($rowCells);
51+
for ($j = 0; $j < $rowCellCount; $j++) {
5152
$cellStyle = $rowCells[$j]->getStyle();
5253
$cellColSpan = $cellStyle->getGridSpan();
5354
$cellRowSpan = 1;
5455
$cellVMerge = $cellStyle->getVMerge();
5556
// If this is the first cell of the vertical merge, find out how man rows it spans
5657
if ($cellVMerge === 'restart') {
57-
for ($k = $i + 1; $k < count($rows); $k++) {
58+
for ($k = $i + 1; $k < $rowCount; $k++) {
5859
$kRowCells = $rows[$k]->getCells();
5960
if (isset($kRowCells[$j])) {
6061
if ($kRowCells[$j]->getStyle()->getVMerge() === 'continue') {
@@ -70,14 +71,14 @@ public function write()
7071
// Ignore cells that are merged vertically with previous rows
7172
if ($cellVMerge !== 'continue') {
7273
$cellTag = $tblHeader ? 'th' : 'td';
73-
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : "");
74-
$cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : "");
74+
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : '');
75+
$cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : '');
7576
$content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}>" . PHP_EOL;
7677
$writer = new Container($this->parentWriter, $rowCells[$j]);
7778
$content .= $writer->write();
7879
if ($cellRowSpan > 1) {
7980
// There shouldn't be any content in the subsequent merged cells, but lets check anyway
80-
for ($k = $i + 1; $k < count($rows); $k++) {
81+
for ($k = $i + 1; $k < $rowCount; $k++) {
8182
$kRowCells = $rows[$k]->getCells();
8283
if (isset($kRowCells[$j])) {
8384
if ($kRowCells[$j]->getStyle()->getVMerge() === 'continue') {

0 commit comments

Comments
 (0)