Google App Script - Appsheet API - write data to Spreadsheet \ Add fields name to row 1

I m now trying to dump App data to Google Spreadsheet, using Google App Script + Appsheet API.

This is code.

function appsheetApiGet() {

var appId = “YOURAPPID”
var accessKey = “YOURACCESSKEY”
var tableName = “YOURTABLENAME”

var url =
https://api.appsheet.com/api/v2/apps/” + appId + “/tables/” + tableName + “/Action?applicationAccessKey=” + accessKey

var payload =
{
“Action”: “Find”,
“Properties”: {
“Locale”: “en-US”,
“Location”: “47.623098, -122.330184”,
“Timezone”: “Pacific Standard Time”
},
“Rows”: [
]
}

var options =
{
“method” : “post”,
“contentType” : “application/json”,
“muteHttpExceptions” : true,
“payload” : JSON.stringify(payload)
};

var response = UrlFetchApp.fetch(url, options);

var json = response.getContentText();

var data = JSON.parse(json)

Logger.log(data)

var sheet = SpreadsheetApp.getActiveSheet()

sheet.clear()

for (var i = 0 ; i<data.length ; i++) {

  var row = [data[i]["Col1"],data[i]["Col2"]]
     
  sheet.appendRow(row)

}

}

It is working as expected apart from one missing thing, i.e. HOW TO ADD THE FIELDS NAME ONTO ROW #1

I dont have coding background with me, much appreciate for help from those who are familiar with App Script!

I assume I need to add spript to the part of

for (var i = 0 ; i<data.length ; i++) {

  var row = [data[i]["Col1"],data[i]["Col2"]]
     
  sheet.appendRow(row)

}

to add table/slice field name to row 1 but… Currently this code add only data values.

2 17 2,012
17 REPLIES 17
Top Labels in this Space