google.script.run
doesn’t return values. So I tried this using jQuery.Deferred.
GAS : Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getValues(e) {
return e + "hoge";
}
HTML : index.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
<body>
<input type="button" id="button" value="ok">
<div id="result"></div>
<script>
$(() => {
$("#button").click(() => {
var str = "fuga";
googleScriptRun(str).then((res) => {
$('#result').text(res);
});
});
});
function googleScriptRun(str) {
var d = new $.Deferred();
google.script.run.withSuccessHandler((res) => {d.resolve(res)}).getValues(str);
return d.promise();
}
</script>
</body>
Result :
fugahoge
Above sample can be also written as follows.
$(() => {
$("#button").click(() => {
var str = "fuga";
google.script.run.withSuccessHandler((res) => {$('#result').text(res)}).getValues(str);
});
});