I am posting a JSON to Google Apps Script and want to display a specific value from that JSON in Google Sheets.
I use the following code to get the data into my sheet from this post
function doPost(e) {
Logger.log("I was called")
if (typeof e !== 'undefined')
Logger.log(e.parameter);
var ss = SpreadsheetApp.openById("ID here")
var sheet = ss.getSheetByName("Sheet1")
sheet.getRange(1, 1).setValue(JSON.stringify(e))
return ContentService.createTextOutput(JSON.stringify(e))
}
My sheet now displays:
{
"parameter":{
},
"contextPath":"",
"contentLength":20,
"queryString":"",
"parameters":{
},
"postData":{
"type":"application/json",
"length":20,
"contents":"{\"myValue\":13}",
"name":"postData"
}
}
I only want it to display the value for myValue - which is the number 13 in this example.
I've tried a number of things suggested on these forums, I figured I was getting there with the solution from this post, and changed the code to:
function doPost(e) {
Logger.log("I was called")
if (typeof e !== 'undefined')
Logger.log(e.parameter);
var jsonString = e.postData.getDataAsString(); //added line
var jsonData = JSON.parse(jsonString); // added line
var ss = SpreadsheetApp.openById("ID here")
var sheet = ss.getSheetByName("Sheet1")
sheet.getRange(1, 1).setValue(JSON.stringify(jsonData.myValue)) //changed "e" to jsonData.myValue
return ContentService.createTextOutput(JSON.stringify(jsonData.myValue)) //changed "e" to jsonData.myValue
}
This didn't work.
As a simple marketer, I wrestled with this for 2 nights now and feel I am pretty much stuck. So any help would be kindly appreciated. Cheers!