From bf59cf0cbcbda92ceeb7f43601726033e7b0f5d2 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 12 Jul 2019 07:52:03 +0200 Subject: [PATCH] Html cellwrapping (#1075) * When
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 --- CHANGELOG.md | 4 +++ src/PhpSpreadsheet/Reader/Html.php | 3 +- tests/PhpSpreadsheetTests/Reader/HtmlTest.php | 33 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f15a894f..0d029fe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org). ## [Unreleased] +### Added + +- When <br> 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) diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 2e823f03..75db7591 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -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); diff --git a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php index f56030e0..415f562a 100644 --- a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php @@ -266,6 +266,39 @@ class HtmlTest extends TestCase unlink($filename); } + public function testCanApplyCellWrapping() + { + $html = ' + + + + + + + + + +
Hello World
Hello
World
Hello
World
'; + $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 *