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 9, 2023
1 parent 66221bf commit 8b2d941
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@ 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'])) {
$sheet->getCell($column . $row)->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 +589,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
17 changes: 17 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,20 @@ 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>
</tr>
</table>';
$reader = new Html();
$spreadsheet = $reader->loadFromString($html);
$firstSheet = $spreadsheet->getSheet(0);

self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('A1')->getDataType());
self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('B1')->getDataType());
}
}

0 comments on commit 8b2d941

Please sign in to comment.