Getting latest Form Response sometimes gets the one before it instead

三世轮回 提交于 2019-11-28 11:30:28
Mogsdad

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?".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!