Google Scripts - trigger and function in different

2019-08-02 08:46发布

I need to:

  • create a new sheet
  • call the main function
  • set a trigger that will call the main function

Basically this:

function new_campaign(){
    var sheet_name = new_sheet();
    main_function(sheet_name);
    trigger(sheet_name);
}

With a single sheet there would be no problem because I could set before the sheet name as a global variable. The problem is that I have to create multiple sheets and all have to keep on working.

I created the function of the new sheet so that it's returning the name of the sheet, so I can call the main_function passing the sheet. Unfortunately for the trigger is not that easy because I don't understand how to pass the sheet.

function trigger(sheetName) {
  ScriptApp.newTrigger("main_function")
  .timeBased()
  .everyMinutes(1)
  .create();
}

UPDATE

As suggested, I tried tu use PropertiesService mapping the ID of the trigger with the parameter I need to use in the function (which in my case is the sheetName)

function trigger(sheetName) {
  var triggerID = ScriptApp.newTrigger("main_function")
  .timeBased()
  .everyMinutes(1)
  .create()
  .getUniqueId();
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperty(triggerID, sheetName);
}

UPDATE 2

I tried to extract the trigger ID from the event object to get the sheetName from the scriptProperties. Now my main_function look like this

function main_function() {
    var e = arguments[0];
    var scriptProperties = PropertiesService.getScriptProperties();
    var sheetName = scriptProperties.getProperty(e.triggerUid);
}

1条回答
smile是对你的礼貌
2楼-- · 2019-08-02 09:34

SOLUTION

There is a function which handles the other functions and the trigger:

function new_campaign(){
    var sheet_name = new_sheet(); //function to create a new sheet
    main_function(); //to execute main_function without delay the first time
    trigger(sheet_name); //set the trigger
}

trigger function:

function trigger(sheetName) {
  var triggerID = ScriptApp.newTrigger("main_function")
  .timeBased()
  .everyMinutes(1)
  .create()
  .getUniqueId();
  var scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperty(triggerID, sheetName); //association between sheetName and triggerID to call it back
}

and the main_function function:

function main_function() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheetName;

    var e = arguments[0]; //argument[0] contains the event object

    //if else to handle the first case
    //if the event object is defined, it extract the sheetName associated to the triggerID in the ScriptProperties
    //otherwise take the active sheet
    if (!(e == undefined)){
        var scriptProperties = PropertiesService.getScriptProperties();
        sheetName = scriptProperties.getProperty(e.triggerUid);
    }else{
        var sheet = ss.getActiveSheet();
        sheetName = sheet.getSheetName();
    }

    var sheet = ss.getSheetByName(sheetName);
    //...
}
查看更多
登录 后发表回答