How to use triggers with functions that require parameters from the spreadsheet?
AFAIK, you cannot. The solution is to create one single function, for example called RefreshAll
, inside which you call all the wrapper functions (GetValue
) containing the actual parameters which need to be passed to the function that actually does the job (RealFunctionThatDoesTheJob
).
Note: Sorry if this sounds complicated. If you only have one set of parameters (as opposed to 3, in the example below), you can do away with GetValue
and call RealFunctionThatDoesTheJob
directly from within the function that is being triggered (RefreshAll
).
Lastly, to have your triggers send to your spreadsheet those values that are dependent on parameters, you need to have the script push the values to the spreadsheet using (currentSheet.getRange(Row,Column).setValue(Value);
)
Here is the example:
function RefreshAll() {
var currentSheet = SpreadsheetApp.openById("ID").getSheetByName("MySheet");
var Value1 = GetValue1();
currentSheet.getRange(Row,Column).setValue(Value1);
var Value2 = GetValue2();
currentSheet.getRange(Row,Column).setValue(Value2);
var Value3 = GetValue3();
currentSheet.getRange(Row,Column).setValue(Value3);
}
function GetValue1() {
return RealFunctionThatDoesTheJob("Parameter1","Parameter2","Parameter3");
}
function GetValue2() {
return RealFunctionThatDoesTheJob("Parameter4","Parameter5","Parameter6");
}
function GetValue3() {
return RealFunctionThatDoesTheJob("Parameter7","Parameter8","Parameter9");
}
The IMPORTDATA()
function has a latency of approximately 1 hour, as explained in this post on the Google Product Forum.
You mentioned the fact that a script was being fired by a time based Google Apps Script trigger. I was thinking, if you're using a script in the first place, why not fetch the CSV file as well, like so:
Code
var url = 'url to public CSV file';
var text = UrlFetchApp.fetch(url).getContentText();
var csv = Utilities.parseCsv(text);
The var csv is turned into a 2d-array and can be used like any other array. This way, you always have the latest CSV file.
References
Best Answer
Say I'm forgetful, but I don't want to pester my coworker by calling her more than every two hours. I might remind myself just by jotting down when I call, so I can glance at that note next time I'm once again reaching for the phone.
A script could do the same, scribbling down when it needs to run again, and checking it later.
Right now I've set the frequency to your mentioned one hour. Apps Script times are in thousandths of a second, so leave on the minutes-conversion, "
* (60*1000)
" part if you ever change the timing.You'll see three "comment plus Logger call" pairs. They themselves are there for clarity, or your logging/testing, and could naturally be removed. Their position in the function and
if
s is all that matters. Arranged this way, at every onOpen the time is checked and three blocks of code are conditionally run.scriptProperty
so we can check it in future onOpen runs.