简体   繁体   English

使用Google Apps脚本解析表单提交

[英]Parse Form Submission with Google Apps Script

I have a Google Spreadsheet that loads a sidebar with an HTML form. 我有一个Google电子表格,可使用HTML表单加载侧边栏。 The code for that form is as follows: 该表格的代码如下:

<script>
function handleFormSubmit(formObject) {
    google.script.run.processForm(formObject);

}
</script>
<form id="myForm" onsubmit="handleFormSubmit(this)">
  PO#: <input type="text" name="po" required><br/>
  Invoice#: <input type="text" name="invoice" required><br/>
  <input type="submit" value="Submit" />
</form>
<div id="output"></div>

It works with the following script: 它与以下脚本一起使用:

function processForm(form) {  
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet5');
  var lr = sheet.getLastRow();
  var range = sheet.getRange(lr+1,1,1,1);
  range.setValue(form);
  Logger.log(form);
}

Currently I am getting {invoice=####, po=####} as a result. 目前,我得到的结果是{invoice=####, po=####} How can I parse (if that's even the right term) the form submission so that each response can go to a different cell? 我如何解析(如果那是正确的术语)表单提交,以便每个响应都可以转到不同的单元格? Is there any way to turn this into an array? 有什么办法可以将其转换为数组吗?

Thanks 谢谢

Not exactly sure what you want to do here, but I don't think you can transfer a dom object to Google Apps Script. 不确定您要在这里做什么,但我认为您无法将dom对象传输到Google Apps脚本。 You could use this: 您可以使用此:

<script>
function handleFormSubmit(formObject) {
    google.script.run.processForm({
        po: formObject.getElementById("po").value,
        invoice: formObject.getElementById("invoice").value
    });

}
</script>
<form id="myForm" onsubmit="handleFormSubmit(this)">
  PO#: <input type="text" name="po" id="po" required><br/>
  Invoice#: <input type="text" name="invoice" id="invoice" required><br/>
  <input type="submit" value="Submit" />
</form>
<div id="output"></div>

Then in your Google Apps Script, to get the values of the inputs, use 然后在您的Google Apps脚本中,要获取输入值,请使用

form.po

and

form.invoice

Using google.script.run.processForm() to pass back the form to the server. 使用google.script.run.processForm()将表单传递回服务器。 Instead of using get last row, and adding +1, just simply use appendRow() function. 无需使用get最后一行并添加+1,只需简单地使用appendRow()函数即可。

    function processForm(form) {  
       var ss = SpreadsheetApp.getActive();
       var sheet = ss.getSheetByName('Sheet5');
       sheet.appendRow(form) //accepts an array [val1,val2,val3]
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本:已提交的邮件内容(表单提交脚本) - Google Apps Script: Message Content As Is submitted (Form Submission Script) Google Apps脚本未上传文件:NetworkError:提交表单失败 - Google Apps Script Not Uploading File: NetworkError: Form Submission Failed 我的“包含”如何在独立的Google Apps脚本中破坏表单提交? - How are my “Includes” breaking the form submission in a Standalone Google Apps Script? 谷歌应用程序脚本。 表单提交随机失败 - Google apps script. Form submission fails randomly Google Apps脚本:通过Google表单提交在Google云端硬盘中创建文件夹 - Google Apps Script: Creating a folder in Google Drive from a Google Form submission Google Apps 脚本:从 Google 表单提交创建 Google 共享驱动器文件夹 - Google Apps Script: Create Google Shared Drive Folder from a Google Form submission 如何使提交功能在使用 Google Apps 脚本创建并嵌入 Google 站点的 Web 表单中工作? - How to make submission function work in a web form that is created using Google Apps Script and embedded in a Google Site? 在谷歌应用程序中获取最新表单提交的 responseID - getting responseID of latest form submission in google apps 在 Google Apps 脚本中,如何通过重新加载 doPost() 返回的 HTML 来防止多次提交表单数据? - In Google Apps Script, How to prevent multiple submission of form data by reloading the HTML returned by doPost()? 如何将值从HTML表单提交传递给Google表格,然后再传递回Google Apps Script Web App中的HTML - How do I pass a value from an HTML form submission to a Google Sheet and back to HTML in a Google Apps Script Web App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM