最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a doGet(e) parameter to another function? - Stack Overflow

programmeradmin3浏览0评论

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

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); 
}
Share Improve this question edited Dec 27, 2014 at 16:12 Alan Wells 31.3k16 gold badges112 silver badges162 bronze badges asked Dec 27, 2014 at 5:01 Bjorn BehrendtBjorn Behrendt 1,26418 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

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 ma:

function doGet(e) {

  code here;

  onRun(id, minutes);
}

function onRun(argId, argMinutes) {
  Logger.log('argId: ' + argId);
  Logger.log('argMinutes: ' + argMinutes);
};
发布评论

评论列表(0)

  1. 暂无评论