Google脚本具有两个可进行编辑的触发器.我可以删除“简单的"邮件吗?一? [英] Google Script has two triggers for on edit. Can I remove the "simple" one?

查看:59
本文介绍了Google脚本具有两个可进行编辑的触发器.我可以删除“简单的"邮件吗?一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有onEdit触发器的电子表格脚本.出于某些原因,两个"code" onEdit 触发器被触发":一个已安装的触发器和一个简单的触发器.我设置了 onEdit 来发送电子邮件.已针对已安装的触发器进行了身份验证,并且工作正常.但是,每次触发简单触发器时,都会引发异常.是否可以删除或禁用简单触发器,以使其不会引发异常?

I have a Spreadsheet script with an onEdit trigger. For some reason two onEdit triggers are "fired": an installed trigger and a simple trigger. I set up onEdit to send an email. This was authenticated for the installed trigger and works fine. However, each time it fires the simple trigger it throws an exception. Is it possible to remove or disable the simple trigger so that it doesn't throw the exception?

我编写了一个简单的函数来检查脚本正在使用哪些触发器.即使它同时触发了简单触发和已安装触发,它也仅显示一个 onEdit 触发.

I wrote a simple function to check what triggers my script was using. It only shows one onEdit trigger, even though it triggers both simple and installed.

function checkTriggers(){
  var triggers = ScriptApp.getProjectTriggers();

  triggers.forEach(function(trigger){console.log(trigger.getHandlerFunction())});
}

checkTriggers()输出的屏幕截图:

推荐答案

合并两个 onEdit 触发器:

假设您有2个 onEdit 触发器.一种简单的方法称为 onEdit(e),另一种可安装的方法称为 myFunction(e).由于您已经授权了 myFunction(e)并且它是可安装的触发器,因此您可以将 onEdit(e)简单触发器重命名为其他名称,例如 myFunction2(e)并放入 myFunction(e):

Merge both onEdit triggers:

Assuming you have 2 onEdit triggers. One simple which is called onEdit(e) and one installable which is called myFunction(e). Since you have already authorized myFunction(e) and it is an installable trigger, you can rename your onEdit(e) simple trigger to a different name e.g. myFunction2(e) and put inside myFunction(e):

function myFunction(e){
  //code for the installable myFunction
  myFunction2(e) 
}

通过这种方式,将同时触发 myFunction myFunction2 .如果要完全删除 myFunction2 ,则只需删除或注释掉与此相关的代码.

In this way both myFunction and myFunction2 will be triggered. If you want to remove myFunction2 altogether, then simply remove or comment out the code that relates to that.

删除简单的 onEdit(e)触发器,因为您的脚本无法使用此触发器,因为您的脚本使用了需要授权的服务.

Remove the simple onEdit(e) trigger as it can't be used in your case because your script uses services that require authorization.

  • 将函数的名称从 onEdit(e)更改为其他名称,例如 myFunction(e).

  • Change the name of the function from onEdit(e) to something else e.g. myFunction(e).

myFunction 创建可安装 onEdit 触发器.

要为 myFunction 创建可安装的触发器,请使用此解决方案.即,执行 create_onEdit 函数:

To create an installable trigger for myFunction use this solution. Namely, execute the create_onEdit function:

function create_onEdit(){
  ScriptApp.newTrigger('myFunction')
  .forSpreadsheet(SpreadsheetApp.getActive())
  .onEdit()
  .create(); 
}

并确保 myFunction(e)位于同一脚本编辑器中.

and make sure myFunction(e) lives in the same script editor.

这篇关于Google脚本具有两个可进行编辑的触发器.我可以删除“简单的"邮件吗?一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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