Mar*_*ear 10
在您的应用程序脚本中尝试类似的操作:
var POST_URL = "enter your webhook URL";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
payload[question] = answer;
}
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(POST_URL, options);
};
Run Code Online (Sandbox Code Playgroud)
请务必将 POST_URL 变量替换为您的 webhook,您可以使用requestcatcher.com来测试这一点。
通过单击侧面菜单中的“触发器”向脚本添加触发器
Script Editorclock图标(左侧菜单),这意味着Triggers。Add trigger按钮(将显示一个表单,如下图所示)。onSubmit在 下Choose which function to run。Select event type设置为On form submit。Save按钮。之后,提交您的表格并注意收到的请求。
小智 8
首先: 您需要设置您的应用程序脚本项目,您将通过以下方式完成此操作:
访问script.google.com打开脚本编辑器。(您需要登录 Google 帐户。)如果这是您第一次访问 script.google.com,您将被重定向到介绍 Apps 脚本的页面。单击“开始脚本编写”以进入脚本编辑器。欢迎屏幕将询问您要创建哪种类型的脚本。单击空白项目或关闭。删除脚本编辑器中的所有代码并粘贴以下代码。 该视频和文档会有所帮助
其次, 您需要创建一个可安装的触发器,您可以将其直接添加到表单或包含响应的电子表格中
function setUpTrigger(){
ScriptApp.newTrigger('sendPostRequest') /* this has the name of the function that will have the post request */
.forForm('formkey') // you'll find it in the url
.onFormSubmit()
.create();
}
Run Code Online (Sandbox Code Playgroud)
检查文档
第三步 创建sendPostRequest函数并向其中添加UrlFetchApp
function sendPostRequest(e){
// Make a POST request with form data.
var resumeBlob = Utilities.newBlob('Hire me!', 'text/plain', 'resume.txt');
var formData = {
'name': 'Bob Smith',
'email': 'bob@example.com',
'resume': resumeBlob
};
// Because payload is a JavaScript object, it is interpreted as
// as form data. (No need to specify contentType; it automatically
// defaults to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
'method' : 'post',
'payload' : formData
};
UrlFetchApp.fetch('https://httpbin.org/post', options);
}
Run Code Online (Sandbox Code Playgroud)
检查文档
| 归档时间: |
|
| 查看次数: |
12896 次 |
| 最近记录: |