From 22bf54ca11579af0edd6f0b613052044cf5258f5 Mon Sep 17 00:00:00 2001 From: "Nathanael d. Noblet" Date: Thu, 17 Oct 2019 13:04:18 -0600 Subject: [PATCH] Allow Html Reader to write into existing spreadsheet Sometimes you may want to read html into multiple worksheets within one spreadsheet. Allowing the passing of a spreadsheet in makes this possible. --- CHANGELOG.md | 1 + docs/topics/reading-and-writing-to-file.md | 24 +++++++++++ src/PhpSpreadsheet/Reader/Html.php | 9 ++-- tests/PhpSpreadsheetTests/Reader/HtmlTest.php | 42 +++++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a113848d..348fa098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Improve performance of IF function calls via ranch pruning to avoid resolution of every branches [#844](https://github.com/PHPOffice/PhpSpreadsheet/pull/844) - MATCH function supports `*?~` Excel functionality, when match_type=0 [#1116](https://github.com/PHPOffice/PhpSpreadsheet/issues/1116) - Allow HTML Reader to accept HTML as a string [#1136](https://github.com/PHPOffice/PhpSpreadsheet/pull/1136) +- Allow HTML Reader to accept HTML as a string into an existing spreadsheet [#1212](https://github.com/PHPOffice/PhpSpreadsheet/pull/1212) ### Fixed diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index 66455356..3b6a037c 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -902,3 +902,27 @@ $spreadsheet = $reader->loadFromString($htmlString); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); $writer->save('write.xls'); ``` + +Suppose you have multiple worksheets you'd like created from html. This can be +accomplished as follows. + +```php +$firstHtmlString = ' + + + +
Hello World
'; +$secondHtmlString = ' + + + +
Hello World
'; + +$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html(); +$spreadsheet = $reader->loadFromString($firstHtmlString); +$reader->setSheetIndex(1); +$spreadhseet = $reader->loadFromString($secondHtmlString, $spreadsheet); + +$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); +$writer->save('write.xls'); +``` diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index bf9c6038..5ef40521 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -606,13 +606,12 @@ class Html extends BaseReader /** * Spreadsheet from content. * - * @param string $content - * - * @throws Exception + * @param string $content + * @param Spreadsheet|null $spreadsheet * * @return Spreadsheet */ - public function loadFromString($content): Spreadsheet + public function loadFromString($content, ?Spreadsheet $spreadsheet = null): Spreadsheet { // Create a new DOM object $dom = new DOMDocument(); @@ -622,7 +621,7 @@ class Html extends BaseReader throw new Exception('Failed to load content as a DOM Document'); } - return $this->loadDocument($dom, new Spreadsheet()); + return $this->loadDocument($dom, $spreadsheet ?? new Spreadsheet()); } /** diff --git a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php index e9dd207f..150793a0 100644 --- a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php @@ -329,6 +329,48 @@ class HtmlTest extends TestCase $this->assertContains("\n", $cellValue); } + public function testCanLoadFromStringIntoExistingSpreadsheet() + { + $html = ' + + + + + + + + + +
Hello World
Hello
World
Hello
World
'; + $reader = new Html(); + $spreadsheet = $reader->loadFromString($html); + $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); + + $reader->setSheetIndex(1); + $html = ' + + + +
Goodbye World
'; + + self::assertEquals(1, $spreadsheet->getSheetCount()); + $spreadsheet = $reader->loadFromString($html, $spreadsheet); + self::assertEquals(2, $spreadsheet->getSheetCount()); + } + /** * @param string $html *