简体   繁体   English

将网络表单值返回到Google应用脚本

[英]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:&nbsp;&nbsp;
    <input type="text" name="user_name"><br><br>
    Last name:&nbsp;&nbsp;
    <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服务器调用中。

index html: 索引html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form>    
        First name:&nbsp;&nbsp;
        <input type="text" name="user_name"><br><br>
        Password:&nbsp;&nbsp;
        <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>

Server code: 服务器代码:

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.

相关问题 如何将Google App Script结果返回到外部网页? - How to return Google App Script result to external web page? 从表单谷歌应用程序脚本Web应用程序动态列表 - google apps script web app dynamic list from form 如何注册唯一值? 在电子表格谷歌应用程序脚本 web - how to register unique values? in the spreadsheet google app script web Web App HTML,JS + Google Apps脚本:在GAS中返回后如何返回我的应用程序? - Web App HTML, JS + Google Apps Script: How to go back to my app after return in GAS? Google App Script + Google Sheet + HTML 模态对话框输入表单 - 获取值并构建 email - Google App Script + Google Sheet + HTML modal dialog input form - Get values and build email Google App Script网络应用程序的局限性 - Limitation of Google App Script web app 用户如何从html表单中收集Google应用程序脚本中变量输入值的值? - userHow to collect the values from html form for variable input values in google app script? 根据 Google Apps 脚本中的 URL 参数返回表格数据 / Web App - Return Sheets data based on URL Parameter in Google Apps Script / Web App .gs / .html-使用表单元素,HTML服务和电子表格的Google Apps脚本作为网络应用程序 - .gs/.html - Google Apps Script as a Web App using Form Elements, Html Service and a spreadsheet 为使用 google apps 脚本和 web app form 上传的所有文件创建一个文件夹 - Create one folder for all files uploaded with google apps script and web app form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM