我正在通过 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 菜单调用该函数,它就可以工作。
我错过了什么???
你的脚本在哪里?您需要将
google.script.run
包含在 HTML 脚本中。
尝试这样的事情:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
function Cancel_Version2(){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.deleteActiveSheet();
}
<?!= 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>
在.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; ?>;"/>
相关