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);
}
PropertiesService
by setting a key and a value - the value would be the parameters your function needs, and the key is the trigger ID. In your triggered function, you use the event object and look up the stored value for the trigger ID in the event object. Apps Script documentation on triggers, event objects, and Properties Service should have what you need to write a script - you can get better help once you have that effort to share. – tehhowchevent.triggerUid
The problem is that I don't know how to find the event object and the docs makes an example receiving the event object. developers.google.com/apps-script/guides/triggers/events How can I call the event object? Thank you!! :) – Davidemain_function
,onEdit
, etc.) receives the event object as an input argument automatically. You can refer to it in the triggered function as eitherarguments[0]
, or, if you specified a parameter name, that parameter name (e.g.evobj
infunction foo(evobj) { /** code */}
). Google will handle creating the event object each time the trigger activates. – tehhowch