复制文件的工作脚本

Working script for copied files

我有一个安装了触发器的表单 (onFormSubmit) 我想为多人复制:“表单人 X”、“表单人 Y”...

复制的表单应该与源的行为完全相同。目前我正在使用函数 makeCopy(name, destination) 来执行此操作。问题是安装的触发器没有被复制,如果脚本不是 运行,在新表单的脚本编辑器中使用 newTrigger(functionName) 似乎没有添加触发器。

formFile.makeCopy(newFileName, formFolder);
var newFile = formFolder.getFilesByName(newFileName).next();
var newFileId = newFile.getId();

var newForm = FormApp.openById(newFileId);
newForm.setTitle(newFileName);
ScriptApp.newTrigger("onFormSubmit")
    .forForm(newForm)
    .onFormSubmit()
    .create();

原因可能是您必须允许脚本访问应用程序,如果是这种情况,有没有办法解决这个问题而不必为每个新表单打开脚本?

编辑

我注意到当我回答“人 X”的形式时,源形式被修改了。这很奇怪,因为我使用的是 ObjectEvent 的 source 形式。

它的工作方式很可能是 WAI。

表单提交EventObject的source定义为

A Form object, representing the Google Forms file to which the script is bound

Source

由于脚本仍然绑定到 "base" 表单,它将 return 基本表单作为源,即使另一个表单正在调用该函数。

向文档、电子表格或表单添加触发器的函数(例如 forForm(Form form))仅适用于插件所属的文档、电子表格或表单

Add-ons can only create triggers for the document or spreadsheet in which the add-on is used. That is, an add-on that is used in Google Doc A cannot create a trigger to monitor when Google Doc B is opened.

Source

一种解决方法是向新创建的表单添加一个必需的复选框,其中只有一个值是表单的 ID。然后,您可以在读取 FormResponse 时使用该 ID。