取得最新的Form Response有时候会获得最新的Form Response [英] Getting latest Form Response sometimes gets the one before it instead

查看:80
本文介绍了取得最新的Form Response有时候会获得最新的Form Response的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否与Google Apps脚本的繁忙时间有关,因为它似乎与更新formResponse []数组的长度(偶尔)有关。我使用以下代码来获取由表单提交触发的最新响应:

  var form = FormApp.getActiveForm() ; 
var formResponses = form.getResponses();
var formResponse = formResponses [formResponses.length-1]; //最新响应
Logger.log('begin length:'+ formResponses.length);

然后我的脚本的其余部分与formResponse []数组中的答案进行交互。偶尔,我会注意到它已经在之前得到了最新的回应。我可以验证这一点,因为带有表单响应的电子表格显示实际的最新响应。我的脚本需要5-15秒才能执行,所以我在我的代码结尾再次检查数组长度:

  var form2 = FormApp.getActiveForm(); 
var formResponses2 = form2.getResponses();
Logger.log('end length:'+ formResponses2.length);

在日志中我会注意到第二个比第一个大第二个是正确的值)。我并没有真正发现它发生的时间,但它似乎更常发生在太平洋标准时间上午7点到9点之间。现在我已经添加了一个 Utilities.sleep(5000)作为函数中的第一行,以便在获取响应之前为表单更新提供时间,并且到目前为止我没有收到任何n-2回复,这让我觉得有些延迟会导致表单在表单提交触发器触发后记录最新回复。



有没有其他人遇到类似的情况?

解决方案

忙,这些种族条件将变得更加明显,但对于云计算,它们只是照常营业。 。简而言之,文档的每个用户都拥有该文档的一个视图(复制),该文档不能保证始终与主版本相同。当您查看电子表格时,您正在查看自己的电子表格副本。你的合作者可能正在查看他们自己的副本。实际上,访问电子表格的触发器功能也将被赋予其自己的副本。在任何地方进行的更改都需要同步,这需要时间。



在这种情况下,您的代码表明您在Google表单中包含的脚本中具有一项功能。该脚本触发后将获得一份表格副本,包括过去的回复。但是,触发脚本的表单提交可能无法与表单提交同步。您还正在处理包含回复的电子表格...当表单提交到表单服务时,它们存储在表单服务中,并且它们也存储在电子表格的副本中。这个动作可能会触发一个Spreadsheet Form Submission事件,并且(你头痛吗?)那个函数将被给予一个电子表格的副本,它可能还没有包含新的表单提交数据!



那么,该怎么办?



假设您使用触发器函数来处理表单响应。

 函数handleForm(event){
...
}

如果您只需处理当前表单回复,则应使用交给触发器的事件信息,而不是阅读电子表格或查询表单回复。通读了解事件来看看事件的信息提供给特定类型的触发,你正在处理。 (额外的好处:使用event参数可以让你不用调用服务API,这会让你的功能更快。)



pre $ function $ handleForm事件){
var formResponse = event.response; //触发这个
//函数的响应是事件
...
}

我建议你也看看如何测试GAS中的触发器功能?


I'm wondering if this has to do with particularly busy times for Google Apps Script, because it seems like it has to do with an (occasional) delay in updating the length of a formResponse[] array. I'm using the following code to get the latest response triggered by a form submit:

var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1]; //latest response only
Logger.log('begin length: ' + formResponses.length);

Then the rest of my script interacts with the answers in the formResponse[] array. Occasionally, I'll notice that it has gotten the response before the latest response. I can verify this because the spreadsheet with the form responses shows the actual latest response. My script takes 5-15 seconds to execute, so I I have the following lines at the end of my code to double check the length of the array again:

var form2 = FormApp.getActiveForm();
var formResponses2 = form2.getResponses();
Logger.log('end length: ' + formResponses2.length);

and in the log I'll notice that the second one is one greater than the first (and the second one is the correct value). I haven't really found much pattern as to when it happens, but it seems to happen more often between the hours of 7-9am PST. For now I've added a Utilities.sleep(5000) as the first line in the function to allow time for the form to update before I get the responses, and so far I haven't had any n-2 responses, which makes me think there is some sort of delay causing the form to record the latest response after the "on form submit" trigger fires.

Has anyone else encountered anything similar?

解决方案

When servers are busy, these race conditions will become more pronounced, but they are simply business as usual for cloud computing. In a nutshell, each user of a document has a view (copy) of that document, which cannot be guaranteed to be identical to a "master version" all the time. When you look at a spreadsheet, you are looking at your own copy of that spreadsheet. Your collaborator might be looking at their own copy. A trigger function accessing "the spreadsheet" will, in fact, be given its own copy as well. Changes made anywhere need to be synchronized everywhere, and that takes time.

In this case your code indicates that you have a function in a script that is contained in a Google Form. That script, when triggered, will be given a copy of the Form, including past responses. However, the form submission that triggered the script may not be synchronized with the form submission yet. You're also working with a spreadsheet that contains responses... when forms are submitted to the Form Service, they are stored in the Forms Service and they are also stored in a copy of the spreadsheet. That action may trigger a Spreadsheet Form Submission event, and (is your head sore yet?) that function will be given a copy of the spreadsheet that might not yet contain the new form submission data!

So, what to do?

Let's assume you're using a trigger function to handle form responses.

function handleForm( event ) {
   ...
}

If you only need to process the "current form response", you should use the event information that is handed to the trigger, rather than reading the spreadsheet or querying the form responses. Read over Understanding Events to see what event information is provided to the specific type of trigger you're dealing with. (Added bonus: using the event parameter saves you from calling the services APIs, which makes your function faster.)

function handleForm( event ) {
   var formResponse = event.response;  // The response that triggered this
                                       // function is in the event
   ...
}

I suggest you also take a look at "How can I test a trigger function in GAS?".

这篇关于取得最新的Form Response有时候会获得最新的Form Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆