Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9786

Converting Google Sheet to JSON format while rows are vertical

$
0
0

I am trying to convert the google sheets into JSON format using Google App Scirpt.It worked but I am not getting the right format and I am little struck.

My sheet looks something like this in the imagegoogle sheets

Then I am running the google script code. This is my code.

function doGet(e) {  var sheet = SpreadsheetApp.getActive();  var nse = sheet.getSheetByName("email-preview");  var data = [];  var rlen = nse.getLastRow();  /*how many rows are there? */  var clen = nse.getLastColumn();  var rows = nse.getRange(1, 1, rlen, clen).getValues();  for (var i = 1; i < rows.length; i++) {    var datarow = rows[i];    var record = "";    for(var j=0;j<35;j++){      record[rows[0][j]]=datarow[j];     }    data.push(record);   }  console.log(data);  var result=JSON.stringify(data);  return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);}

I am getting the output something like this,

   [ { field1: 1000 },      { field2: 2000 },      { field3: 3000 },      { field4: 4000 },      { field5: 5000 },      { field6: 6000 },      { field7: 7000 },      { field8: 800 },      { field9: 9000 },      { field10: 1 },      { field11: 2 },      { field12: 3 },      { field13: 4 },      { field14: 5 },      { field15: 6 },      { field16: 7 },      { field17: 8 },      { field18: 9 },      { field19: 10 },      { field20: 11 },      { field21: 12 },      { field22: 13 },      { field23: 14 },      { field24: 15 },      { field25: 16 } ]

Expected Output:

    [ { field1: 1000,      field2: 2000,       field3: 3000,      field4: 4000,      field5: 5000,      field6: 6000,      field7: 7000,       field8: 800,       field9: 9000,       field10: 1,       field11: 2,       field12: 3,       field13: 4,       field14: 5,       field15: 6,       field16: 7,       field17: 8,       field18: 9,       field19: 10,       field20: 11,       field21: 12,       field22: 13,       field23: 14,       field24: 15,       field25: 16 } ]

Can anyone let me know how to fix it? and also make the results dynamic in case I add a new field


Viewing all articles
Browse latest Browse all 9786

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>