如何像其他用户一样在Google Apps脚本中动态创建可安装的触发器? [英] How to dynamically create installable triggers as other users in Google Apps Script?

查看:52
本文介绍了如何像其他用户一样在Google Apps脚本中动态创建可安装的触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个绑定Google表单的脚​​本,该脚本在每次提交表单时都会创建一个可安装的触发器.现在,我将表格和脚本共享给了实际用户.在某些时候,我注意到由于已经达到配额限制,所以没有创建新的触发器.当我查看已安装的触发器列表时,我注意到所有触发器均以"Me"的形式安装.我该如何做,以便将实际提交表单(并在流程中创建了可安装触发器)的其他用户标识为触发器的所有者",而不是我.

So I have a Google Form-bound script that creates an installable trigger every time the Form is submitted. Now, I shared both the Form and the script to the actual users. At some point I have noticed that new triggers are not being created because quota limit is already reached. As I reviewed the list of installed triggers, I noticed that all of them are installed as "Me". How do I do it so that the other users who actually submitted the form (and created the installable trigger in the process) are identified as the "Owner" of the trigger and not me.

推荐答案

假设您的代码如下所示:

Assuming your code looks something like this:

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 函数并授予权限的人,将为您的帐户创建触发器(安装为我").您需要要求每个用户自己运行 createOnFormSubmitTrigger 函数,以便授予权限并在其帐户下安装触发器.在这种情况下,这没有多大意义,因为无论用户提交表单时,每次提交表单时都会运行每个用户触发器.

And you're the one running the createOnFormSubmitTrigger function and granting the permissions, the trigger will be created for your account (installed as "me"). You would need to ask each user to run the createOnFormSubmitTrigger function by themselves in order to grant the permissions and get the trigger installed under their account. Which in this case wouldn't make much sense because each user trigger would be run each time a form submission is made no matter who submitted it.

可安装触发器的文档所述:

可安装触发器始终在以下人员的帮助下运行:创建了它们.例如,如果您创建一个可安装的打开触发器,它在您的同事打开文档时运行(如果您的同事有编辑访问权限),但它以您的帐户身份运行.这意味着,如果您创建一个触发器以在打开文档时发送电子邮件,该电子邮件始终是从您的帐户发送的,不一定是打开文档.但是,您可以创建一个可安装的触发器对于每个帐户,这将导致每个帐户发送一封电子邮件帐户.

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来尝试解决您的用例方法,该方法将返回触发器所有者用户并将其与提交表单的用户进行比较,您可以通过

EDIT

You could try a workaround for your use-case by using 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 method, using an if statement to compare both user's emails and run the code you need depending on that. This way the triggers will be run every time but the necessary code would be run only when the trigger owner is the same as the form respondent.

这篇关于如何像其他用户一样在Google Apps脚本中动态创建可安装的触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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