我有一个非常简单的doPost(e)脚本项目,它正在作为webhook端点运行。它只是将接收到的JSON有效负载记录到目标电子表格的A2单元中。在目标电子表格中,我有一个onChange(e)触发器,它将此有效负载传递给外部API。尽管运行时没有错误,但我的onChange(e)脚本根本没有触发。如果我剪切/粘贴了工作表上的单元格内容,那么onChange(e)会被正确触发。
在Google Developers页面here中,提到了一些限制,但据说这些限制不适用于doGet(e)和doPost(e)。
我想念什么?
这是我的doPost代码:
var SHEETID = "my_sheetID_here";
function doPost(e) {
try {
var data = JSON.parse(e.postData.contents);
var sSht = SpreadsheetApp.openById(SHEETID);
var sht = sSht.getSheetByName("TEMP");
sht.getRange("A2").setValue(e.postData.contents);
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(data) }))
.setMimeType(ContentService.MimeType.JSON);
} catch (e) {
MailApp.sendEmail("xx@xx.com,yy@xx.com", "FireStation Webhook Error Occured!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
e = (typeof e === 'string') ? new Error(e) : e;
Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
throw e;
}
}
这是我在目标表中的onChange(e)代码:
function onChange(e) {
try {
var sheet = e.source.getActiveSheet();
var sheetname = sheet.getName();
Logger.log("Sheetname: "+sheetname);
if (sheetname === "TEMP" && sheet.getRange("A2").getValue() !== "") {
var payload = sheet.getRange("A2").getValue();
var URL = "my_external_api_URL_here";
var options =
{
"method" : "POST",
"payload" : payload,
"followRedirects" : true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(URL, options);
Logger.log("HTTP Response: "+result.getResponseCode())
}
} catch (e) {
MailApp.sendEmail("xx@xx.com,yy@xx.com", "FireStation Template GAS Error Occured!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
e = (typeof e === 'string') ? new Error(e) : e;
Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
throw e;
}
}
答案 0 :(得分:2)
Sheets API可用于触发“更改时”事件。不要使用setValues(array)
。删除该代码,然后添加一个函数以将值写入电子表格。
您可能需要在appsscript.json文件(清单文件)中设置范围。
{
"timeZone": "America/New_York",
"dependencies": {
},
"webapp": {
"access": "MYSELF",
"executeAs": "USER_DEPLOYING"
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": ["https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.external_request"]
}
如果还有其他作用域,请在“文件”“项目属性”和“作用域”选项卡中找到它们。复制它们并将它们放入appsscript.json文件中,以保持相同的作用域。
function doPost(e) {
try {
var data = JSON.parse(e.postData.contents);
writeToSheet({shName:"TEMP",ssFileID:SHEETID,data:data});//Write data to sheet using API
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(data) }))
.setMimeType(ContentService.MimeType.JSON);
} catch (e) {
//code . . . .
}
使用带有valueInputOption=USER_ENTERED
选项的Sheets API v4的函数:
function writeToSheet(po) {
var id,options,range,response,sh,targetSheetName,ss,url,values;
targetSheetName = po.shName;
id = po.ssFileID;
range = targetSheetName + "!A2:A2";
values = {values: [[po.data]]};
url = "https://sheets.googleapis.com/v4/spreadsheets/" +
id + "/values/" + range + ":append?valueInputOption=USER_ENTERED";
options = {
"method":"post",
"muteHttpExceptions": true,
"headers": {
"Authorization": "Bearer " + ScriptApp.getOAuthToken()
},
"contentType": "application/json",
"payload": JSON.stringify(values)
}
response = UrlFetchApp.fetch(url,options)
response = JSON.parse(response);
Logger.log('response ' + JSON.stringify(response))
}
答案 1 :(得分:1)
使用Sheets API version 4可以解决问题并触发问题,而不是使用内置的Sheets服务(例如 SpreadsheetApp.openById(sheetid)和 Range.setValue())目标电子表格上的onChange(e)。请注意,此解决方案要求从开发者控制台启用Advanced Google Services / Sheet API。
这是我的doPost(e)调用的最新版本,并且运行良好:
var SHEETID = "my_sheet_id_here";
function doPost(e) {
try {
var data = e.postData.contents;
var rowValues = [
[data,""],
];
var request = {
'valueInputOption': 'USER_ENTERED',
'data': [
{
"range": "TEMP!A2:B2",
"majorDimension": "ROWS",
"values": rowValues,
},
],
};
var response = Sheets.Spreadsheets.Values.batchUpdate(request, SHEETID);
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(data) }))
.setMimeType(ContentService.MimeType.JSON);
} catch (e) {
MailApp.sendEmail("levent@able3ventures.com,alkio@able3ventures.com", "FireStation Webhook Error Occured!",
"\r\nMessage: " + e.message
+ "\r\nFile: " + e.fileName
+ "\r\nLine: " + e.lineNumber);
e = (typeof e === 'string') ? new Error(e) : e;
Logger.severe('%s: %s (line %s, file "%s"). Stack: "%s" . While processing %s.',e.name||'',
e.message||'', e.lineNumber||'', e.fileName||'', e.stack||'', processingMessage||'');
throw e;
}
}