对整个表单的回复。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