如何从Google应用脚本中的html表单传递/获取数据?我尝试了很多次,但仍然无法获得正确的代码。我希望有人可以帮我解决这个问题。
code.公司
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getValuesFromForm(form) {
var firstname = form.lastname,
lastname = form.firstname,
ss = SpreadsheetApp.openById('1XvrQFzVTlCqtB6HGggeGKraaLrd_8wdw6rAGVNVxYC0');
var sheet = ss.getSheets()[0];
sheet.appendRow([firstname, lastname]);
}
的index.html
<form>
First name: <input id="firstname" name="firstName" type="text" />
Last name: <input id="lastname" name="lastName" type="text" />
<input onclick="google.script.run.getValuesFromForm(this.form)" type="button" value="Add Row" />
</form>
两件事情 :
name
而不是id
[目前你正在使用id,交叉检查它,请参阅大写]所以你有两个选择:
<form id='myForm'>
First name: <input id="firstname" name="firstname" type="text" /> Last name: <input id="lastname" name="lastname" type="text" />
<input onclick="google.script.run.getValuesFromForm(document.getElementById('myForm'))" type="button" value="Add Row" />
</form>
要么
在form.firstName
的脚本中使用form.lastName
和code.gs
[而不是form.firstname
和form.lastname
]