【发布时间】:2020-10-09 00:21:00
【问题描述】:
好的,我在开始设置动态独立谷歌脚本时遇到了困难。我无法获得对 html 进行任何动态更改的按钮。我认为单击该按钮将调用 google 脚本代码函数并对生成的 Index.html 进行更改。我错过了什么?
代码.gs:
function doGet() {
return HtmlService
.createHtmlOutputFromFile('Index')
.setTitle('DynamicTest');
}
function DoSomething() {
var obj = document.getElementById("status");
alert("kachow");
obj.innerText = "It's magic!";
}
Index.html
<!DOCTYPE html>
<html>
<body>
<button id="submitButton">Submit</button>
<br />
<div id="status">Static</div>
<br />
<script>
document.getElementById('submitButton').addEventListener('click', function() {
google.script.run.DoSomething();
});
</script>
</body>
</html>
谢谢!
更新解决方案:感谢@Tanaike 的解决方案!这是完整的代码,它还添加了一条预处理消息以及一条在谷歌脚本应用程序完成调用函数后显示的后处理消息:
<!DOCTYPE html>
<html>
<body>
<button id="submitButton">Submit</button>
<br />
<div id="status">Static</div>
<br />
<script>
document.getElementById('submitButton').addEventListener('click', function() {
var obj = document.getElementById('status');
obj.innerText = 'Processing. Please wait...';
google.script.run.withSuccessHandler(value => {
var obj = document.getElementById("status");
obj.innerText = value;
}).DoSomething();
});
</script>
</body>
</html>
代码.gs
function doGet() {
return HtmlService
.createHtmlOutputFromFile('Index')
.setTitle('DynamicTest');
}
function DoSomething() {
return "It's magic!";
}
【问题讨论】:
标签: javascript html google-apps-script dynamic web-applications