Google表单脚本 – 优化表单读取最后一次响应的方式?

huangapple go评论115阅读模式
英文:

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] === &#39;Notify the community manager of a livestream report in real time?&#39; &amp;&amp; response[1].getItem().getTitle() === &#39;Page/Group Identification&#39;)
  {
        UrlFetchApp.fetch(&#39;https://discord.com/api/webhooks/824374930422497331/X&#39;, {headers: {&#39;Content-Type&#39;: &#39;application/json&#39;}, method: &#39;POST&#39;, payload: JSON.stringify({content: &#39;&lt;https://www.facebook.com/&#39; + response[1].getResponse() + &#39;&gt;&#39;})})
  }

    if (response[2].getResponse()[0] === &#39;Notify the community manager of a livestream report in real time?&#39; &amp;&amp; response[1].getItem().getTitle() === &#39;Channel Identification&#39;)
  {
    UrlFetchApp.fetch(&#39;https://discord.com/api/webhooks/824374930422497331/X&#39;, {headers: {&#39;Content-Type&#39;: &#39;application/json&#39;}, method: &#39;POST&#39;, payload: JSON.stringify({content: &#39;&lt;https://www.youtube.com/channel/&#39; + response[1].getResponse() + &#39;&gt;&#39;})})
  }

      if (response[2].getResponse()[0] === &#39;Notify the community manager of a livestream report in real time?&#39; &amp;&amp; response[1].getItem().getTitle() === &#39;Account Username&#39;)
  {
    UrlFetchApp.fetch(&#39;https://discord.com/api/webhooks/824374930422497331/X&#39;, {headers: {&#39;Content-Type&#39;: &#39;application/json&#39;}, method: &#39;POST&#39;, payload: JSON.stringify({content: &#39;&lt;https://www.tiktok.com/@&#39; + response[1].getResponse() + &#39;&gt;&#39;})})
  }
}

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。

/**
 * 当接收到新条目时执行。需要使用编辑 ---&gt;  当前项目触发器 来设置。
 */
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 ---&gt;  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>



huangapple
  • 本文由 发表于 2023年1月9日 08:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定