A way to specify date formating when using XLSX.utils.sheet_to_row_object_array() #134
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
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#134
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?
Hi
Is there a way to specify date formatting to "dd/mm/yyyy" when converting a sheet to object array?
Currently function returns "mm/dd/yyyy" seems like this is hardcoded into SSF somewhere as a default.
@rotemtam Let's take the write example https://github.com/SheetJS/test_files/blob/master/write.xlsx in node:
Cell C3 is a date:
To reformat this, first vacate the formatted text and add the desired number format:
Now apply the number format:
Now all of the output functions will use the new format.
@SheetJSDev thanks for the reply! I've managed to figure that much out.
I wanted to check (and perhaps suggest ) that we should be able to specify a default date format to use for cells containing date data.
@rotemtam you probably know this, but for posterity's sake let's review dates in Excel:
Generally Excel stores dates as numbers with a special formatting. For example, today is October 11 2014. If you use the formula
=TODAY()
you see a date rendering, but that comes from the number format. If you change the format toGeneral
you will see the date code41923
.Right now only the formatting library is aware that the number is a date. To make the change you propose, we would need to make the core library more aware of dates. The big stumbling block, which is why we store the date code rather than storing the raw date object, is a bug that was replicated from Lotus 1-2-3: 1900 was not a leap year but Lotus treated it as one. Who would have thought a 30+ year old bug would haunt us today?
One solution, as discussed in https://github.com/SheetJS/js-xlsx/issues/126 ( h/t @elad ), is to just store the raw dates internally. I've been playing with it, and quirks aside it works fairly well (you see the problems when trying to perform date arithmetic -- some versions of excel store the difference as a date, and the dreaded 1900 bug shows up at 60 days)
Another solution (which fakes a "default date") is to add an override to the output functions which would effectively apply the approach I suggested earlier (reformat cells that are assumed to be dates).
A third solution involves some not-yet-released logic: as part of https://github.com/SheetJS/js-xlsx/issues/78 ( @sysarchitect the issue was not forgotten :) the whole localization logic is changing. It is possible to create a fake locale with the default date.
The third option definitely will be implemented. The second option can be added really quickly. The first may happen, although I'm not sure when. Which sounds like the most appealing option?
@SheetJSDev not sure I understand all the options, but thanks for the thorough replies and explanations.
Not sure what I'm suggesting fits in one of these three. What I was thinking is that I as a user of the library should be able to do is something like:
The reason i think we should be able to override whatever the user did in the original excel file, is that we get all sorts of funky data from all sorts of users and we need to be able to standardize it and process it.
Also what @elad suggests sounds useful to me, if i could tell by looking at a cell object whether it contains date data i could scan the results and deal with them myself, currently i could not find an easy way to tell that a cell contains date data.
@rotemtam do you need the date in a consistent format so that you're able to parse it into a javascript date object (or a moment.js object or whatever)?
@elad exactly!
@SheetJSDev I'd say this is another instance of #126 then...
Note: these changes are in the repo and will appear in version 0.9.5.
If you read in with
cellDates:true
, the date-stored-as-number cells will be proper dates.If you set the
dateNF
key of the options object to a number format string, the formatter will use it. Following the example from my previous comment:Where is the arguments to
format_cell
even documented? I just see it mentioned in the docs.