aVadim483 / fast-excel-reader

Lightwight and very fast XLSX Excel Spreadsheet Reader in PHP
MIT License
63 stars 16 forks source link

Cell value is incorrectly parsed as an int #16

Closed lrakauskas closed 11 months ago

lrakauskas commented 11 months ago

If in Excel cell value is "013", $sheet->nextRow() would return such cell as (int) 13, which is not correct.

Problem stems from https://github.com/aVadim483/fast-excel-reader/blob/main/src/FastExcelReader/Sheet.php#L170

For the sake of testing, you can simply run the following:

 $x = "013";
 dd($x == (int)$x, $x, (int)$x); // true, "013", 13

Is there a way to skip automated cell type casting so that original cell values are returned? Or is there a way to force certain column to always return string ?

MiMatus commented 11 months ago

I've just came here to report the same issue... As I had to create small work-around for it:

....
$rowIterator = $sheet->nextRow($headers, Excel::KEYS_FIRST_ROW, true);
// and then $rowIterator is basically
/** @var iterable<int, array<string, array{v: mixed, t: string, o: mixed}>> where `o` is original value before any casting  */

So you can create whatever casting logic you want 😉

MiMatus commented 11 months ago

I spent some time on this so I will try to write-up my findings.

Problem is not related only to int values but to all numeric/numeric like values which can be found xlsx cell value.

Examples of problematic values: