Skip to content Skip to sidebar Skip to footer

How To Pass A Doget(e) Parameter To Another Function?

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 onRu

Solution 1:

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 = "";

functiondoGet(e) {

  id = e.parameter.id;
  minutes = e.parameter.min;

  var html = . . . . . HTMLHERE;

  onRun(id, minutes);
  returnHtmlService.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.

functiondoGet(e) {

  var id = e.parameter.id;
  var minutes = e.parameter.min;

  var html = . . . . . HTMLHERE;

  onRun(id, minutes);
  returnHtmlService.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:

functiondoGet(e) {

  code here;

  onRun(id, minutes);
}

functiononRun(argId, argMinutes) {
  Logger.log('argId: ' + argId);
  Logger.log('argMinutes: ' + argMinutes);
};

Solution 2:

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'));
}

Post a Comment for "How To Pass A Doget(e) Parameter To Another Function?"