Javascript gs函数传递';未定义';到jsfunction(动态表单更新)
谷歌应用程序脚本中动态更新的表单有问题Javascript gs函数传递';未定义';到jsfunction(动态表单更新),javascript,google-apps-script,Javascript,Google Apps Script,谷歌应用程序脚本中动态更新的表单有问题表单位于HTML模板(比如index.HTML)中,如下所示: <form id="login"> <p>Enter username and password below, or use the links to the right for more services:</p> <input type="text" placeholder="username" id="username" name="usernam
表单位于HTML模板(比如index.HTML)中,如下所示:
<form id="login">
<p>Enter username and password below, or use the links to the right for more services:</p>
<input type="text" placeholder="username" id="username" name="username" />
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" value="Login" onclick="google.script.run.withSuccessHandler(form_login).processForm(this.parentNode)" id="login_button" />
</form>
function form_login(formObject) {
//connect to users spread sheet
var userssheet = SpreadsheetApp.openById("SPREADSHEET-ID-STRING");
//get variables handed over and existing on page
var username = formObject.username;
var pass = formObject.password;
//get where data range in spreadsheet
var searchrange = userssheet.getActiveSheet().getDataRange();
//check if username exists and return password (change to check if password and username are correct and)
if (find(username, searchrange)) {
var enteredpassword = searchrange.getCell(find(username, searchrange),4).getValue();
var message = (username+" Found! Password is "+enteredpassword); //get password value
} else {
var message = (username+" Not Found!");
}
//check if password entered is correct
if (enteredpassword) {
if (enteredpassword==pass) {
message = message + "Correct Password";
} else {
message = message + "Incorrect Password";
}
}
return message;
}
gs文件中处理此问题的函数如下所示:
<form id="login">
<p>Enter username and password below, or use the links to the right for more services:</p>
<input type="text" placeholder="username" id="username" name="username" />
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" value="Login" onclick="google.script.run.withSuccessHandler(form_login).processForm(this.parentNode)" id="login_button" />
</form>
function form_login(formObject) {
//connect to users spread sheet
var userssheet = SpreadsheetApp.openById("SPREADSHEET-ID-STRING");
//get variables handed over and existing on page
var username = formObject.username;
var pass = formObject.password;
//get where data range in spreadsheet
var searchrange = userssheet.getActiveSheet().getDataRange();
//check if username exists and return password (change to check if password and username are correct and)
if (find(username, searchrange)) {
var enteredpassword = searchrange.getCell(find(username, searchrange),4).getValue();
var message = (username+" Found! Password is "+enteredpassword); //get password value
} else {
var message = (username+" Not Found!");
}
//check if password entered is correct
if (enteredpassword) {
if (enteredpassword==pass) {
message = message + "Correct Password";
} else {
message = message + "Incorrect Password";
}
}
return message;
}
(显然,我还在构建功能)
问题在于gs功能,这可能是我的一个明显疏忽。它返回的任何内容都将作为“未定义”输出到HTML中。即使我更改返回消息函数中的code>返回“hello”代码>,我仍然会得到未定义的附加到输出div(我尝试在javascript函数中使用toString();
,但这没有改变任何事情)
有什么想法吗?我遗漏了什么?发现了问题所在,我认为这是我的错误。按钮操作(我从中复制然后编辑)是:
onclick="google.script.run
.withSuccessHandler(form_login)
.processForm(this.parentNode)"
问题是我忘了将processForm
更改为新的gs函数名:form\u login
。我更改了它(更改为do\u form\u login
,并更改了代码中的函数名,以避免与javascript代码混淆),它就像一个符咒一样工作(:
注意:谢谢桑迪的帮助,你让我仔细检查了代码,发现了这个错误