如何将 doGet(e) 参数传递给另一个函数?

Bjo*_*ndt 5 javascript google-apps-script

我能够从已发布的应用程序脚本的 url 捕获变量,但我不确定如何将该变量传递给另一个函数。如果包含变量,下面的脚本将不会运行 onRun 函数。我的目标是传递 2 个变量,但一次传递一个问题。

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); 
}
Run Code Online (Sandbox Code Playgroud)

Bjo*_*ndt 5

诀窍是使用公共缓存(谢谢 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'));
}
Run Code Online (Sandbox Code Playgroud)


Ala*_*lls 3

要么使用全局变量,要么将值放入缓存中。

要声明全局变量,请在函数外部声明该变量:

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

}
Run Code Online (Sandbox Code Playgroud)

或者将值放入缓存:

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

}
Run Code Online (Sandbox Code Playgroud)

然后,您可以从缓存中检索值,或在代码中的任何位置使用全局变量。

因此,您不需要将任何内容从 HTML 传递到代码.gs

我没有看到任何onRun()doGet()函数内调用该函数的内容。

可以从该doGet()函数调用另一个函数,只要它位于return. return在该点停止该功能。

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

}
Run Code Online (Sandbox Code Playgroud)

你不能拥有:

  return HtmlService.createHtmlOutput(html).setSandboxMode(HtmlService.SandboxMode.IFRAME);
  onRun(id, minutes);
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下命令从 HTML 脚本标记触发脚本运行window.onload = function() {code};

<script>
  window.onload = function() {
    console.log("This onload did run");
    code here;
  };
</script>
Run Code Online (Sandbox Code Playgroud)

要传递多个变量,只需用逗号分隔它们:

function doGet(e) {

  code here;

  onRun(id, minutes);
}

function onRun(argId, argMinutes) {
  Logger.log('argId: ' + argId);
  Logger.log('argMinutes: ' + argMinutes);
};
Run Code Online (Sandbox Code Playgroud)