google.script.run在HTML模板中不起作用 [英] google.script.run does not work from HTML template
问题描述
大家好,谢谢您的帮助. 这是我的问题:
Hi all and thank you for your help in advance. Here is my problem:
我正在通过HTML模板从库中开发电子表格工具,但是服务器功能不起作用.
I am developing spreadsheet tools from a library, through an HTML template, but the server functions do not work.
gs简化代码(功能更长):
gs simplified code (the functions are longer):
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菜单调用该函数,那么它将起作用.
Why the code action does not work??. Even I have substituted <?!= action; ?>
with Cancel_Version2()
but still does not work. On the other hand, if I call directly the function from a onOpen menu, it works.
我想念什么???由于我正在追逐这几天,请提出建议!
What am I missing??? Please advice as I'm chasing this many days!!
推荐答案
您的脚本在哪里?您需要在HTML脚本中包含google.script.run
.
Where is your script? You need to include the google.script.run
inside a script in the HTML.
尝试这样的事情:
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()将是您正在调用的第一个函数的响应函数.
You can also run it with a success or failure handler to get a call back function. Where onSuccess() & onFailure() would be response functions of the first function you are calling.
<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>
这篇关于google.script.run在HTML模板中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!