如何使用Google App Script从html表单中获取数据

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

如何从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>

enter image description here

google-apps-script
1个回答
1
投票

两件事情 :

  1. 我们必须通过父母,以便我们可以访问它的孩子。 this.form将不发送任何内容[我猜]因为这个[输入按钮]没有表单子项。相反,你应该发送表格[使用parentNode或getElement()]
  2. 我们必须使用字段name而不是id [目前你正在使用id,交叉检查它,请参阅大写]

所以你有两个选择:

  1. 更改字段名称以匹配脚本中的字段名称

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

要么

  1. 更改脚本中使用的名称以匹配表单中的名称

form.firstName的脚本中使用form.lastNamecode.gs [而不是form.firstnameform.lastname]

© www.soinside.com 2019 - 2024. All rights reserved.