Cell values with date format showing as "1/0/1900" despite raw:true when cellStyles:true is enabled #3268
Labels
No Label
DBF
Dates
Defined Names
Features
Formula
HTML
Images
Infrastructure
Integration
International
ODS
Operations
Performance
PivotTables
Pro
Protection
Read Bug
SSF
SYLK
Style
Write Bug
good first issue
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#3268
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
I'm experiencing an issue where cell values (both headers and data) that have date formatting in Excel are being converted to "1/0/1900" or "Invalid Date" in the output, even when using
raw:true
. This occurs specifically whencellStyles:true
is enabled, which I need for theskipHidden
functionality.Current Behavior
cellStyles:true
is set, text values that have date formatting in Excel are converted to "1/0/1900" or "Invalid Date"raw:true
is set both in the read options and sheet_to_json optionsraw:true
settingExpected Behavior
raw:true
should take precedence over cell style formattingReproduction Steps
Current Workaround
Currently, I can fix this by manually processing the cells:
However, this is not ideal because:
raw:true
should handle this automaticallyEnvironment
Additional Context
cellStyles:true
because it's required for theskipHidden
property to workQuestion
Is there a way to get the raw text values while keeping
cellStyles:true
enabled? Or is there an alternative approach to usingskipHidden
without requiringcellStyles:true
?Thanks for reporting! In this case, you have a text cell with a date format, which incorrectly elicited a date conversion. There is a missing
break
in themake_json_row
function:Thank you so much for the quick response and fix! The missing
break
statement explains the date conversion issue perfectly.While we're on the topic, I also noticed that the
raw: true
option insheet_to_json()
doesn't seem to work as expected for dates. Even withraw: true
, instead of getting the original Excel epoch timestamp, it returns a JavaScript timestamp. For example:Shouldn't
raw: true
return the original Excel epoch number rather than converting it to a JavaScript date/timestamp? This seems inconsistent with the documentation which suggestsraw: true
should return the underlying value without type conversion.There are two steps here:
The
raw
parameter toread
andreadFile
affect formats where plaintext values are parsed (including CSV and HTML).cellDates
enables the value coercion for XLSX, XLSB, XLS and other formats that use date codes.The
raw
parameter tosheet_to_json
controls which values are emitted (underlying cell value or formatted text from the worksheet).The only way to get the date codes is to pass the option
cellDates: false
when reading the file andraw: true
when generating rows, as it ensures the date codes are preserved when parsing the file.Thanks for the explanation about the
raw
andcellDates
parameters. However, I'm still seeing an issue with these settings. I am already using the combination you suggested:Even with
cellDates: false
andraw: true
in both places, I'm not getting the date codes. I've noticed this only happens whencellStyles: true
is enabled. When I setcellStyles: false
, the date codes are returned as expected.Since I need
cellStyles: true
for the skipHidden functionality to work, is there a way to preserve the date codes while keeping cell styles enabled?Currently,
cellStyles: true
forcescellNF: true
(save number formats), which forces the date conversion in line 28:In general, Dates are more useful in JS contexts than the raw date codes. We can add a
cellDates
option tosheet_to_json
that would suppress the conversion.Thanks for explaining about the cellStyles behavior. So if I understand correctly: if I add a
cellDates
option tosheet_to_json
, it would allow us to control whether date codes get converted to JS dates, even whencellStyles
is enabled?I've added the change below - could you confirm if this is what you had in mind?
This would only perform the date conversion when
cellDates: true
is explicitly passed tosheet_to_json
.Also, I noticed something else regarding date formatting: When using
cellStyles: true
while reading the file,In thesheet_to_json
method even though I specifydateNF: 'yyyy-mm-dd'
, the dates are being returned with the Excel cell's format (e.g., 'mm/dd/yyyy') instead of the requested format. Looking at thesafe_format_cell
function, it seems to usecell.z
format instead of the provideddateNF
.Should
dateNF
take precedence over the cell's format when explicitly provided in the options? Or am I misunderstanding how these options should interact?