-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
feat: add support for rowspan in addHTML() #2643
base: master
Are you sure you want to change the base?
Changes from all commits
7bbc9a9
1dc34b7
33cea1b
7f99efb
f4fac68
da62475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -472,11 +472,53 @@ protected static function parseCell($node, $element, &$styles) | |
$cellStyles['gridSpan'] = $colspan - 0; | ||
} | ||
|
||
$rowspan = $node->getAttribute('rowspan'); | ||
if (!empty($rowspan)) { | ||
$cellStyles['vMerge'] = 'restart'; | ||
} | ||
$beforespan = $node->getAttribute('beforespan'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This attribute doesn't exist. Can you use a Real attribute or an another method ? |
||
if (!empty($beforespan)) { | ||
$cellRowContinue = ['vMerge' => 'continue']; | ||
$beforecolspan = $node->getAttribute('beforecolspan'); | ||
|
||
for ($s = 1; $s <= $beforespan; ++$s) { | ||
if (!empty($beforecolspan)) { | ||
if (is_numeric($beforecolspan)) { | ||
$beforecolspan = (int) $beforecolspan; | ||
} else { | ||
$beforecolspans = json_decode($beforecolspan, true); | ||
$beforecolspan = $beforecolspans[$s - 1]; | ||
} | ||
$cellRowContinue['gridSpan'] = $beforecolspan; | ||
} | ||
$element->addCell(null, $cellRowContinue); | ||
} | ||
} | ||
|
||
// set cell width to control column widths | ||
$width = $cellStyles['width'] ?? null; | ||
unset($cellStyles['width']); // would not apply | ||
$cell = $element->addCell($width, $cellStyles); | ||
|
||
$afterspan = $node->getAttribute('afterspan'); | ||
if (!empty($afterspan)) { | ||
$cellRowContinue = ['vMerge' => 'continue']; | ||
$aftercolspan = $node->getAttribute('aftercolspan'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This attribute doesn't exist. Can you use a Real attribute or an another method ? |
||
|
||
for ($s = 1; $s <= $afterspan; ++$s) { | ||
if (!empty($aftercolspan)) { | ||
if (is_numeric($aftercolspan)) { | ||
$aftercolspan = (int) $aftercolspan; | ||
} else { | ||
$aftercolspans = json_decode($aftercolspan, true); | ||
$aftercolspan = $aftercolspans[$s - 1]; | ||
} | ||
$cellRowContinue['gridSpan'] = $aftercolspan; | ||
} | ||
$element->addCell(null, $cellRowContinue); | ||
} | ||
} | ||
|
||
if (self::shouldAddTextRun($node)) { | ||
return $cell->addTextRun(self::filterOutNonInheritedStyles(self::parseInlineStyle($node, $styles['paragraph']))); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,7 +127,7 @@ public function testParseStyle(): void | |
background-color:red; | ||
} | ||
</style> | ||
|
||
<p class="pStyle">Calculator</p>'; | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
|
@@ -418,6 +418,38 @@ public function testParseTable(): void | |
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr')); | ||
} | ||
|
||
public function testParseTableWithRowSpan(): void | ||
{ | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
$html = '<table style="width: 100%; border: 1px #000000 solid;" cellspacing="0" collpadding="0"> | ||
<thead> | ||
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; "> | ||
<th style="text-align:center;">A</th> | ||
<th style="text-align:center;">B</th> | ||
<th style="text-align:center;">C</th> | ||
<th style="text-align:center;">D</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr><td > A1 </td><td colspan="2"> BC1 </td><td> D1 </td></tr> | ||
<tr><td rowspan="2" colspan="2"> AB23 </td><td> C2 </td><td> D2 </td></tr> | ||
<tr><td beforespan="1" beforecolspan="2" > C3 </td><td> D3 </td></tr> | ||
<tr><td rowspan="3" > A456 </td><td> B4 </td><td rowspan="2" colspan="2"> CD45 </td></tr> | ||
<tr><td rowspan="2" beforespan="1" afterspan="1" aftercolspan="2">B5</td></tr> | ||
<tr><td beforespan="2">C6</td><td> D6 </td></tr> | ||
<tr><td> A7 </td><td> B7 </td><td> C7 </td><td> D7 </td></tr> | ||
<tr><td > A8 </td><td colspan="2"> BC8 </td><td > D8 </td></tr> | ||
<tr><td colspan="3"> ABC9 </td><td rowspan="2"> D9 </td></tr> | ||
<tr><td > A9 </td><td > B9 </td><td afterspan="1"> C9 </td></tr> | ||
</tbody> | ||
</table>'; | ||
Html::addHtml($section, $html); | ||
|
||
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jnlin Could you add more assertions for checking the colspan and the rowspan ? |
||
} | ||
|
||
/** | ||
* Parse widths in tables and cells, which also allows for controlling column width. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jnlin Could you add you as author ?