简体   繁体   English

Google App脚本onOpen()触发器在工作表上不起作用

[英]Google App Scripts onOpen() trigger doesn't work on sheets

I am unable to get my script to run on opening the spreadsheet. 我无法在打开电子表格时运行脚本。 I've set the trigger manually see . 我已经设定触发手动看到 Authorization scope has been set too. 授权范围也已设置。

The code is intended to take some contact information from a table in a sheet, create a Contact and then add that Contact to an email list specified in the table. 该代码旨在从工作表的表格中获取一些联系信息,创建联系人,然后将该联系人添加到表格中指定的电子邮件列表中。 There is a function to check if this email already exists and prevent duplication. 有一个功能可以检查此电子邮件是否已经存在并防止重复。 The code works fine if I run it from the script editor. 如果我从脚本编辑器运行该代码,则该代码可以正常工作。 I'm not sure why I am unable to run it with an onOpen(e) trigger. 我不确定为什么无法使用onOpen(e)触发器运行它。

I thought the issue is related to this but the bare minimum code works for me and creates a second sheet on the open trigger. 我以为问题与有关,但是最起码的代码对我有用,并在打开触发器上创建了第二张表。

Any help appreciated as I'm pretty stuck - it must be something with my code. 任何帮助都表示赞赏,因为我很坚持–它必须与我的代码有关。

Code: 码:

 function onOpen(e) { var sheet = SpreadsheetApp.getActiveSheet(); var dataRange = sheet.getDataRange(); var data = dataRange.getValues(); // Iterate through all the data minus the header for (var i=1; i<data.length; i++){ currApplicant = data[i] applicantFirstName = currApplicant[1] applicantLastName = currApplicant[2] applicantEmail = currApplicant[3] emailGroup = currApplicant[13] addToEmailBool = currApplicant[12] //do you want to add them to the email list? var numDuplicates = 0; Logger.log(applicantEmail); if ((addToEmailBool == 1) && (emailGroup != "")) { var duplicateCounter = 0; var numDuplicates = checkForDuplicates(emailGroup, applicantEmail); if (numDuplicates==0){ var contact = ContactsApp.createContact(applicantFirstName, applicantLastName, applicantEmail); var members = ContactsApp.getContactGroup(emailGroup); members.addContact(contact); Logger.log("Adding:", applicantEmail) Browser.msgBox("Added new contact"); } } } } function checkForDuplicates(emailGroup, applicantEmail) { var duplicateCounter = 0; var groupContacts = ContactsApp.getContactGroup(emailGroup).getContacts() //go thru all the contacts in this group and check if their emails == applicantEmail for (var i in groupContacts) { var emails = groupContacts[i].getEmails(); for (var e in emails) { if (emails[e].getAddress() == applicantEmail){ duplicateCounter += 1; Logger.log("Duplicate found:", applicantEmail); } } } return duplicateCounter; } 

Simple Triggers cannot be used for processes that require authorization. 简单触发器不能用于需要授权的流程。

  • Reference Check the 4th bullet point. 参考检查第四个项目符号点。

Reading Down a little further the documentation states: 进一步阅读文档说明:

G Suite application triggers G Suite应用程序触发器

Installable triggers for G Suite applications are conceptually similar to simple triggers like onOpen(), but they can respond to additional events, and they behave differently. G Suite应用程序的可安装触发器在概念上类似于像onOpen()这样的简单触发器,但是它们可以响应其他事件,并且行为不同。

For example, the installable open trigger for Google Sheets activates whenever the spreadsheet is opened by any user who has edit access, just like the simple onOpen() trigger. 例如,只要具有编辑权限的任何用户打开电子表格,就可以激活Google表格的可安装打开触发器,就像简单的onOpen()触发器一样。 However, the installable version can call services that require authorization. 但是,可安装版本可以调用需要授权的服务。 The installable version runs with the authorization of the user who created the trigger, even if another user with edit access opens the spreadsheet. 即使另一个具有编辑访问权限的用户打开了电子表格,该可安装版本仍在创建触发器的用户的授权下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM