I am able to capture a variable from the url of a published app script, but I am not sure how I can pass that variable to another function. The below script will not run the onRun function if a variable is included. My goal is to pass 2 variables, but one problem at a time.
function doGet(e) {
var id = e.parameter.id;
var minutes = e.parameter.min;
var html = '<p>'
+'<button onClick=google.script.run.onRun('+id+')>Run</button>' // Does not work when clicked
+'<button onClick=google.script.run.onRun()>Run without parameter</button>'
+'<button onClick=google.script.run.turnOn()>On</button>'
+'<button onClick=google.script.run.turnOff()>Off</button>'
+'</p>';
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function onRun(id){
Logger.log("id: "+id);
}
Either use a global variable, or put the values into cache.
To declare a global variable, declare the variable outside of a function:
var id = "";
var minutes = "";
function doGet(e) {
id = e.parameter.id;
minutes = e.parameter.min;
var html = . . . . . HTML HERE;
onRun(id, minutes);
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Or put the values into Cache:
function doGet(e) {
var id = e.parameter.id;
var minutes = e.parameter.min;
if (!!id) {cache.put('theid', id, 4000);};
if (!!minutes) {cache.put('min', minutes, 4000);};
var html = . . . . . HTML HERE;
onRun(id, minutes);
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Then you can retrieve the values from cache, or use the global variable anywhere in your code.
So, you don't need to pass anything from the HTML to the .gs
code.
I don't see anything calling the onRun()
function within the doGet()
function.
You can call another function from the doGet()
function, as long as it's before the return
. return
stops the function at that point.
function doGet(e) {
var id = e.parameter.id;
var minutes = e.parameter.min;
var html = . . . . . HTML HERE;
onRun(id, minutes);
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
You can't have:
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
onRun(id, minutes);
You can also trigger script to run from an HTML Script tag with window.onload = function() {code};
:
<script>
window.onload = function() {
console.log("This onload did run");
code here;
};
</script>
To pass multiple variables, just separate them by a comma:
function doGet(e) {
code here;
onRun(id, minutes);
}
function onRun(argId, argMinutes) {
Logger.log('argId: ' + argId);
Logger.log('argMinutes: ' + argMinutes);
};
The trick is to use a public cache (Thank you Sandy Good).
function doGet(e) {
var id = e.parameter.id;
var minutes = e.parameter.min;
var cache = CacheService.getPublicCache();
cache.put("id", id, 30);
cache.put("minutes", minutes, 30);
var html = '<p>'
+'<button onClick=google.script.run.onRun()>Run without parameter</button>'
+'<button onClick=google.script.run.turnOn()>On</button>'
+'<button onClick=google.script.run.turnOff()>Off</button>'
+'</p>';
return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function onRun(id){
var cache = CacheService.getPublicCache();
var id = cache.get('id'));
var minutes = cache.get('minutes'));
}