可安装的触发器以其他形式触发脚本? [英] Installable trigger to fire script in a different form?
问题描述
我正在尝试运行提交不同表单(B)时连接到一种表单(A)的脚本.我有一系列相似的不同形式,需要运行相同的脚本,因此我不希望将代码复制到每个脚本中,而不想这样做.我在A中成功地制作了一个可安装的触发器函数,指向B.在出现提示时,我已授予权限.当我发布B时,A中的脚本会触发,并且我会在日志中得到它:
I'm trying to run a script that is connected to one form (A) when a different form (B) is submitted. I have a series of different forms that are similar and needs to run the same script, so instead of copying the code to each script I wonder if this is possible. I successfully made an installable trigger function in A, pointing at B. I have granted permission when prompted. When i post B, the script in A fires, and I get this in the log:
Exception: No response with ID 2_ABaOnudSFDkNQOL2Xn4fNOmT95GrTotEW8LSjxfI5qf6qceDN5hD5CHKqNT5D4G_DdONWq0 exists for this form.
at onFormAnswerSubmit(Kod:40:20)
暂停的行(Kod:40:20)是获取已发布数据的行:
The line (Kod:40:20) that halts is the line that fetches the posted data:
var items = e.response.getItemResponses();
这应该可行吗,还是不可能将发布的数据从一种形式传递到另一种形式的脚本中?
Is this supposed to work or is it impossible to pass posted data from one form to a script in a different form?
或者在触发条件下还有其他事情可以做吗?就像强迫e传递一样.或者其他的东西?这是我创建的触发器:
Or is there something more I can do in the trigger? Like forcing a pass on of e. or something? Here is the trigger I created:
/**
* Creates a trigger for when a different form is posted that runs a function in this script (run once)
*/
function createOtherFormTrigger() {
var formID = FormApp.openById("XXXXX"); // form B
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form A
.forForm(formID)
.onFormSubmit()
.create();
}
推荐答案
您需要稍微更改一下逻辑:
You need to change the logic a bit:
为表单B创建formSubmit触发器的示例代码:
Sample code to create a formSubmit trigger for form B:
//run this once
function createOtherFormTrigger() {
var formB = FormApp.openById("XXX");
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form B
.forForm(formB) //the active form is Form B
.onFormSubmit()
.create();
}
function onFormAnswerSubmit(e){
var formB = e.source;
var items = e.response.getItemResponses(); //items of the latest form response of form B
var formA = FormApp.openById("XXXX"); // Form A
...
// do what you need to do with form A
}
上面的代码将创建触发器,该触发器将在提交表单B的表单响应时触发.因此,将脚本绑定到表格A,表格B还是独立.实际上,在独立脚本中实现这些功能可能最有意义,以确保它们不会干扰您可能在表单绑定脚本中拥有的其他表单提交触发器.
The code above will create trigger that fires when a form response for form B is being submitted. Thereby it is not important either the script is bound to form A, form B or is standalone. Actually, it might make mosst sense to implement those functions in a standalone script, to make sure that they don't interfer with other form submit triggers you might have in a form-bound script.
这篇关于可安装的触发器以其他形式触发脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!