json2xls
json2xls copied to clipboard
Add the date support
I added the date support, by modifying your code and excel-export's one.
Here is the excel-export's index.js :
Line 139, I added a function a call it in the addDateCol.
var JSDateToExcelDate = function (inDate) {
var returnDateTime = 25569.0 + ((inDate.getTime() - (inDate.getTimezoneOffset() * 60 * 1000)) / (1000 * 60 * 60 * 24));
return returnDateTime.toString().substr(0, 20);
}
var addDateCol = function (cellRef, value, styleIndex) {
styleIndex = styleIndex || 1;
if (value === null)
return "";
else
return '<x:c r="' + cellRef + '" s="' + styleIndex + '" t="n"><x:v>' + JSDateToExcelDate(value) + '</x:v></x:c>';
};
In your library, in json2xls.js, I added an "if" to the default case :
switch (t) {
case 'string':
case 'number':
return t;
case 'boolean':
return 'bool';
default:
if (obj instanceof Date)
return 'date'
else
return 'string';
@Thetortuga
I'm struggling to format my excel file, can you perhaps help by spotting what I'm doing wrong.
- I am using express and using json2xls as middleware
app.use(json2xls.middleware);
- I create a json array called docs and an options object as described:
var docs = [{ Date: 42500,
'Account Name': 'SAS',
'Total Sold Imps': 3,
'Imps Bought': 2,
'Publisher Rev': '1' },
{ Date: 42491,
'Account Name': 'SAS',
'Total Sold Imps': 6,
'Imps Bought': 7,
'Publisher Rev': '7' },
{ Date: 42492,
'Account Name': 'SAS',
'Total Sold Imps': 2,
'Imps Bought': 3,
'Publisher Rev': '7' } ]
var options = var options = { 'Imps Bought': 'number'}
- I create and download the file
res.xls('test.xlsx', docs, options);
I also tried with the options as an array with no luck:
var options = ['Imps Bought']
Thanks so much!
Steven
Thanks for your thoughts. I'll have a look at having a better support for dates.
better support? Isn't there no support whatsoever?