HTML表单未运行(withSuccessHandler)函数 [英] HTML form is not running a (withSuccessHandler)function
本文介绍了HTML表单未运行(withSuccessHandler)函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Google App脚本(表格)中有此HTML表单,该表单要求用户提供日期值,然后提交该值.HTML表单运行.唯一的问题是obj不记录.我不知道为什么会这样.
I have this HTML form in Google App Script(Sheets) that asks a user for a date value and then to submit the value. The HTML form runs. the only problem is the obj doesn't log. I can't figure out why this is.
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form id="myForm">
<input type="date" name="startDate" value="" id="demo" >
<input type="button" style="font-family: verdana;" value="Submit" onclick="getStartDate()">
</form>
<script>
function success(msg) {
alert(msg);
}
function getStartDate(){
var form = document.getElementById("myForm").elements;
var obj ={};
for(var i = 0 ; i < form.length ; i++){
var item = form.item(i);
obj[item.name] = item.value;
}
google.script.run
.withSuccessHandler(success)
.getStartDate(obj);
google.script.host.close();
};
</script>
</body>
</html>
JavaScript:
The Javascript:
function getStartDate(obj){
Logger.log(obj)
}
这应该输出对象,但不会输出.
This should output the object but it doesn't.
感谢您的帮助!
推荐答案
此修改如何?
-
google.script.run
通过异步处理工作.因此,在您的脚本中,由于google.script.host.close()
,getStartDate()
和success()
无法运行. GAS端的 -
getStartDate()
不返回值.因此,alert(msg)
显示为undefined
.
google.script.run
works by the asynchronous processing. So in your script,getStartDate()
andsuccess()
are not run due togoogle.script.host.close()
.getStartDate()
at GAS side doesn't return values. Soalert(msg)
showsundefined
.
当以上几点反映到您的脚本中时,它如下所示.
When above points are reflected to your script, it becomes as follows.
请按如下所示修改Javascript.
Please modify Javascript as follows.
function success(msg) {
alert(JSON.stringify(msg)); // Modified
}
function getStartDate(){
var form = document.getElementById("myForm").elements;
var obj ={};
for(var i = 0 ; i < form.length ; i++){
var item = form.item(i);
obj[item.name] = item.value;
}
google.script.run
.withSuccessHandler(function(e) { // Modified
success(e);
google.script.host.close();
})
.getStartDate(obj);
};
气体:
function getStartDate(obj){
Logger.log(obj)
return obj; // Modified
}
注意:
- 如果您想将密钥提供给
Submit
,请将name
添加到< input type ="button" style ="font-family:verdana;"value ="Submit" onclick ="getStartDate()">
. - 当然,您也可以将
google.script.host.close()
移至success()
. - If you want to give the key to
Submit
, please addname
to<input type="button" style="font-family: verdana;" value="Submit" onclick="getStartDate()">
. - Of course, you can also move
google.script.host.close()
tosuccess()
.
Note:
如果这不是您想要的,请告诉我.我想修改它.
If this was not what you want, please tell me. I would like to modify it.
这篇关于HTML表单未运行(withSuccessHandler)函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文