user interface - How to convert excel data to json at frontend side -


i want convert excel data json. plan excel file d drive, read data , make ui this. can 1 please me out?

data :-

country       year    1       2       3       4  netherlands   1970    3603    4330    5080    5820 netherlands   1971    3436    4165    4929    5693 netherlands   1972    3384    4122    4899    5683 sweden        1970    1479    1963    2520    3132 sweden        1971    1497    1985    2547    3163 sweden        1972    1419    1894    2445    3055 

you of course use excel vba, working excel. following sub exports used range of current sheet valid json .js-file:

option explicit sub jsonex() dim fname, q$, str$, lineend$, na, va, ur range, i%, j%, ncols%, nrows% set ur = application.activesheet.usedrange ' set few variables ... na = ur.rows(1): ncols = ur.columns.count: nrows = ur.rows.count: lineend = ",": q = """"  fname = "d:\tmp\data.js"                 ' prefedined filename  ' fname = application.getsaveasfilename  ' or use file selector box ...  open fname output #1 print #1, "var data=[" = 2 ur.rows.count   va = ur.rows(i)   str = ""   j = 1 ncols     str = str & "," & q & na(1, j) & q & ":" & q & va(1, j) & q   next   if (i = nrows) lineend = "];"   print #1, "  {" & mid(str, 2) & "}" & lineend next close #1 end sub 

this produces file d:\tmp\data.js with

var data=[   {"country":"netherlands","year":"1970","1":"3603","2":"4330","3":"5080","4":"5820"},   {"country":"netherlands","year":"1971","1":"3436","2":"4165","3":"4929","4":"5693"},   {"country":"netherlands","year":"1972","1":"3384","2":"4122","3":"4899","4":"5683"},   {"country":"sweden","year":"1970","1":"1479","2":"1963","3":"2520","4":"3132"},   {"country":"sweden","year":"1971","1":"1497","2":"1985","3":"2547","4":"3163"},   {"country":"sweden","year":"1972","1":"1419","2":"1894","3":"2445","4":"3055"}]; 

once got in json format convert different array/object structure using plain javascript like

var da={}; data.slice(1).foreach(function(e){   var arr=[e[1],e[2],e[3],e[4]];   if (!da[e.country]) da[e.country]={}   da[e.country][e.year]=arr; }); 

maybe more format ("beautified" json.stringify(da)):

{"netherlands":{"1970":["3603","4330","5080","5820"],                 "1971":["3436","4165","4929","5693"],                 "1972":["3384","4122","4899","5683"]},  "sweden":     {"1970":["1479","1963","2520","3132"],                 "1971":["1497","1985","2547","3163"],                 "1972":["1419","1894","2445","3055"]}} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -