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

社会主义新天地 提交于 2021-01-07 07:10:10

问题


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.


回答1:


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')
}

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.

As stated in the documentation for 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.

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.



来源:https://stackoverflow.com/questions/61356994/how-to-dynamically-create-installable-triggers-as-other-users-in-google-apps-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!