我正在尝试使用Apps脚本(作为Webapp)在Gsheet中获取POST请求!它适用于JavaScript主体POST类型,但是当我尝试使用x-www-form-urlencoded时出现错误。
function doPost(e) {
//Return if null
if( e == undefined ) {
console.log("no data");
return HtmlService.createHtmlOutput("need data");
}
//Parse the JSON data
var event = JSON.parse(e.postData.contents);
var data = event.data;
//Get the last row without data
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
//Get current timestamp
var timestamp = new Date();
//Insert the data into the sheet
sheet.getRange(lastRow + 1, 1).setValue(event.bt_signature);
sheet.getRange(lastRow + 1, 2).setValue(data.bt_payload);
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("post request received");}
谢谢!
对于x-www-form-urlencoded
内容,POST
数据存储在事件对象的parameter
和parameters
属性中。参见documentation。