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.
This commit is contained in:
Nathanael d. Noblet 2019-10-17 13:04:18 -06:00 committed by Adrien Crivelli
parent 788f79c1bb
commit 22bf54ca11
4 changed files with 71 additions and 5 deletions

View File

@ -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) - 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) - 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 [#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 ### Fixed

View File

@ -902,3 +902,27 @@ $spreadsheet = $reader->loadFromString($htmlString);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('write.xls'); $writer->save('write.xls');
``` ```
Suppose you have multiple worksheets you'd like created from html. This can be
accomplished as follows.
```php
$firstHtmlString = '<table>
<tr>
<td>Hello World</td>
</tr>
</table>';
$secondHtmlString = '<table>
<tr>
<td>Hello World</td>
</tr>
</table>';
$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');
```

View File

@ -607,12 +607,11 @@ class Html extends BaseReader
* Spreadsheet from content. * Spreadsheet from content.
* *
* @param string $content * @param string $content
* * @param Spreadsheet|null $spreadsheet
* @throws Exception
* *
* @return Spreadsheet * @return Spreadsheet
*/ */
public function loadFromString($content): Spreadsheet public function loadFromString($content, ?Spreadsheet $spreadsheet = null): Spreadsheet
{ {
// Create a new DOM object // Create a new DOM object
$dom = new DOMDocument(); $dom = new DOMDocument();
@ -622,7 +621,7 @@ class Html extends BaseReader
throw new Exception('Failed to load content as a DOM Document'); throw new Exception('Failed to load content as a DOM Document');
} }
return $this->loadDocument($dom, new Spreadsheet()); return $this->loadDocument($dom, $spreadsheet ?? new Spreadsheet());
} }
/** /**

View File

@ -329,6 +329,48 @@ class HtmlTest extends TestCase
$this->assertContains("\n", $cellValue); $this->assertContains("\n", $cellValue);
} }
public function testCanLoadFromStringIntoExistingSpreadsheet()
{
$html = '<table>
<tr>
<td>Hello World</td>
</tr>
<tr>
<td>Hello<br />World</td>
</tr>
<tr>
<td>Hello<br>World</td>
</tr>
</table>';
$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 = '<table>
<tr>
<td>Goodbye World</td>
</tr>
</table>';
self::assertEquals(1, $spreadsheet->getSheetCount());
$spreadsheet = $reader->loadFromString($html, $spreadsheet);
self::assertEquals(2, $spreadsheet->getSheetCount());
}
/** /**
* @param string $html * @param string $html
* *