Can a standalone script create a trigger for anoth

2019-08-10 20:30发布

问题:

I am trying to create an installable onEdit trigger for a spreadsheet bound script. I would like to do this programmatically with a separate, standalone script. It looks like this should be possible according to documentation:

Note that, unlike for a simple onOpen() trigger, the script for the installable trigger does not need to be bound to the spreadsheet. To create this trigger from a standalone script, simply replace SpreadsheetApp.getActive() with a call to SpreadsheetApp.openById(id). https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically

However, when I run the code below, the trigger is added to the standalone script project instead of the target, spreadsheet-bound script.

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.openById('1vcAgQ6vPZiILFX0fB_jojyrSdGKr7goD_iCQcFsImEM');
  ScriptApp.newTrigger('update')
   .forSpreadsheet(ss)
   .onEdit()
   .create();
}

What am I missing?

回答1:

Well, this may be possible, but not like that. I think you misunderstood the documentation a bit. Putting in other words, what it says is: to create a installable trigger, a script does not need to be bounded to the target spreadsheet. But the trigger is for the running script itself, as always. There's no installing a trigger for another script.

Scripts can only set triggers for themselves, and there's no API to set a trigger for another script.

You could have the bounded script published as a web-app, then the remote standalone script could call its URL, basically "telling" the bounded script that it's time to set its installable trigger.