Google apps script 谷歌应用程序脚本:否“;资料来源;事件触发器上的字段
我已经设置了一个提交谷歌表单的触发器来运行我的谷歌脚本:Google apps script 谷歌应用程序脚本:否“;资料来源;事件触发器上的字段,google-apps-script,triggers,google-forms,Google Apps Script,Triggers,Google Forms,我已经设置了一个提交谷歌表单的触发器来运行我的谷歌脚本: ScriptApp.newTrigger('onFormSubmitted') .forForm(form).onFormSubmit().create(); MyonFormSubmitted函数被触发,但提供给该函数的事件参数没有source属性。它有4个字段中的3个 那么,我应该如何获得触发此事件的表单的引用 TMI function onFormSubmitted(data, arg) { log(8, data['
ScriptApp.newTrigger('onFormSubmitted')
.forForm(form).onFormSubmit().create();
MyonFormSubmitted
函数被触发,但提供给该函数的事件参数没有source
属性。它有4个字段中的3个
那么,我应该如何获得触发此事件的表单的引用
TMI
function onFormSubmitted(data, arg) {
log(8, data['triggerUid'], -1) // => 1874727473075378640
log(9, data['authMode'], -1) // => FULL
log(10, data['response'], -1) // => FormResponse
log(11, data['source'], -1) // => undefined
}
如中所述,如果脚本未绑定,则没有关联的源属性。您可以通过使用触发器uid将表单id存储在中来克服这一问题。然后在表单提交代码中,使用触发器uid获取正确的表单
function getFormId_(triggerId) {
const store = PropertiesService.getScriptProperties();
var formId = store.getProperty(triggerId);
if (!formId) console.warn("No form ID found for trigger ID '" + triggerId);
return formId;
}
function createSubmitTrigger_(form, functionName) {
const formId = form.getId();
const trigger = ScriptApp.newTrigger(functionName).forForm(form)
.onFormSubmit()
.create();
const store = PropertiesService.getScriptProperties();
store.setProperty(trigger.getUniqueId(), formId);
}
function myFormSubmit(e) {
const form = FormApp.openById(getFormId_(e.triggerUid));
...
}
这种方法(存储和检索)可以向后应用,尽管这取决于触发器以前的配置方式-您不能以编程方式(或其他方式)与其他触发器交互
function storeAll() { // Run once (more is ok too, will just overwrite existing keys).
function cb(acc, t, i, allVals) { // callback for Array#reduce
acc[t.getUniqueId()] = t.getTriggerSourceId();
return acc;
}
const newProps = ScriptApp.getProjectTriggers().reduce(cb, {});
PropertiesService.getScriptProperties().setProperties(newProps);
}
参考资料
显示onFormSubmitted
功能代码和确切错误。如果应用程序脚本文件已绑定到表单,则可以获取活动表单<代码>变量形式=FormApp.getActiveForm()代码>这也适用于作为附加组件安装的独立文件。未用作加载项的独立文件将无法触发表单提交事件。我不确定有没有图书馆。但除非您将应用程序脚本文件用作库,否则我会尝试获取活动窗体。@SandyGood:hm。。。我用这个脚本创建了很多表单并注册了一个触发器。一个脚本可以绑定到多个表单吗?@SandyGood:是的,我对多个表单使用相同的函数。(表单是以编程方式生成的。)当myonFormSubmit
函数调用FormApp.getActiveForm()
时,我会得到未定义的。我相信@tehhowch的回答满足了你的问题。