在表单上提交触发器-Google文档表单的脚本结果 [英] On form submit trigger - scripting results from a Google Docs Form

查看:74
本文介绍了在表单上提交触发器-Google文档表单的脚本结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提交数据的表单,一旦将新表单数据附加到电子表格中,就需要进行测试和操作.因此,我希望使用提交表单时提交"触发器来处理最新的条目(新的最后一行).

I have a form that submits data which will require testing and manipulation once the new form data is appended to the spreadsheet. Therefore I hope to use an "On form submit" trigger to process the latest entry (new last line).

但是,在我对触发器没有太深入的了解之前,我很想知道在表单提交"触发器是否可靠.例如,它会立即触发吗?如果同时(或几乎同时)提交2个(或更多)表单,会发生什么情况?附加到该触发器的脚本是否会分别顺序处理每个表单提交?最终,我主要担心的是,如果从表单提交中追加新记录时,如果先前的脚本仍在运行,则追加记录将被从测试中跳过.

However before I get too deep into scripting for that trigger, I am curious to know if the "On form submit" trigger is reliable. For instance, does it trigger immediately? And what happens if 2 (or more) form submissions occur simultaneously (or near-simultaneously)? Will the script attached to that trigger process each form submission separately and sequentially? Ultimately my main concern is that an appended record will get skipped from testing if a previous script is still running while new records are being appended from a form submission.

另一种选择是时间驱动"触发器,尽管这将需要测试所有数据,然后处理满足特定条件的记录.我不反对使用这种类型的触发器,但是它将需要更复杂的脚本,以及对该过程的不同处理.

The other alternative is a "Time-driven" trigger, although that would require testing all of the data and then manipulating the records that meet certain criteria. I'm not opposed to using that type of trigger, but it will require more complex scripting, as well as a different approach to the process.

有人能与我分享与提交表单"有关的成功/恐怖故事吗?

Does anyone have success/horror stories relating to the "On form submit" trigger that they could share with me?

推荐答案

请参阅:类锁

互斥锁的表示.此类允许脚本确保一次仅一个脚本实例正在执行给定的代码段.这对于回调和触发器特别有用,在回调和触发器中,用户操作可能会导致共享资源发生更改,并且您要确保不会发生冲突.以下示例显示了如何在表单提交处理程序中使用锁.

A representation of a mutual-exclusion lock. This class allows scripts to make sure that only one instance of the script is executing a given section of code at a time. This is particularly useful for callbacks and triggers, where a user action may cause changes to a shared resource and you want to ensure that aren't collisions. The following examples shows how to use a lock in a form submit handler.

// Generates a unique ticket number for every form submission.
 function onFormSubmit(e) {
   var targetCell = e.range.offset(0, e.range.getNumColumns(), 1, 1);

   // Get a public lock on this script, because we're about to modify a shared resource.
   var lock = LockService.getPublicLock();
   // Wait for up to 30 seconds for other processes to finish.
   lock.waitLock(30000);

   var ticketNumber = Number(ScriptProperties.getProperty('lastTicketNumber')) + 1;
   ScriptProperties.setProperty('lastTicketNumber', ticketNumber);

   // Release the lock so that other processes can continue.
   lock.releaseLock();

   targetCell.setValue(ticketNumber);
 }

如果没有LockService,则如果两个用户在大约同一时间提交表单,则票证编号可能会以相同的结尾,这是因为lastTicketNumber属性在从ScriptProperties中读取之后但在回写新值之前可能会更改./p>

Without the LockService, if two users submit the form at approximately the same time the ticket numbers could end up the same, since the lastTicketNumber property could change after it was read from the ScriptProperties but before the new value was written back.

以上内容是从经过改进的新文档中复制的.

The above was copied from the new and improved documentation.

这篇关于在表单上提交触发器-Google文档表单的脚本结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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