【问题标题】:Post multiple checkbox selections in comma list to Google Sheets将逗号列表中的多个复选框选择发布到 Google 表格
【发布时间】:2020-02-07 22:27:13
【问题描述】:

我正在使用脚本将 HTML 表单中的数据输入到 Google 表格中。有没有办法可以修改(或添加其他内容),以便我的复选框字段将作为列表输入到我的 Google 表格中,以逗号分隔?

因此,使用下面的表格,如果选中所有框,我希望 Google 表格中的“时间”单元格列出上午 9 点、上午 10 点、上午 11 点。现在,它只显示第一个选中的框的值。

表格示例:

<form>
<input type="checkbox" name="time" value="9am">
<label for="time">9:00am</label><br>
<input type="checkbox" name="time" value="10am">
<label for="time">10:00am</label><br>
<input type="checkbox" name="time" value="11am">
<label for="time">11:00am</label><br>
<button type="submit">Submit times</button>
</form>

脚本(来自https://github.com/jamiewilson/form-to-google-sheets):

<script>
  const scriptURL = 'https://script.google.com/macros/s/XXXX/exec'
  const form = document.forms['submit-to-google-sheet']

  form.addEventListener('submit', e => {
    e.preventDefault()
    fetch(scriptURL, { method: 'POST', body: new FormData(form)})
      .then(response => console.log('Success!', response))
      .catch(error => console.error('Error!', error.message))
  })
</script>

【问题讨论】:

  • 我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,我深表歉意。到时候,可以问一下你现在的情况吗?我想学习解决你的问题。

标签: javascript html forms google-apps-script checkbox


【解决方案1】:

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

修改点:

  • 在您的 HTML 中,复选框使用相同的名称。这是time。在这种情况下,您可以在 Google 端通过e.parameters["time"] 检索值。

修改脚本:

请修改your Google Apps Script如下。

从:
return header === 'timestamp' ? new Date() : e.parameter[header]
到:
return header === 'timestamp' ? new Date() : (header == "time" ? e.parameters[header].join(",") : e.parameter[header]);
  • 在这种情况下,如果标题行中不存在time,则不会放置这些值。请注意这一点。

注意:

  • 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。

参考:

很遗憾,我看不到您当前的 Google Apps 脚本和电子表格。因此,如果上述修改没有解决您的问题,我深表歉意。当时,为了正确理解您的情况,您能否提供包含整个脚本的示例电子表格?通过这个,我想检查一下。

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 2016-11-21
    • 2022-11-13
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2017-01-04
    • 2019-11-24
    • 2013-03-31
    相关资源
    最近更新 更多