How to Connect a Script with Triggers and Function

2019-08-27 05:33发布

问题:

I have a standalone script with the following code:

var key = "ID of the Sheet";

function createTriggers() {

  ScriptApp.newTrigger('filter')
      .forSpreadsheet(key)
      .onEdit()
      .create();
}

function filter(){
var ss = SpreadsheetApp.openById(key);
// rest of the function below
}

This has 3 parts - The key of the Google spreadsheet the script is connected to, the trigger creation function of the spreadsheet and the function.

I need this script to be connected to multiple sheets. As of now, I am creating individual standalone script for each sheet.

How to have multiple sheets connected to the same standalone script when the code is in the above format?

If we cannot have multiple sheets connected to this script, is there any other way that's better than creating a standalone scripts for each and every sheet?

回答1:

Since there is a restriction for creating triggers

A script is bound to a Google Sheets, Docs, or Forms file if it was created from that document rather than as a standalone script.

You could save your script as standalone service with doGet or doPost function. And make requests to it from your spreadsheets by their own scripts on trigger.