对整个表单的回答。Form
可用于以下三种用途:访问答题者提交的答案(请参阅 get
)、以编程方式向表单提交回答(请参阅 with
和 submit()
),以及为表单生成一个预先使用提供的答案填充字段的网址。Form
可通过 Form
创建或访问。
// Open a form by ID and log the responses to each question. const form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); const formResponses = form.getResponses(); for (let i = 0; i < formResponses.length; i++) { const formResponse = formResponses[i]; const itemResponses = formResponse.getItemResponses(); for (let j = 0; j < itemResponses.length; j++) { const itemResponse = itemResponses[j]; Logger.log( 'Response #%s to the question "%s" was "%s"', (i + 1).toString(), itemResponse.getItem().getTitle(), itemResponse.getResponse(), ); } }
方法
方法 | 返回类型 | 简介 |
---|---|---|
get | String | 生成一个可用于修改已提交的回答的网址。 |
get | Item | 获取表单响应中包含的所有问题响应,这些响应的顺序与问题在表单中的显示顺序相同。 |
get | Item | 获取表单回答中针对指定题目的回答。 |
get | String | 获取表单回答的 ID。 |
get | Item | 获取表单响应中包含的所有问题响应,这些响应的顺序与问题在表单中的显示顺序相同。 |
get | String | 如果启用了 Form.setCollectEmail(collect) 设置,则获取提交回答的人员的电子邮件地址。 |
get | Item | 获取相应表单回答中包含的指定题目的回答。 |
get | Date | 获取表单回答提交的时间戳。 |
submit() | Form | 提交回答。 |
to | String | 根据此表单回答中的答案,生成一个预填了答案的表单网址。 |
with | Form | 将指定题项回答的得分添加到表单回答中。 |
with | Form | 将指定的问题回答添加到表单回答中。 |
详细文档
get Edit Response Url()
生成一个可用于修改已提交的回答的网址。如果 Form.setAllowResponseEdits(enabled)
设置处于停用状态,该链接会指向一个页面,其中说明了修改表单回复的功能已停用。访问链接的任何人都可以修改回答,但如果启用了
设置,则需要拥有对相应表单的访问权限的账号。如果启用了Form.setRequireLogin(requireLogin)Form.setCollectEmail(collect)
设置,表单会记录修改回答的用户的电子邮件地址,而不是原始回答者的电子邮件地址。
对于脚本已创建但尚未提交的表单回答,此方法会返回 null
。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first form response. const formResponse = form.getResponses()[0]; // Gets the edit URL for the first form response and logs it to the console. const editUrl = formResponse.getEditResponseUrl(); console.log(editUrl);
返回
String
- 用于更改已提交回答的网址。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Gradable Item Responses()
获取表单响应中包含的所有问题响应,这些响应的顺序与问题在表单中的显示顺序相同。此方法的工作方式与 get
类似,但为了能够对缺少的答案进行评分,即使没有实际的回答,如果相应的 Item
可以评分(即具有分值),该方法仍会返回 Item
。不过,如果 Item
不可评分,此方法会从其返回的数组中排除相应商品。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Gets the item responses contained in each form response. for (const formResponse of formResponses) { const gradableItemsResponses = formResponse.getGradableItemResponses(); // Logs the title and score for each item response to the console. for (const gradableItemsResponse of gradableItemsResponses) { console.log(`${gradableItemsResponse.getItem().getTitle()} score ${gradableItemsResponse.getScore()}`); } }
返回
Item
- 针对表单中受访者可获得得分的每个问题项的回答数组。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Gradable Response For Item(item)
获取表单回答中针对指定题目的回答。此方法与 get
类似,但为了允许对缺少的答案进行评分,即使没有实际的回答,如果相应的 Item
可以评分(即具有点值),它仍会返回 Item
。不过,如果 Item
不可分级,此方法会返回 null
。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Gets the item responses contained in a form response. for (const formResponse of formResponses) { const formItemResponses = formResponse.getGradableItemResponses(); // Logs the title and score for responses to the first item of the form. const itemResponse = formResponse.getGradableResponseForItem( formItemResponses[0].getItem(), ); console.log( `${itemResponse.getItem().getTitle()} score ${itemResponse.getScore()}`, ); }
参数
名称 | 类型 | 说明 |
---|---|---|
item | Item |
返回
Item
- 指定商品的响应;如果不存在响应且商品未评分,则为 null
。
get Id()
获取表单回答的 ID。如果表单回答尚未提交,此方法会返回 null
。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the form responses and logs the ID for each form response to // the console. for (const formResponse of formResponses) { console.log(`Response ID: ${formResponse.getId()}`); }
返回
String
- 表单回答的 ID;如果表单回答尚未提交,则为 null
。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Item Responses()
获取表单响应中包含的所有问题响应,这些响应的顺序与问题在表单中的显示顺序相同。如果表单回答不包含针对指定 Text
、Date
、Time
或 Paragraph
的回答,则为相应项返回的 Item
将包含一个空字符串作为回答。如果表单回答省略了任何其他题型的回答,此方法会从其返回的数组中排除相应题目。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the responses to the form. const formResponses = form.getResponses(); // Iterates over the responses. for (const formResponse of formResponses) { // Gets the item responses from each form response. const itemResponses = formResponse.getItemResponses(); // Iterates over the item responses. for (const itemResponse of itemResponses) { // Logs the items' questions and responses to the console. console.log( `Response to the question '${itemResponse.getItem().getTitle()}' was '${itemResponse.getResponse()}'`); } }
返回
Item
- 一个数组,包含对表单中每个问题的回答(如果回答者提供了回答)。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Respondent Email()
获取提交回答的人员的电子邮件地址(如果已启用 Form.setCollectEmail(collect)
设置)。
对于脚本已创建但尚未提交的表单回答,此方法会返回 null
。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs each respondent's email to the console. // To collect respondent emails, ensure that Form.setCollectEmail(collect) is // set to true. for (const formResponse of formResponses) { console.log(`Respondent Email: ${formResponse.getRespondentEmail()}`); }
返回
String
- 提交相应回答的人员的电子邮件地址(如果有),或者如果脚本创建了相应回答但尚未提交,则为 null
。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Response For Item(item)
获取相应表单回答中针对指定问题的回答。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first item on the form. const item = form.getItems()[0]; // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs each response to the first item to the // console. for (const formResponse of formResponses) { const itemResponse = formResponse.getResponseForItem(item); console.log(itemResponse.getResponse()); }
参数
名称 | 类型 | 说明 |
---|---|---|
item | Item |
返回
Item
- 指定商品的响应,如果没有,则为 null
。
get Timestamp()
获取表单回答提交的时间戳。
对于脚本已创建但尚未提交的表单回答,此方法会返回 null
。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs the timestamp of each response to the // console. for (const formResponse of formResponses) { console.log(`Timestamp: ${formResponse.getTimestamp()}`); }
返回
Date
- 提交相应回答的时间戳;如果脚本创建了相应回答但尚未提交,则为 null
。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
submit()
提交回答。如果回答已提交,则抛出脚本异常。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Creates an empty response for the form. const formResponse = form.createResponse(); // Submits an empty response. formResponse.submit();
返回
Form
- 已保存到表单的回答存储区的新创建的回答。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
to Prefilled Url()
根据此表单回答中的答案生成一个预填了答案的表单网址。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first form response. const formResponse = form.getResponses()[0]; // Generates and logs the URL of a pre-filled form response based on the answers // of the first form response. const prefilledUrl = formResponse.toPrefilledUrl(); console.log(prefilledUrl);
返回
String
- 预填了答案的表单的网址。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
with Item Grade(gradedResponse)
将指定题目回答的得分添加到表单回答中。此方法仅适用于已提交的表单回答,并且仅在提交后影响存储的成绩。此方法也仅更新题目回答的得分,不会影响实际回答(因为回答已提交)。如果针对同一商品多次调用此方法,则仅保留最后一次的评分。如果 ItemResponse 不包含任何成绩,此方法会移除相应商品的成绩。
// Programmatically award partial credit for a given response const form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); const formResponses = form.getResponses(); const formItems = form.getItems(); for (const formResponse of formResponses) { for (const item of formItems) { const points = item.asMultipleChoiceItem().getPoints(); const itemResponse = formResponse.getGradableResponseForItem(item); Logger.log('Award half credit for answers containing the word "Kennedy"'); const answer = itemResponse.getResponse(); if (answer?.includes('Kennedy')) { itemResponse.setScore(points / 2); formResponse.withItemGrade(itemResponse); } } } form.submitGrades(formResponses);
参数
名称 | 类型 | 说明 |
---|---|---|
graded | Item |
返回
Form
- 此 Form
,用于链式调用
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
with Item Response(response)
将指定的问题回答添加到表单回答中。此方法仅适用于脚本已创建但尚未提交的表单回答;它无法影响已存储的回答。如果针对同一商品多次调用此方法,则仅保留最后一个商品响应。
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Creates a response for the form. const formResponse = form.createResponse(); // Appends a checkbox item to the form. const item = form.addCheckboxItem(); // Sets the title of the item to 'Which items are ice cream flavors?' item.setTitle('Which items are ice cream flavors?'); // Sets choices for the item. item.setChoices([ item.createChoice('Vanilla'), item.createChoice('Strawberry'), item.createChoice('Brick'), ]); // Creates a response for the item. const response = item.createResponse(['Vanilla', 'Strawberry']); // Adds the item response to the form response. formResponse.withItemResponse(response); // Submits the form response. formResponse.submit();
参数
名称 | 类型 | 说明 |
---|---|---|
response | Item |
返回
Form
- 此 Form
,用于链式调用。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms