英文:
Google Form Script - Optimize how the form reads the last response?
问题
我有这个在Google表单中的脚本:
function onSubmit()
{
var response = FormApp.getActiveForm().getResponses()[FormApp.getActiveForm().getResponses().length - 1].getItemResponses()
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Page/Group Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.facebook.com/' + response[1].getResponse() + '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Channel Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.youtube.com/channel/' + response[1].getResponse() + '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Account Username')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.tiktok.com/@' + response[1].getResponse() + '>'})})
}
}
它正常运行,但在几个月后出现了一个问题。在表单提交后,需要60-80秒才会触发Webhook。我认为这是因为表单花了很多时间遍历150,000个响应并找到最后一个响应。当我复制了表单时,Webhook在表单提交后立即触发。是否有一种优化表单读取最后响应的方法?
英文:
I have this script in a Google form:
function onSubmit()
{
var response = FormApp.getActiveForm().getResponses()[FormApp.getActiveForm().getResponses().length - 1].getItemResponses()
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Page/Group Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.facebook.com/' + response[1].getResponse() + '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Channel Identification')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.youtube.com/channel/' + response[1].getResponse() + '>'})})
}
if (response[2].getResponse()[0] === 'Notify the community manager of a livestream report in real time?' && response[1].getItem().getTitle() === 'Account Username')
{
UrlFetchApp.fetch('https://discord.com/api/webhooks/824374930422497331/X', {headers: {'Content-Type': 'application/json'}, method: 'POST', payload: JSON.stringify({content: '<https://www.tiktok.com/@' + response[1].getResponse() + '>'})})
}
}
It works like a charm, but an issue has popped up after some months. After form submission, it takes 60-80 seconds before the webhook gets triggered. I think it is because the form spends a lot of time going through the 150,000 responses and finding the last one. When I created a copy of the form, the webhook was fired instantly upon form submission. Is there a way to optimize how the form reads the last response?
答案1
得分: 1
Forms内置了一个消息,用于查找触发事件的表单。请参考下面的方法来确保获取最新的响应。或者,考虑第二种方法,只获取最新的响应-1。
/**
* 当接收到新条目时执行。需要使用编辑 ---> 当前项目触发器 来设置。
*/
function entryMade(e) {
reviewResponse_(e.response);
}
这也可以在开销最小的情况下工作:
function getLatestResponse() {
var allResponses = FormApp.getActiveForm().getResponses();
var latestResponse = allResponses[allResponses.length - 1];
return latestResponse;
}
英文:
Forms has a built in message to find the form that triggered an event. See below to establish a method to be guaranteed to get latest response. Alternatively, consider the second approach to just go with latest -1.
/**
* Executed when new entry received. This needs to be setup using EDIT ---> Current Project Triggers
*/
function entryMade(e) {
reviewResponse_(e.response);
}
This should also work with minimal overhead:
function getLatestResponse() {
var allResponses = FormApp.getActiveForm().getResponses();
var latestResponse= allResponses[allResponses.length - 1];
return latestResponse;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论