Skip to content

Commit

Permalink
Html cellwrapping (#1075)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mark Baker committed Jul 12, 2019
1 parent de3f948 commit bf59cf0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Reader/Html.php
Expand Up @@ -374,8 +374,9 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row,
// 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);
Expand Down
33 changes: 33 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/HtmlTest.php
Expand Up @@ -266,6 +266,39 @@ public function testCanInsertImage()
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
*
Expand Down

0 comments on commit bf59cf0

Please sign in to comment.