diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 780cfec8a7..5614da0b1c 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -564,6 +564,18 @@ private function processDomElementDataFormat(Worksheet $sheet, int $row, string } } + private function processDomElementDataType(Worksheet $sheet, int $row, string $column, array $attributeArray): void + { + if (isset($attributeArray['data-type'])) { + $cell = $sheet->getCell($column . $row); + $cellValue = $cell->getValue(); + //cast value to the datatype + $sheet->setCellValueExplicit($column . $row, $cellValue, $attributeArray['data-type']); + //set datatype + $cell->setDataType($attributeArray['data-type']); + } + } + private function processDomElementThTd(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void { while (isset($this->rowspan[$column . $row])) { @@ -582,6 +594,7 @@ private function processDomElementThTd(Worksheet $sheet, int &$row, string &$col $this->processDomElementAlign($sheet, $row, $column, $attributeArray); $this->processDomElementVAlign($sheet, $row, $column, $attributeArray); $this->processDomElementDataFormat($sheet, $row, $column, $attributeArray); + $this->processDomElementDataType($sheet, $row, $column, $attributeArray); if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) { //create merging rowspan and colspan diff --git a/tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php b/tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php index ad395ace8e..cf16c68c4e 100644 --- a/tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader\Html; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Style\Alignment; @@ -363,4 +364,31 @@ public function testBorderWithColspan(): void self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); } } + + public function testDataType(): void + { + $html = ' + + + + + +
112345678987654321=CONCAT("TEXT A ","TEXT B")
'; + $reader = new Html(); + $spreadsheet = $reader->loadFromString($html); + $firstSheet = $spreadsheet->getSheet(0); + + // check boolean data type + self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('A1')->getDataType()); + self::assertIsBool($firstSheet->getCell('A1')->getValue()); + + // check string data type + self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('B1')->getDataType()); + self::assertIsString($firstSheet->getCell('B1')->getValue()); + + // check formula data type + self::assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C1')->getDataType()); + // check formula output + self::assertEquals('TEXT A TEXT B', $firstSheet->getCell('C1')->getFormattedValue()); + } }