代码之家  ›  专栏  ›  技术社区  ›  KPM

将web表单值返回到Google应用程序脚本

  •  2
  • KPM  · 技术社区  · 9 年前

    这是我要开始工作的谷歌工作表脚本。函数checkLogin会被调用,直到我尝试从网页向它传递值。

    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); 
    }
    

    <!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>
    

    我希望有人能给我指出正确的方向。谢谢

    1 回复  |  直到 9 年前
        1
  •  2
  •   Alan Wells    9 年前

    this.parentNode

    onclick="google.script.run.withSuccessHandler(showMsgForLoginAttempt)
      .checkLogin(this.parentNode)" />
    

    添加 <script> 标记到 index.html 带有成功处理程序的文件。添加 withSuccessHandler() google.script.run 服务器调用。

    索引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>
    

    服务器代码:

    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;
      }
    }