admin管理员组文章数量:1390531
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!
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!
Share edited Feb 21, 2018 at 11:53 MRI80 asked Feb 21, 2018 at 10:45 MRI80MRI80 351 silver badge6 bronze badges1 Answer
Reset to default 6You can do something like the following:
function doPost(request) {
var ss = SpreadsheetApp.openById("ID here")
var sheet = ss.getSheetByName("Sheet1")
// Now Parse the body of your post request in to a data object
var data = JSON.parse (request.postData.contents)
//Now put the data in the sheet
sheet.getRange(1, 1).setValue(data['myValue'])
// Note no need to stringify
// now do your other stuff
The body of the post request is available to you in request .postData.contents
. Here's the Google documentation
You parse the content string into a JSON object and then access the fields you need.
本文标签:
版权声明:本文标题:javascript - POSTed JSON to Google Apps Script, need to extract value and place in Google Sheets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650306a2617643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论