我无法获得输入以在Google App脚本下工作 [英] I cannot get input to work under google app script
问题描述
我正在Google表格下编写一个Web应用程序,但无法使用输入字段.我究竟做错了什么? 一切正常,但uname始终为空(不是未定义).
I'm writing a web app under google sheets and can't get an input field to work. What am I doing wrong? everything works but uname is always empty (not undefined).
edit:在尽可能简化之后,我将添加完整的代码. 无论输入什么内容,我都会在日志中得到名称".
edit: I'm adding the full code after simplifying it as much as I could. In the log I get "name" regardless of the input I type in.
在文件代码中.gs:
function doGet () {
var participant = {};
var templ = HtmlService.createTemplateFromFile('out');
return templ.evaluate();;
}
function formSubmit(name) {
Logger.log("name " + name);
}
In out.html
In out.html
<!DOCTYPE html>
<html>
<head>
<base target ="_top">
</head>
<body dir="rtl"; background-color: #92a8d1;>
<label> Name 1 </label> <input type="text" id="firstname"><br>
<label> Name 2 </label> <input type="text" id="lastname"> <br><br>
<button type="button" id="send">Send</button>
<script>
document.getElementById("send").addEventListener("click", getData());
function getData(){
var uname = document.getElementById("firstname").value;
google.script.run.formSubmit(uname);
}
</script>
</body>
</html>
推荐答案
- 您想在单击按钮时检索
<input type="text" id="firstname">
的值.- 在当前情况下,当您使用脚本编辑器查看日志时,仅检索
name
.这是您当前的问题. - You want to retrieve the value of
<input type="text" id="firstname">
when the button is clicked.- In your current situation, when you see the log with the script editor, only
name
is retrieved. This is your current issue. - 在脚本中使用
document.getElementById("send").addEventListener("click", getData());
.在这种情况下,加载HTML时,getData()
由getData()
的()
运行.这样,uname
变为""
,并且""
发送到formSubmit(uname)
,然后,当您看到日志时,您会看到name
.而且,在这种情况下,即使单击该按钮,也无法运行google.script.run.formSubmit(uname);
.我认为这是问题中出现脚本问题的原因. - In your script,
document.getElementById("send").addEventListener("click", getData());
is used. In this case, when the HTML is loaded,getData()
is run by()
ofgetData()
. By this,uname
becomes""
and""
is sent toformSubmit(uname)
, then, when you see the log, you seename
. And also, in this case, even when the button is clicked,google.script.run.formSubmit(uname);
cannot be run. I think that this is the reason of the issue of your script in your question.
如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
为避免这种情况,请按如下所示修改脚本.
In order to avoid this, please modify your script as follows.
document.getElementById("send").addEventListener("click", getData());
到:
document.getElementById("send").addEventListener("click", getData);
- 通过对脚本的上述修改,当将
sample
输入到名称1"并单击发送"按钮时,可以使用脚本编辑器在日志中看到name sample
. - By the above modification for your script, when
sample
is inputted to "Name 1" and click "Send" button, you can seename sample
at the log with the script editor.
如果我误解了您的问题,而这不是您想要的结果,我深表歉意.
If I misunderstood your question and this was not the result you want, I apologize.
这篇关于我无法获得输入以在Google App脚本下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- In your current situation, when you see the log with the script editor, only
- 在当前情况下,当您使用脚本编辑器查看日志时,仅检索