如何将表单数据传递到GAS

问题描述 投票:0回答:1

我正在尝试将数据从表单传递到Google应用程序脚本中,但是当我按Submit时,我会被空白屏幕打招呼。

表格:

<div id="nameDiv">
                        <form action="https://script.google.com/a/umbc.edu/macros/s/AKfycbztum1ImJZeXXYt0fFhwOAMUsB5zCsJQohrum4W7qiH/dev">
                            <label for="fname">First Name</label>
                            <input type="text" id="fname" name="firstname">

                            <label for="lname">Last Name</label>
                            <input type="text" id="lname" name="lastname" >

                            <input type="submit" value="Submit" onclick="google.script.run.nameSearch()">
                        </form>
                    </div>

脚本:

function nameSearch(){
     try{
            var firstName = document.getElementById("fname").value
            var lastName = document.getElementById("lname").value
            var inputSheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1z3j7wxMLsXilyKDIH7XnE7VNQqF66fIH4B-mmuWwCJ8/edit#gid=1235654559");
            var inputData = inputSheet.getDataRange().getValues();
            for (var i = 1; i < inputData.length; i++) {
                if (inputData[i][10] == firstName && inputData[i][11] == lastName) {
                    var result = inputData[i][14] + ": " + inputData[i][15]
                }
            }
            document.getElementById('nameDiv').innerHTML = 
                "<center>Last Name:" + lastName + "</center>" +
                "</br><center>First Name:" + firstName + "</center>"
     }
     catch(e){
                alert(e)
            }
        }

我正在尝试将此数据传递给脚本,以便它可以使用它搜索Google表格,因此我不能仅将脚本作为客户端脚本放在html中。有什么想法吗?

javascript html google-apps-script google-sheets
1个回答
0
投票

尝试一下:

启动对话框,填充文本框,然后单击提交。该视图记录并查看下一个对话框。

function launchADialog() {
  var html='<form><br /><input type="text" name="Name" /> Name: <br /><input type="text" name="Age" /> Age: <br />';
  html+='<select name="Children" ><option value="0">None</option><option value="1">One</option><option value="2">Two</option></select> Children:<br />';
  html+='<input type="button" value="Submit" onClick="google.script.run.processForm(this.parentNode);" /></form>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, "The Form");
}

function processForm(form) {
  Logger.log(JSON.stringify(form));
  var s=Utilities.formatString('<br />Name: %s <br />Age:%s <br />Number Of Children: %s', form.Name, form.Age, form.Children);
  s+='<br /><input type="button" value="Close" onClick="google.script.host.close();" />';
  var userInterface=HtmlService.createHtmlOutput(s);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, "Form Data")
}
© www.soinside.com 2019 - 2024. All rights reserved.