如何在 Google Apps 脚本中像其他用户一样动态创建可安装的触发器?

How to dynamically create installable triggers as other users in Google Apps Script?

所以我有一个 Google 表单绑定脚本,每次提交表单时都会创建一个可安装的触发器。现在,我将表单和脚本都分享给了实际用户。在某些时候,我注意到没有创建新的触发器,因为已经达到了配额限制。当我查看已安装触发器的列表时,我注意到所有触发器都安装为 "Me"。我该怎么做才能将实际提交表单(并在此过程中创建可安装触发器)的其他用户识别为触发器的 "Owner" 而不是我。

假设您的代码如下所示:

function createOnFormSubmitTrigger() {
  var form = FormApp.openById('[FORM-ID]');
  ScriptApp.newTrigger('createTrigger')
  .forForm(form)
  .onFormSubmit()
  .create();
}

function createTrigger() {
  ScriptApp.newTrigger("myFunction2")
  .timeBased()
  .everyMinutes(10)
  .create();
}

function myFunction2 () {
  console.log('test')
}

并且您是 运行 启用 createOnFormSubmitTrigger 功能并授予权限的人,将为您的帐户创建触发器(安装为 "me")。您需要要求每个用户自己 运行 createOnFormSubmitTrigger 函数,以便授予权限并在他们的帐户下安装触发器。在这种情况下,这没有多大意义,因为无论是谁提交的,每次提交表单时每个用户触发器都会 运行。

installable triggers 的文档所述:

Installable triggers always run under the account of the person who created them. For example, if you create an installable open trigger, it runs when your colleague opens the document (if your colleague has edit access), but it runs as your account. This means that if you create a trigger to send an email when a document is opened, the email is always be sent from your account, not necessarily the account that opened the document. However, you could create an installable trigger for each account, which would result in one email sent from each account.

编辑

您可以尝试使用 getActiveUser method which would return the trigger owner user and compare it to the user who submitted the form, which you can get with getRespondentEmail 方法解决您的用例,使用 if 语句比较用户的电子邮件和 运行 您需要的代码,具体取决于.这样,触发器每次都是 运行,但只有当触发器所有者与表单响应者相同时,必要的代码才会是 运行。