2

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

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");
  }
}

如果我尝试从脚本编辑器运行 onEdit,则会创建触发器,但电子表格上的每次编辑都会在toast中收到“错误”消息

有人可以帮我理解吗?

4

1 回答 1

4

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.

于 2012-05-24T11:34:15.530 回答