Skip to content

Commit

Permalink
Check TYPE_INLINE in flushCell and some changes in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
PouriaSeyfi committed Mar 18, 2023
1 parent 9037905 commit ceb87b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ protected function flushCell(Worksheet $sheet, $column, $row, &$cellContent, arr
// Set cell value explicitly if there is data-type attribute
if (isset($attributeArray['data-type'])) {
$datatype = $attributeArray['data-type'];
if ($datatype == DataType::TYPE_STRING || $datatype == DataType::TYPE_STRING2) {
if (substr($cellContent, 0, 1) === '=' || is_numeric($cellContent)) {
if (in_array($datatype, [DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_INLINE])) {
//Prevent to Excel treat string with beginning equal sign or convert big numbers to scientific number
if (substr($cellContent, 0, 1) === '=') {
$sheet->getCell($column . $row)
->getStyle()
->setQuotePrefix(true);
Expand Down
29 changes: 14 additions & 15 deletions tests/PhpSpreadsheetTests/Reader/Html/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Font;
Expand Down Expand Up @@ -370,19 +372,20 @@ public function testDataType(): void
$html = '<table>
<tr>
<td data-type="b">1</td>
<td data-type="s">1234567</td>
<td data-type="f">=CONCAT("TEXT A ","TEXT B")</td>
<td data-type="s">12345678987654</td>
<!-- in some cases, you may want to treat the string with beginning equal sign as a string rather than a formula -->
<td data-type="s">=B1</td>
<td data-type="d">2022-02-21 10:20:30</td>
<td data-type="invalid-datatype">text</td>
<td data-type="null">null</td>
<td data-type="invalid-datatype">text with invalid datatype</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());
Expand All @@ -391,19 +394,15 @@ public function testDataType(): void
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());

// check string with beginning equal sign is string and not formula
self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('D1')->getDataType());
self::assertEquals('=B1', $firstSheet->getCell('D1')->getValue());
self::assertIsString($firstSheet->getCell('D1')->getValue());
self::assertTrue($firstSheet->getCell('D1')->getStyle()->getQuotePrefix());
// check string with beginning equal sign (=B1) and string datatype,is not formula
self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('C1')->getDataType());
self::assertEquals('=B1', $firstSheet->getCell('C1')->getValue());
self::assertTrue($firstSheet->getCell('C1')->getStyle()->getQuotePrefix());

//check iso date
self::assertEquals($firstSheet->getCell('E1')->getValue(), 44613.43090277778);
self::assertEqualsWithDelta($firstSheet->getCell('D1')->getValue(), 44613.43090277778, 1.0e-12);

//null
self::assertEquals($firstSheet->getCell('E1')->getValue(), null);
}
}

0 comments on commit ceb87b0

Please sign in to comment.