如何使用 App 脚本将 HTML 表单提交到 Google 表单?
How to Submit an HTML Form to a Google Forms using App Script?
我有一个连接到 Google Sheet 的 Google 表单来收集答案。在我的表单中,我有一个应该是下拉列表的字段,但我需要下拉列表的内容以适应填写表单的用户。这就是为什么我使用 App Script 创建了一个 HTML 表单,它与 Google 表单完全相同,除了下拉列表(它是 HTML 表单中的下拉列表,根据用户更新使用一些 javascript 代码,以及 Google 表格中的简短文本回答)。
我想了解如何将 HTML 表单的输入值提交到对 Google 表单的响应中。
它必须是我的 google 表格,而不是直接进入我的 google 工作表,因为我需要 google 表格创建的编辑 url 并且我希望我的用户能够访问它。
我确定我有日期选择器、文件上传、复选框等字段...
我该怎么做?
使用FormApp
服务
这是一个简短的例子:
- 我创建了一个包含 2 个“简答”的表单。
- 我想使用 Apps 脚本创建响应,对第一个响应为“Hello”,对第二个响应为“World”。
- 我打开了表单本身的脚本编辑器并使用了这段代码:
function createResponse() {
// Get the form this script is bound to
let form = FormApp.getActiveForm()
// Get the question items from the form
let items = form.getItems()
let item1 = items[0]
let item2 = items[1]
// Create a new form response
let newResponse = form.createResponse();
// Before making an ItemResponse, the items need to be cast as text types
// Not sure why this is necessary, since they are already text types...
// Not doing it results in an error
let item1asText = item1.asTextItem()
let item2asText = item2.asTextItem()
// Creating each item response
let itemResponse1 = item1asText.createResponse("Hello")
let itemResponse2 = item2asText.createResponse("World")
// Adding the item responses to the form response
newResponse.withItemResponse(itemResponse1)
newResponse.withItemResponse(itemResponse2)
// Submitting the form response
newResponse.submit()
}
为您的 HTML 表格解释的流程
- 从您的表单中获取全部
items
。
- 创建表单响应
- 对于每个项目
- 将其转换为适当的类型(例如
asTextItem()
、asMultipleChoiceItem()
等)
- 根据您从 HTML 表单中获得的内容创建一个 item 响应。大多数似乎都将字符串作为参数,但请查看文档以防万一。
- 将项目响应添加到表单响应
- 提交表格。
参考资料
我有一个连接到 Google Sheet 的 Google 表单来收集答案。在我的表单中,我有一个应该是下拉列表的字段,但我需要下拉列表的内容以适应填写表单的用户。这就是为什么我使用 App Script 创建了一个 HTML 表单,它与 Google 表单完全相同,除了下拉列表(它是 HTML 表单中的下拉列表,根据用户更新使用一些 javascript 代码,以及 Google 表格中的简短文本回答)。
我想了解如何将 HTML 表单的输入值提交到对 Google 表单的响应中。
它必须是我的 google 表格,而不是直接进入我的 google 工作表,因为我需要 google 表格创建的编辑 url 并且我希望我的用户能够访问它。 我确定我有日期选择器、文件上传、复选框等字段...
我该怎么做?
使用FormApp
服务
这是一个简短的例子:
- 我创建了一个包含 2 个“简答”的表单。
- 我想使用 Apps 脚本创建响应,对第一个响应为“Hello”,对第二个响应为“World”。
- 我打开了表单本身的脚本编辑器并使用了这段代码:
function createResponse() {
// Get the form this script is bound to
let form = FormApp.getActiveForm()
// Get the question items from the form
let items = form.getItems()
let item1 = items[0]
let item2 = items[1]
// Create a new form response
let newResponse = form.createResponse();
// Before making an ItemResponse, the items need to be cast as text types
// Not sure why this is necessary, since they are already text types...
// Not doing it results in an error
let item1asText = item1.asTextItem()
let item2asText = item2.asTextItem()
// Creating each item response
let itemResponse1 = item1asText.createResponse("Hello")
let itemResponse2 = item2asText.createResponse("World")
// Adding the item responses to the form response
newResponse.withItemResponse(itemResponse1)
newResponse.withItemResponse(itemResponse2)
// Submitting the form response
newResponse.submit()
}
为您的 HTML 表格解释的流程
- 从您的表单中获取全部
items
。 - 创建表单响应
- 对于每个项目
- 将其转换为适当的类型(例如
asTextItem()
、asMultipleChoiceItem()
等) - 根据您从 HTML 表单中获得的内容创建一个 item 响应。大多数似乎都将字符串作为参数,但请查看文档以防万一。
- 将项目响应添加到表单响应
- 将其转换为适当的类型(例如
- 提交表格。