sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

function table_to_book returns a date if td element contains text "marks" followed by a number

Open aleemkhan opened this issue 9 years ago • 8 comments

my data has text like "Tally marks - up to 10" and "Tally marks - up to 20" in td element of my html. When I convert this data to xlsx it returns a date object like this

A4:{t: "n", v: 36586, z:"m/d/yy"}

instead of same text value. I have then tried to figure it out what is causing it. So I concluded that if there is a word "marks" followed by a number then it shows this kind of behavior.

Why is it showing this behavior.

aleemkhan avatar Aug 16 '17 15:08 aleemkhan

The error comes from the Chrome date logic:

new Date("Tally marks - up to 10") // Sat Mar 10 2001 00:00:00 GMT-0500 (EST)

To avoid matching those, the reader checks for a short month name, and of course "Marks" happens to contain "Mar".

Until it is resolved, passing raw:true bypasses the value guessing logic.

reviewher avatar Aug 16 '17 18:08 reviewher

@reviewher should I pass raw:true like this

var worksheet = XLSX.utils.table_to_book(document.getElementById(tableId), {raw : true});

I have tried this but this is not working. let me know if I am doing something wrong.

Also I am writing the file like this

var wopts = { bookType:'xlsx', bookSST:false, type:'binary', raw: true};
var wbout = XLSX.write(worksheet, wopts);

aleemkhan avatar Aug 17 '17 05:08 aleemkhan

Your first line is correct. Are you using the latest version? console.log(XLSX.version) will tell you, and the current release is 0.11.2

Just as a sanity check, try going to http://sheetjs.com/demos/table and editing the number 4 to Marks 4, then open the console and run:

XLSX.utils.table_to_book(document.getElementById('table')).Sheets.Sheet1.D3
// {t: "n", v: 36954.041666666664, z: "m/d/yy"}
XLSX.utils.table_to_book(document.getElementById('table'), {raw:true}).Sheets.Sheet1.D3
// {t: "s", v: "Marks 4"}

reviewher avatar Aug 17 '17 06:08 reviewher

@reviewher Thank you so much for helping me out here. putting raw: true and updating library to latest version fixed it for me.

God Bless You

aleemkhan avatar Aug 17 '17 06:08 aleemkhan

Glad that worked :)

reviewher avatar Aug 17 '17 06:08 reviewher

I would like to know how to use the raw parameter but at the same time, format the numeric columns in a way that im still allowed to add the values in excel.

ninettyone avatar Sep 09 '20 20:09 ninettyone

Currently that's not possible.

For a customer of our Pro offering, we added a backdoor rawDates option that skips the entire date parsing logic. We can do something similar here too.

SheetJSDev avatar Sep 09 '20 20:09 SheetJSDev

@reviewher Thank you so much for helping me out here. putting raw: true and updating library to latest version fixed it for me.

Best Regards

epeker avatar May 24 '23 06:05 epeker