Class ItemResponse
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
项响应
对表单中某个题目的回答。您可以通过 FormResponse
访问项回答,也可以通过要求受访者回答问题的任何 Item
创建项回答。
// 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(),
);
}
}
详细文档
getFeedback()
获取针对受访者提交的回答提供的反馈。
返回
Object
- 题目项的 QuizFeedback
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItem()
获取此回答所对应的问题项。
返回
Item
- 此回答对应的题目项
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponse()
获取受访者提交的回答。对于大多数类型的题目项,此方法会返回 String
。
对于 CheckboxItem
题目,此方法会返回一个 String[]
数组,其中包含回复者的选项。数组中字符串的顺序可能会有所不同。
对于 GridItem
题目,此方法会返回一个 String[]
数组,其中编号为 n
的答案对应于网格中第 n + 1
行的题目。如果受访者未回答网格中的某个问题,系统会将该答案返回为 ''
。
对于 CheckboxGridItem
题目,此方法会返回一个 String[][]
数组,其中行编号 n
对应的答案与复选框网格中行编号 n + 1
对应的题目相对应。如果受访者未回答网格中的某个问题,系统会将该答案返回为 ''
。
返回
Object
- 题目答案的 String
、String[]
或 String[][]
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getScore()
获取受访者提交的回答的得分。
返回
Object
- 一个 Double
,表示题目项的分数
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setFeedback(feedback)
设置应针对受访者提交的答案显示的反馈。
在使用更新后的 FormResponse 调用 Form.submitGrades(responses)
之前,此方法实际上不会在 Google 表单中保存反馈。如需查看示例,请参阅 setScore()
。
参数
返回
ItemResponse
- 用于链接的 ItemResponse
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setScore(score)
为受访者提交的答案设置分数。null 值会清除现有得分。
在使用更新后的 FormResponse 调用 Form.submitGrades(responses)
之前,此方法实际上不会在 Google 表单中保存分数。
// For a multiple choice question with options: "Always true", "Sometimes true",
// and "Never", award half credit for responses that answered "Sometimes true".
const formResponses = FormApp.getActiveForm().getResponses();
// Go through each form response
for (let i = 0; i < formResponses.length; i++) {
const response = formResponses[i];
const items = FormApp.getActiveForm().getItems();
// Assume it's the first item
const item = items[0];
const itemResponse = response.getGradableResponseForItem(item);
// Give half credit for "Sometimes true".
if (itemResponse != null && itemResponse.getResponse() === 'Sometimes true') {
const points = item.asMultipleChoiceItem().getPoints();
itemResponse.setScore(points * 0.5);
// This saves the grade, but does not submit to Forms yet.
response.withItemGrade(itemResponse);
}
}
// Grades are actually submitted to Forms here.
FormApp.getActiveForm().submitGrades(formResponses);
参数
返回
ItemResponse
- 用于链接的 ItemResponse
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eAn \u003ccode\u003eItemResponse\u003c/code\u003e represents an answer to a single question within a Google Form.\u003c/p\u003e\n"],["\u003cp\u003eYou can access individual item responses through a \u003ccode\u003eFormResponse\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eItemResponse\u003c/code\u003e objects provide methods to get and set feedback and scores for answers, as well as retrieve the question itself and the respondent's answer.\u003c/p\u003e\n"],["\u003cp\u003eScores and feedback set using \u003ccode\u003eItemResponse\u003c/code\u003e methods must be saved to the form using \u003ccode\u003eForm.submitGrades()\u003c/code\u003e to take effect.\u003c/p\u003e\n"]]],[],null,["# Class ItemResponse\n\nItemResponse\n\nA response to one question item within a form. Item responses can be accessed from [FormResponse](/apps-script/reference/forms/form-response) and created from any [Item](/apps-script/reference/forms/item) that asks the respondent to answer a question.\n\n```javascript\n// Open a form by ID and log the responses to each question.\nconst form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst formResponses = form.getResponses();\nfor (let i = 0; i \u003c formResponses.length; i++) {\n const formResponse = formResponses[i];\n const itemResponses = formResponse.getItemResponses();\n for (let j = 0; j \u003c itemResponses.length; j++) {\n const itemResponse = itemResponses[j];\n Logger.log(\n 'Response #%s to the question \"%s\" was \"%s\"',\n (i + 1).toString(),\n itemResponse.getItem().getTitle(),\n itemResponse.getResponse(),\n );\n }\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------|-------------------------------------------|-----------------------------------------------------------------------------------|\n| [getFeedback()](#getFeedback()) | `Object` | Gets the feedback that was given for the respondent's submitted answer. |\n| [getItem()](#getItem()) | [Item](/apps-script/reference/forms/item) | Gets the question item that this response answers. |\n| [getResponse()](#getResponse()) | `Object` | Gets the answer that the respondent submitted. |\n| [getScore()](#getScore()) | `Object` | Gets the score for the respondent's submitted answer. |\n| [setFeedback(feedback)](#setFeedback(Object)) | [ItemResponse](#) | Sets the feedback that should be displayed for the respondent's submitted answer. |\n| [setScore(score)](#setScore(Object)) | [ItemResponse](#) | Sets the score for the respondent's submitted answer. |\n\nDetailed documentation\n----------------------\n\n### `get``Feedback()`\n\nGets the feedback that was given for the respondent's submitted answer.\n\n#### Return\n\n\n`Object` --- a `Quiz``Feedback` for the question item\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`\n\n*** ** * ** ***\n\n### `get``Item()`\n\nGets the question item that this response answers.\n\n#### Return\n\n\n[Item](/apps-script/reference/forms/item) --- the question item that this response answers\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`\n\n*** ** * ** ***\n\n### `get``Response()`\n\nGets the answer that the respondent submitted. For most types of question items, this returns a\n`String`.\n\nFor [CheckboxItem](/apps-script/reference/forms/checkbox-item) questions, this returns a `String[]` array containing the\nresponder's choices. The order of the strings in the array may vary.\n\nFor [GridItem](/apps-script/reference/forms/grid-item) questions, this returns a `String[]` array in which the answer at\nindex `n` corresponds to the question at row `n + 1` in the grid. If a respondent\ndid not answer a question in the grid, that answer is returned as `''`.\n\nFor [CheckboxGridItem](/apps-script/reference/forms/checkbox-grid-item) questions, this returns a `String[][]` array in which the\nanswers at row index `n` corresponds to the question at row `n + 1` in the checkbox\ngrid. If a respondent did not answer a question in the grid, that answer is returned as `''`.\n\n#### Return\n\n\n`Object` --- a `String` or `String[]` or `String[][]` of answers to the question\nitem\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`\n\n*** ** * ** ***\n\n### `get``Score()`\n\nGets the score for the respondent's submitted answer.\n\n#### Return\n\n\n`Object` --- a `Double` representing the score for the question item\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`\n\n*** ** * ** ***\n\n### `set``Feedback(feedback)`\n\nSets the feedback that should be displayed for the respondent's submitted answer.\n\nThis method does not actually save the feedback in Forms until [Form.submitGrades(responses)](/apps-script/reference/forms/form#submitGrades(FormResponse)) is called with the updated FormResponses. See `set``Score()` for an example.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|----------|-------------|\n| `feedback` | `Object` | |\n\n#### Return\n\n\n[ItemResponse](#) --- a `Item``Response` for chaining\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`\n\n*** ** * ** ***\n\n### `set``Score(score)`\n\nSets the score for the respondent's submitted answer. A null value will clear the existing\nscore.\n\nThis method does not actually save the score in Forms until [Form.submitGrades(responses)](/apps-script/reference/forms/form#submitGrades(FormResponse)) is called with the updated FormResponses.\n\n```javascript\n// For a multiple choice question with options: \"Always true\", \"Sometimes true\",\n// and \"Never\", award half credit for responses that answered \"Sometimes true\".\nconst formResponses = FormApp.getActiveForm().getResponses();\n// Go through each form response\nfor (let i = 0; i \u003c formResponses.length; i++) {\n const response = formResponses[i];\n const items = FormApp.getActiveForm().getItems();\n // Assume it's the first item\n const item = items[0];\n const itemResponse = response.getGradableResponseForItem(item);\n // Give half credit for \"Sometimes true\".\n if (itemResponse != null && itemResponse.getResponse() === 'Sometimes true') {\n const points = item.asMultipleChoiceItem().getPoints();\n itemResponse.setScore(points * 0.5);\n // This saves the grade, but does not submit to Forms yet.\n response.withItemGrade(itemResponse);\n }\n}\n// Grades are actually submitted to Forms here.\nFormApp.getActiveForm().submitGrades(formResponses);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|-------------|\n| `score` | `Object` | |\n\n#### Return\n\n\n[ItemResponse](#) --- a `Item``Response` for chaining\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/forms.currentonly`\n- `https://www.googleapis.com/auth/forms`"]]