Skip to content

Commit

Permalink
Specify data type in html tags using attributes PHPOffice#3444
Browse files Browse the repository at this point in the history
  • Loading branch information
PouriaSeyfi committed Mar 10, 2023
1 parent 66221bf commit eac0aef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -363,4 +364,31 @@ public function testBorderWithColspan(): void
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle());
}
}

public function testDataType(): void
{
$html = '<table>
<tr>
<td data-type="b">1</td>
<td data-type="s">12345678987654321</td>
<td data-type="f">=CONCAT("TEXT A ","TEXT B")</td>
</tr>
</table>';
$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());
}
}

0 comments on commit eac0aef

Please sign in to comment.