I have CSVโs sent to me on a daily basis which need to be added to a table in my app. Iโm having them automatically sent to a folder on my drive which will trigger a doPost script Iโm working on. I have the spreadsheet importing and converting to JSON, however, I canโt figure out how to pass all of the rows of my spreadsheet into the POST request.
function doPost() {
var apiurl = โhttps://api.appsheet.com/api/v2/apps/MYAPPID/tables/MYTABLE/Actionโ;
var result = {};
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(โTHE SHEET I WANT TO IMPORTโ)
.getDataRange()
.getValues();
result.sheet = makeObject(sheet);
Logger.log(result.sheet);
var body = {
โActionโ: โAddโ,
โPropertiesโ: {
โLocaleโ: โen-USโ,
โLocationโ: โ47.623098, -122.330184โ,
โTimezoneโ: โEastern Standard Timeโ,
โRunAsUserEmailโ: โmyemail@ICANTCODEFORSHIT.comโ
},
โRowsโ: [{
โMY COLUMN NAME 1โ: HOW DO I REFERENCE COLUMN 1 OF MY SHEET?,
โMY COLUMN NAME 2โ: WTF DO I PUT HERE? WHY CAN I NOT FIGURE THIS OUT?
}
]
}}
var options = {
โheadersโ : {โApplicationAccessKeyโ : โMYAPPLICATIONACCESSKEYโ,
โAcceptโ : โapplication/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8โ,
โAuthorizationโ: โโ
},
'method' : 'POST',
'muteHttpExceptions': true,
'contentType': 'application/json',
'payload': JSON.stringify(body),
};
console.log(JSON.stringify(options));
var response = UrlFetchApp.fetch(apiurl, options);
}
You didnโt provide the makeObject() code but thatโs where you would return the JSON that would go into the Rows.
See https://stackoverflow.com/questions/47555347/creating-a-json-object-from-google-sheets
Thanks for the tip! How do I format the โRowsโ section?
Thatโs what I canโt figure out. Would it be something like this?
โColumn 1โ: object[0],
โColumn 2โ:object[1]
...
โRowsโ: [getJsonFromSheet(sheet)],
...
where
function getJsonFromSheet(data)
{
var obj = {};
var result = [];
var headers = data[0];
var cols = headers.length;
var row = [];
for (var i = 1, l = data.length; i < l; i++)
{
// get a row to fill the object
row = data[i];
// clear object
obj = {};
for (var col = 0; col < cols; col++)
{
// fill object with new values
obj[headers[col]] = row[col];
}
// add object in a final result
result.push(obj);
}
return result;
}
omg, that took so long for me to figure out how to plug everything together but it finally works. Thank you so much!!!
Youโre welcome.
User | Count |
---|---|
25 | |
15 | |
4 | |
3 | |
3 |