【问题标题】:google.script.run does not work from HTML templategoogle.script.run 不适用于 HTML 模板
【发布时间】:2017-03-06 20:30:16
【问题描述】:

大家好,提前感谢您的帮助。 这是我的问题:

我正在通过 HTML 模板从库中开发电子表格工具,但服务器功能不起作用。

gs简化代码(功能较长):

function Cancel_Version(){

    return Action_Box("¡HEY!","You're to cancel active edition.\n\n Sure?","Cancel_Version2()");

}

function Action_Box(TITLE,MESSAGE,FUNCTION){

   var html = HtmlService.createTemplateFromFile('ACTION_BOX');
   html.data = MESSAGE;
   html.action = FUNCTION;
   SpreadsheetApp.getUi().showModalDialog(html.evaluate().setHeight(200),TITLE);

}

function Cancel_Version2(){

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.deleteActiveSheet();

}

HTML 代码:

<!DOCTYPE html>

    <?!= include('CSS'); ?>

  <form style="font-size: 22px; font-family: 'calibri'; ">

        <?!= data; ?>
        <br>
        <br>
        <input type="button" value="ACCEPT" onclick="<?!= action; ?>; google.script.host.close();"/>
        <input type="button" value="CANCEL" onclick="google.script.host.close();"/>

  </form> 

为什么代码操作不起作用?即使我已经用Cancel_Version2() 替换了&lt;?!= action; ?&gt;,但仍然无法正常工作。另一方面,如果我直接从 onOpen 菜单调用该函数,它就可以工作。

我错过了什么???追了这么多天,求指教!!

【问题讨论】:

  • scriptlet &lt;?!= action; ?&gt; 不是指函数。所以,如果你试图从服务器返回一个函数名,那是行不通的。您可以从服务器获取全局变量。但我在您的服务器代码中没有看到任何名为 action 的全局变量。如果您要完成的是能够从同一个按钮调用不同的服务器功能,那么更改功能名称的标准是什么?您可以使用相同的服务器函数名称:google.script.run.alwaysTheSameFunctionName(different value) 但发送不同的值,并导致该函数
  • 根据传递给服务器的值分支到其他不同的函数。您可以在服务器上运行返回正确 google.script.run 文本的函数:&lt;?!= serverFunctionToGetCorrectCode() ?&gt; 然后在 .gs 代码中:function serverFunctionToGetCorrectCode() { if (1===1) {return 'google.script.run.myServerFunctionOne()';}}
  • 嗨桑迪。感谢您的回答。你是对的,我的目的是将不同的函数作为纯文本传递。通过“动作”变量。无论如何,忘记这一点,如果我得到这个并直接放置函数名称,它仍然会失败。为什么?

标签: javascript html google-apps-script


【解决方案1】:

你的脚本在哪里?您需要在 HTML 的脚本中包含 google.script.run

Info here
And here

试试这样的:

代码.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

function Cancel_Version2(){
  var ui = SpreadsheetApp.getUi();
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  ss.deleteActiveSheet();
}

index.html

<?!= include('CSS'); ?>

<form style="font-size: 22px; font-family: 'calibri'; ">
<?!= data; ?>
<br>
<br>
<input type="button" value="ACCEPT" onclick="deleteSheet()"/>
<input type="button" value="CANCEL" onclick="google.script.host.close();"/>
</form> 

<script>
  function deleteSheet() {
    google.script.run.Cancel_Version2()
  }
</script>

您还可以使用成功或失败处理程序运行它以获取回调函数。其中 onSuccess() 和 onFailure() 将是您调用的第一个函数的响应函数。

<script>
function onSuccess() {
  //create onSuccess response function
}

function onFailure() {
  //create onFailure response function
}

function deleteSheet() { 
  google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).Cancel_Version2()
}
</script>

【讨论】:

  • 您好,企鹅先生。我已经尝试过了,但没有任何反应。问题是我想使用 'HtmlService.createTemplateFromFile' 而不是 'HtmlService.createHtmlOutputFromFile' 因为我想为我显示的 html 对话框的不同标题、提示和操作使用相同的模板,所以我需要评估html.
  • 需要考虑的一件事是所有代码都在库中,并且我正在从电子表格执行。这与失败有关吗?在那种情况下,就好像我直接从自定义菜单执行该功能一样,它工作得很好。
  • 您需要将google.script 放在&lt;script&gt;&lt;/script&gt; 中,以便从谷歌脚本文件中调用函数以使HTML 服务正常工作。 onclick="&lt;?!= action; ?&gt;; google.script.host.close();"/&gt; 什么都不做。
  • 很抱歉不同意,但确实如此。现在我测试了另一件事,它可以工作:我已将所有代码(gs 和 html)作为绑定放在电子表格中,它运行完美!!最后我发现问题是代码在库中。问题是我不明白为什么,因为一开始使用 ui 警报时一切正常。有什么想法???
  • 这没什么?请帮忙!!
猜你喜欢
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2013-01-25
  • 2015-07-23
  • 2013-01-07
  • 2023-03-28
相关资源
最近更新 更多