json2xls icon indicating copy to clipboard operation
json2xls copied to clipboard

Add the date support

Open Thetortuga opened this issue 8 years ago • 3 comments

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 avatar Jun 02 '16 13:06 Thetortuga

@Thetortuga

I'm struggling to format my excel file, can you perhaps help by spotting what I'm doing wrong.

  1. I am using express and using json2xls as middleware
app.use(json2xls.middleware);
  1. 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'}
  1. 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

swinston100 avatar Jun 30 '16 07:06 swinston100

Thanks for your thoughts. I'll have a look at having a better support for dates.

rikkertkoppes avatar Jul 01 '16 07:07 rikkertkoppes

better support? Isn't there no support whatsoever?

RomanShabanov avatar Mar 20 '17 19:03 RomanShabanov