google.script.run 无法在 HTML 模板中运行

问题描述 投票:0回答:2

我正在通过 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> 

为什么代码操作不起作用??即使我已将

<?!= action; ?>
替换为
Cancel_Version2()
但仍然不起作用。另一方面,如果我直接从 onOpen 菜单调用该函数,它就可以工作。

我错过了什么???

javascript google-apps-script modal-dialog template-engine server-communication
2个回答
0
投票

你的脚本在哪里?您需要将

google.script.run
包含在 HTML 脚本中。

信息在这里
这里

尝试这样的事情:

代码.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>

0
投票

在.gs文件的Action_Box函数上,替换

html.action = FUNCTION;

html.action = 'google.script.run.';
html.action += 'withSuccessHandler(google.script.host.close).'; 
html.action += FUNCTION;

在 ACTION_BOX.html 文件上而不是

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

使用

<input type="button" value="ACCEPT" onclick="<?!= action; ?>;"/>

相关

© www.soinside.com 2019 - 2024. All rights reserved.