MRI*_*I80 2 javascript google-apps-script
我正在向 Google Apps Script 发布 JSON,并希望在 Google 表格中显示该 JSON 中的特定值。
我使用以下代码从这篇文章中获取数据到我的工作表中
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))
}
Run Code Online (Sandbox Code Playgroud)
我的工作表现在显示:
{
"parameter":{
},
"contextPath":"",
"contentLength":20,
"queryString":"",
"parameters":{
},
"postData":{
"type":"application/json",
"length":20,
"contents":"{\"myValue\":13}",
"name":"postData"
}
}
Run Code Online (Sandbox Code Playgroud)
我只希望它显示 myValue 的值——在这个例子中是数字 13。
我已经尝试了这些论坛上建议的许多内容,我想我是通过这篇文章中的解决方案到达那里的,并将代码更改为:
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
}
Run Code Online (Sandbox Code Playgroud)
这没有用。
作为一个简单的营销人员,我现在与这个搏斗了 2 个晚上,觉得我几乎被卡住了。所以任何帮助将不胜感激。干杯!
您可以执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
您可以在request .postData.contents. 这是谷歌文档
您将内容字符串解析为 JSON 对象,然后访问您需要的字段。
| 归档时间: |
|
| 查看次数: |
3569 次 |
| 最近记录: |