5

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); 
}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
Bjorn Behrendt
  • 1,204
  • 17
  • 35

2 Answers2

4

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'));
}
Bjorn Behrendt
  • 1,204
  • 17
  • 35
3

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);
};
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Thank you for the detailed description, and I understand what you are saying. However I want the variables to pass to the onRun() function when a button in clicked. This means that the onRun(id,min) (or google.script.run.onRun(id,min)) needs to be ran from the HtmlService instead of the directly from gs file. – Bjorn Behrendt Dec 27 '14 at 23:48
  • You don't need to pass the variables. You need to put the values into Cache or into a global variable. See my updated answer. – Alan Wells Dec 28 '14 at 00:13
  • I had tested using a private cache and it produced an error, when I switched to a public one, everything stated working as it should. I posted a second answer with updated code from my original post. – Bjorn Behrendt Dec 28 '14 at 05:05