javascript - Complex object data structure to xlsx -
i have javascript object contains in other objects:
{ "10": {}, "12": { "20": { "value": 1, "id": 1, }, "100": { "value": 12, "id": 1, } }, "14": { "100": { "value": 14, "id": 2, } }, "16": {}, "18": {}, "20": { "100": { "value": 23, "id": 1, }, "150": { "value": 56, "id": 3, } }, "22": {}, "24": {}, "26": { "50": { ...
i want export xlsx file, have issues doing so.
i have resorted @ using js-xlsx not helpful regarding it's documentation, , alasql.
creating such file simpler datastructure quite easy. have few issues when trying create own structure.
firstly trying this:
alasql('select * xlsx("test.xlsx",{headers:true}) ?',[$scope.testdata]);
fails printing [object]
instead of actual values.
trying create cells myself end empty file.
function sheet_from_array_of_arrays(data, opts) { var ws = {}; var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }}; var row1 = 0; (var r in data) { if (data.hasownproperty(r)) { var row2 = 0; (var c in data[r]) { if (data[r].hasownproperty(c)) { var col = 0; (var k in data[r][c]) { if (data[r][c].hasownproperty(c)) { var rr = row1 + row2; var cc = col; if(range.s.r > rr) range.s.r = rr; if(range.s.c > cc) range.s.c = c; if(range.e.r < rr) range.e.r = rr; if(range.e.c < cc) range.e.c = cc; var cell = {v: data[r][c][k] }; if(cell.v == null) continue; var cell_ref = xlsx.utils.encode_cell({c:cc,r:rr}); if(typeof cell.v === 'number') cell.t = 'n'; else if(typeof cell.v === 'boolean') cell.t = 'b'; else if(cell.v instanceof date) { cell.t = 'n'; cell.z = xlsx.ssf._table[14]; cell.v = datenum(cell.v); } else cell.t = 's'; ws[cell_ref] = cell; col = col + 1; } } row2 = row2 + 1; } } row1 = row1 + 1; } } if(range.s.c < 10000000) ws['!ref'] = xlsx.utils.encode_range(range); return ws; } function workbook() { if(!(this instanceof workbook)) return new workbook(); this.sheetnames = []; this.sheets = {}; } var wb = new workbook(), ws = sheet_from_array_of_arrays($scope.testdata); /* add worksheet workbook */ wb.sheetnames.push(ws_name); wb.sheets[ws_name] = ws; var wbout = xlsx.write(wb, {booktype:'xlsx', booksst:true, type: 'binary'}); function s2ab(s) { var buf = new arraybuffer(s.length); var view = new uint8array(buf); (var i=0; i!=s.length; ++i) view[i] = s.charcodeat(i) & 0xff; return buf; } saveas(new blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx")
is there way me create xlsx file object presented above?
you can use search operator parsing of nested json objects:
var data = { "10": {}, "12": { "20": { "value": 1, "id": 1, }, "100": { "value": 12, "id": 1, } }, "14": { "100": { "value": 14, "id": 2, } }, "16": {}, "18": {}, "20": { "100": { "value": 23, "id": 1, }, "150": { "value": 56, "id": 3, } } }; var res = alasql('search of(@a) of(@c) \ return(@a a,@c c, _->[value] [value], _->id id) \ xlsx("test406.xlsx",{headers:true}) \ ?',[data]);
here:
- search ... xlsx ? - special search operator
- of(@v) - list of keys of object (key value stored variable
- return(...) - result object
- _ - current search value
Comments
Post a Comment