From 1b731a3ef4366e9415591658f11130532df3adc6 Mon Sep 17 00:00:00 2001 From: suwenxiong Date: Wed, 19 Sep 2018 18:58:14 +0800 Subject: [PATCH] support cell object in sheet_add_aoa Fixes #1279 --- bits/27_csfutils.js | 21 ++++++++++++--------- test.js | 8 +++++++- xlsx.flow.js | 21 ++++++++++++--------- xlsx.js | 21 ++++++++++++--------- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/bits/27_csfutils.js b/bits/27_csfutils.js index cae35e5..b6814f7 100644 --- a/bits/27_csfutils.js +++ b/bits/27_csfutils.js @@ -106,21 +106,24 @@ function sheet_add_aoa(_ws/*:?Worksheet*/, data/*:AOA*/, opts/*:?any*/)/*:Worksh for(var C = 0; C != data[R].length; ++C) { if(typeof data[R][C] === 'undefined') continue; var cell/*:Cell*/ = ({v: data[R][C] }/*:any*/); - if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } var __R = _R + R, __C = _C + C; if(range.s.r > __R) range.s.r = __R; if(range.s.c > __C) range.s.c = __C; if(range.e.r < __R) range.e.r = __R; if(range.e.c < __C) range.e.c = __C; - if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } - else if(typeof cell.v === 'number') cell.t = 'n'; - else if(typeof cell.v === 'boolean') cell.t = 'b'; - else if(cell.v instanceof Date) { - cell.z = o.dateNF || SSF._table[14]; - if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } - else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C]; + else { + if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } + if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } + else if(typeof cell.v === 'number') cell.t = 'n'; + else if(typeof cell.v === 'boolean') cell.t = 'b'; + else if(cell.v instanceof Date) { + cell.z = o.dateNF || SSF._table[14]; + if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } + else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + } + else cell.t = 's'; } - else cell.t = 's'; if(dense) { if(!ws[__R]) ws[__R] = []; ws[__R][__C] = cell; diff --git a/test.js b/test.js index 04d4019..100445f 100644 --- a/test.js +++ b/test.js @@ -761,6 +761,13 @@ describe('API', function() { X.utils.sheet_add_aoa(ws, [[4,5,6,7,8,9,0]], {origin: -1}); assert.equal(X.utils.sheet_to_csv(ws).trim(), "S,h,e,e,t,J,S\n1,2,,,5,6,7\n2,3,,,6,7,8\n3,4,,,7,8,9\n4,5,6,7,8,9,0"); }); + it('sheet_add_aoa support object cell', function() { + var data = X.utils.aoa_to_sheet([ + ['url', 'name', 'id'], + [ { l: { Target: 'https://123.com' }, v: 'url', t: 's' }, 'tom', 'xxx' ] + ]); + if(assert.deepEqual) assert.deepEqual(data.A2, { l: { Target: 'https://123.com' }, v: 'url', t: 's' }); + }); }); function coreprop(props) { @@ -2373,4 +2380,3 @@ mft.forEach(function(x) { case "yes-formula": formulae = true; break; }}); }); }); - diff --git a/xlsx.flow.js b/xlsx.flow.js index 10c0be0..89d463b 100644 --- a/xlsx.flow.js +++ b/xlsx.flow.js @@ -3684,21 +3684,24 @@ function sheet_add_aoa(_ws/*:?Worksheet*/, data/*:AOA*/, opts/*:?any*/)/*:Worksh for(var C = 0; C != data[R].length; ++C) { if(typeof data[R][C] === 'undefined') continue; var cell/*:Cell*/ = ({v: data[R][C] }/*:any*/); - if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } var __R = _R + R, __C = _C + C; if(range.s.r > __R) range.s.r = __R; if(range.s.c > __C) range.s.c = __C; if(range.e.r < __R) range.e.r = __R; if(range.e.c < __C) range.e.c = __C; - if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } - else if(typeof cell.v === 'number') cell.t = 'n'; - else if(typeof cell.v === 'boolean') cell.t = 'b'; - else if(cell.v instanceof Date) { - cell.z = o.dateNF || SSF._table[14]; - if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } - else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C]; + else { + if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } + if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } + else if(typeof cell.v === 'number') cell.t = 'n'; + else if(typeof cell.v === 'boolean') cell.t = 'b'; + else if(cell.v instanceof Date) { + cell.z = o.dateNF || SSF._table[14]; + if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } + else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + } + else cell.t = 's'; } - else cell.t = 's'; if(dense) { if(!ws[__R]) ws[__R] = []; ws[__R][__C] = cell; diff --git a/xlsx.js b/xlsx.js index 895de0d..d826c3b 100644 --- a/xlsx.js +++ b/xlsx.js @@ -3592,21 +3592,24 @@ function sheet_add_aoa(_ws, data, opts) { for(var C = 0; C != data[R].length; ++C) { if(typeof data[R][C] === 'undefined') continue; var cell = ({v: data[R][C] }); - if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } var __R = _R + R, __C = _C + C; if(range.s.r > __R) range.s.r = __R; if(range.s.c > __C) range.s.c = __C; if(range.e.r < __R) range.e.r = __R; if(range.e.c < __C) range.e.c = __C; - if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } - else if(typeof cell.v === 'number') cell.t = 'n'; - else if(typeof cell.v === 'boolean') cell.t = 'b'; - else if(cell.v instanceof Date) { - cell.z = o.dateNF || SSF._table[14]; - if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } - else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + if(data[R][C] && typeof data[R][C] === 'object' && !Array.isArray(data[R][C]) && !(data[R][C] instanceof Date)) cell = data[R][C]; + else { + if(Array.isArray(cell.v)) { cell.f = data[R][C][1]; cell.v = cell.v[0]; } + if(cell.v === null) { if(cell.f) cell.t = 'n'; else if(!o.cellStubs) continue; else cell.t = 'z'; } + else if(typeof cell.v === 'number') cell.t = 'n'; + else if(typeof cell.v === 'boolean') cell.t = 'b'; + else if(cell.v instanceof Date) { + cell.z = o.dateNF || SSF._table[14]; + if(o.cellDates) { cell.t = 'd'; cell.w = SSF.format(cell.z, datenum(cell.v)); } + else { cell.t = 'n'; cell.v = datenum(cell.v); cell.w = SSF.format(cell.z, cell.v); } + } + else cell.t = 's'; } - else cell.t = 's'; if(dense) { if(!ws[__R]) ws[__R] = []; ws[__R][__C] = cell;