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);
}
SOLUTION
There is a function which handles the other functions and the trigger:
trigger function:
and the main_function function: