Html cellwrapping (#1075)

* When <br> appears in a table cell, set the cell to wrap.

If the cell is not set to wrap, it appears as a single line when first
displayed in Excel, although editing the cell will cause Excel to wrap
it.

* fix whitespace

Upstream has a coding standard that includes whitespace

* Add Unit tests for cell wrapping

* Update changelog
This commit is contained in:
Mark Baker 2019-07-12 07:52:03 +02:00 committed by GitHub
parent de3f948b27
commit bf59cf0cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased]
### Added
- When &lt;br&gt; appears in a table cell, set the cell to wrap [Issue #1071](https://github.com/PHPOffice/PhpSpreadsheet/issues/1071) and [PR #1070](https://github.com/PHPOffice/PhpSpreadsheet/pull/1070)
### Fixed
- COUPNUM should not return zero when settlement is in the last period - [Issue #1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [PR #1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021)

View File

@ -374,8 +374,9 @@ class Html extends BaseReader
// no break
case 'br':
if ($this->tableLevel > 0) {
// If we're inside a table, replace with a \n
// If we're inside a table, replace with a \n and set the cell to wrap
$cellContent .= "\n";
$sheet->getStyle($column . $row)->getAlignment()->setWrapText(true);
} else {
// Otherwise flush our existing content and move the row cursor on
$this->flushCell($sheet, $column, $row, $cellContent);

View File

@ -266,6 +266,39 @@ class HtmlTest extends TestCase
unlink($filename);
}
public function testCanApplyCellWrapping()
{
$html = '<table>
<tr>
<td>Hello World</td>
</tr>
<tr>
<td>Hello<br />World</td>
</tr>
<tr>
<td>Hello<br>World</td>
</tr>
</table>';
$filename = $this->createHtml($html);
$spreadsheet = $this->loadHtmlIntoSpreadsheet($filename);
$firstSheet = $spreadsheet->getSheet(0);
$cellStyle = $firstSheet->getStyle('A1');
self::assertFalse($cellStyle->getAlignment()->getWrapText());
$cellStyle = $firstSheet->getStyle('A2');
self::assertTrue($cellStyle->getAlignment()->getWrapText());
$cellValue = $firstSheet->getCell('A2')->getValue();
$this->assertContains("\n", $cellValue);
$cellStyle = $firstSheet->getStyle('A3');
self::assertTrue($cellStyle->getAlignment()->getWrapText());
$cellValue = $firstSheet->getCell('A3')->getValue();
$this->assertContains("\n", $cellValue);
unlink($filename);
}
/**
* @param string $html
*