onEdit可以编程方式创建一个触发器? [英] onEdit can programmatically create a trigger?

查看:67
本文介绍了onEdit可以编程方式创建一个触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的脚本,试图以编程方式从onEdit函数创建一个脚本。

  function onEdit(){
test();
}

function test(){
triggerLater();


函数customMsgBox(){
Browser.msgBox(hello world);


函数triggerLater(){
var date = new Date();
date.setMinutes(date.getMinutes()+ 1);
try {
var oneTimeOnly = ScriptApp.newTrigger(customMsgBox)
.timeBased()
.at(date)
.create();
返回oneTimeOnly.getUniqueId();
}
catch(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast(error);




$ b $ p
$ b如果我尝试从脚本编辑器运行onEdit,触发器已创建,但电子表格中的每一个编辑都会在一个 toast

中收到错误消息。

有人可以帮助我理解吗?

解决方案 onEdit 简单事件处理函数的权限有限(即不能发送电子邮件,打开日历等)。因为它运行时没有用户权限。因此,它不能设置触发器(它没有任何限制,并且会是一个巨大的安全漏洞)。



如果您希望在下创建触发器您的帐户,使用编辑触发器上的可安装。首先,将onEdit函数重命名为其他内容(因此它不会作为简单的事件处理程序触发),然后转到资源菜单并选择您的函数以在电子表格编辑事件上运行。查看文档获取更多信息。


I wrote a simple script trying to programmatically create a script from the onEdit function

function onEdit() {
  test();
}

function test() {
  triggerLater();
}

function customMsgBox() {
  Browser.msgBox("hello world");
}

function triggerLater() {
  var date = new Date();
  date.setMinutes(date.getMinutes() + 1);
  try {
    var oneTimeOnly = ScriptApp.newTrigger("customMsgBox")
      .timeBased()
      .at(date)
      .create();
    return oneTimeOnly.getUniqueId();
  }
  catch (e) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.toast("error");
  }
}

If I try to run onEdit from the script editor the trigger is created but every edit on the spreadsheet get a "error" message in a toast

Can someone help me to understand ?

解决方案

The onEdit simple event handler function has limited permissions (i.e. it can not send emails, open the calendar, etc). Because it runs without the users permissions. Therefore, it can not set up a trigger (which do not have any restrictions and would be a huge security bug).

If you expected that the trigger should be created under your account, use the installable on edit trigger. First, rename your onEdit function to something else (so it is not triggered as a simple event handler), then go to the Resources menu and pick your function to run on spreadsheet edit events. Take a look at the docs for more.

这篇关于onEdit可以编程方式创建一个触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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