[英]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.