[英]Return web form values to Google app script
I have a google sheets script that allows the user to enter certain data. 我有一个Google表格脚本,该脚本允许用户输入某些数据。 I want to be able to pass these values back to a function in the google app script when the user clicks OK.
当用户单击“确定”时,我希望能够将这些值传递回google app脚本中的函数。
Here is the Google sheet script I am trying to get to work. 这是我要开始使用的Google工作表脚本。 The function checkLogin does get called until I try to pass the values to it from the web page.
在我尝试从网页将值传递给它之前,函数checkLogin确实会被调用。
The gscript 脚本
function onOpen() {
openLogin();
SpreadsheetApp.getUi()
.createMenu('Dialog')
.addItem('Open', 'openLogin')
.addToUi()
}
function openLogin() {
var html = HtmlService.createHtmlOutputFromFile('index');
SpreadsheetApp.getUi()
.showModalDialog(html, 'Login Form');
}
function checkLogin(user_name, user_password, staff, student) {
SpreadsheetApp.getUi().alert(user_name);
}
and the web page code is as follows: 网页代码如下:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
First name:
<input type="text" name="user_name"><br><br>
Last name:
<input type="password" name="user_password"><br><br>
Staff or Student?
<br>
<input type="radio" name="staff" value="staff" checked> Staff<br>
<input type="radio" name="student" value="student"> Student<br>
<br><br>
<input type="button" value="OK"
onclick="google.script.run.checkLogin(user_name, user_password, staff, student)" />
<input type="button" value="Close"
onclick="google.script.host.close()" />
</form>
</body>
</html>
I hope someone can point me in the right direction. 我希望有人能指出我正确的方向。 Thanks
谢谢
The button to submit the login information needs to be changed. 需要更改提交登录信息的按钮。 There needs to be a way to collect the input information.
需要一种收集输入信息的方法。 Right now none of the information in the form is being sent to the server.
现在,表格中的任何信息都没有发送到服务器。 Either the form object needs to be sent to the server, or the values need to be retrieved some other way.
表单对象需要发送到服务器,或者值需要以其他方式检索。 If you want to use the form object, you must get it with
this.parentNode
如果要使用表单对象,则必须使用
this.parentNode
获取它
onclick="google.script.run.withSuccessHandler(showMsgForLoginAttempt)
.checkLogin(this.parentNode)" />
Add a <script>
tag to the index.html
file with a success handler. 使用成功处理程序将
<script>
标记添加到index.html
文件。 Add withSuccessHandler()
to the google.script.run
server call. 将
withSuccessHandler()
添加到google.script.run
服务器调用中。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
First name:
<input type="text" name="user_name"><br><br>
Password:
<input type="password" name="user_password"><br><br>
Staff or Student?
<br>
<input type="radio" name="staff" value="staff" checked> Staff<br>
<input type="radio" name="student" value="student"> Student<br>
<br><br>
<input type="button" value="OK"
onclick="google.script.run.withSuccessHandler(showMsgForLoginAttempt).checkLogin(this.parentNode)" />
<input type="button" value="Close"
onclick="google.script.host.close()" />
</form>
</body>
<script>
window.showMsgForLoginAttempt = function(valueFromServer) {
console.log('showMsgForLoginAttempt ran');
alert('Your attempt: ' + valueFromServer);
};
</script>
</html>
function checkLogin(formObject) {
var passwordsMatch;
Logger.log('formObject: ' + formObject)
Logger.log('formObject: ' + formObject.user_name);
formObject
//SpreadsheetApp.getUi().alert('Hello, world!');
passwordsMatch = true; //check password
if (passwordsMatch) {
return true;
} else {
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.