Class FormResponse

FormResponse

对表单的整体回复。FormResponse 可以通过三种方式使用:访问受访者提交的回答(请参阅 getItemResponses())、以编程方式提交对表单的回复(请参阅 withItemResponse(response)submit()),以及为使用提供的回答预填充字段的表单生成网址。您可以通过 Form 创建或访问 FormResponse

// Open a form by ID and log the responses to each question.
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
  var formResponse = formResponses[i];
  var itemResponses = formResponse.getItemResponses();
  for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
    Logger.log('Response #%s to the question "%s" was "%s"',
        (i + 1).toString(),
        itemResponse.getItem().getTitle(),
        itemResponse.getResponse());
  }
}

方法

方法返回类型简介
getEditResponseUrl()String生成一个网址,可用于修改已提交的回复。
getGradableItemResponses()ItemResponse[]获取表单响应中包含的所有项响应,响应顺序与项在表单中显示的顺序相同。
getGradableResponseForItem(item)ItemResponse获取给定项目的表单响应中包含的项目响应。
getId()String获取表单回复的 ID。
getItemResponses()ItemResponse[]获取表单响应中包含的所有项响应,响应顺序与项在表单中显示的顺序相同。
getRespondentEmail()String如果启用了 Form.setCollectEmail(collect) 设置,则获取提交回复的用户的电子邮件地址。
getResponseForItem(item)ItemResponse获取此表单响应中包含的针对给定项的项响应。
getTimestamp()Date获取表单回复提交的时间戳。
submit()FormResponse提交响应。
toPrefilledUrl()String根据此表单回复中的答案,为表单生成一个网址,在该表单中预填充答案。
withItemGrade(gradedResponse)FormResponse将指定内容回复的成绩添加到表单回复中。
withItemResponse(response)FormResponse将指定项响应添加到表单回复。

详细文档

getEditResponseUrl()

生成一个网址,可用于修改已提交的回复。如果 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

getGradableItemResponses()

获取表单响应中包含的所有项响应,响应顺序与项在表单中显示的顺序相同。此方法的工作方式与 getItemResponses() 类似,但为了允许对缺失的答案进行评分,如果相应的 Item 可以评分(即具有分值),它仍会返回 ItemResponse,即使没有实际回复也是如此。不过,如果 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()}`);
  }
}

弃踢回攻

ItemResponse[] - 一个对表单内每个问题项的回复数组,回答者可以根据此获得分数。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

getGradableResponseForItem(item)

获取给定项目的表单响应中包含的项目响应。此方法的工作方式与 getResponseForItem(item) 类似,但为了允许对缺失的答案进行评分,如果相应的 Item 可以评分(即具有分值),它仍会返回 ItemResponse,即使没有实际回复也是如此。不过,如果 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()}`);
}

参数

名称类型说明
itemItem

弃踢回攻

ItemResponse - 给定项的回复,如果不存在且未评分,则为 null


getId()

获取表单回复的 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

getItemResponses()

获取表单响应中包含的所有项响应,响应顺序与项在表单中显示的顺序相同。如果表单响应不包含对给定 TextItemDateItemTimeItemParagraphTextItem 的响应,那么针对该项返回的 ItemResponse 的响应将使用空字符串作为响应。如果表单响应省略了对任何其他项目类型的响应,则此方法会将该项目从返回的数组中排除。

// 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()}'`);
  }
}

弃踢回攻

ItemResponse[] - 一个由回复者提供答案的表单中每个问题项的回复数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

getRespondentEmail()

如果启用了 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

getResponseForItem(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());
}

参数

名称类型说明
itemItem

弃踢回攻

ItemResponse - 给定项的响应,如果不存在,则为 null


getTimestamp()

获取表单回复提交的时间戳。

对于脚本已创建但尚未提交的表单响应,此方法会返回 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();

弃踢回攻

FormResponse - 新创建的回复,会保存到表单的响应存储区。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

toPrefilledUrl()

根据此表单回复中的答案,为表单生成网址,其中系统会根据表单预先填充答案。

// 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

withItemGrade(gradedResponse)

将指定内容回复的成绩添加到表单回复中。此方法仅适用于已提交的表单回复,并且仅在提交后影响存储的成绩。此方法也只会更新推荐项响应的成绩,不会影响实际响应(因为已提交响应)。如果针对同一项内容多次调用此方法,将仅保留最近一次成绩。如果 ItemResponse 不包含任何成绩,则此方法会移除相应项目的成绩。

// Programmatically award partial credit for a given response
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
var formResponses = form.getResponses();
var formItems = form.getItems();
for (var i = 0; i < formResponses.length; i++) {
  var formResponse = formResponses[i];
  for (var j = 0; j < formItems.length; j++) {
    var item = formItems[j];
    var points = item.asMultipleChoiceItem().getPoints();
    var itemResponse = formResponse.getGradableResponseForItem(item);
    Logger.log('Award half credit for answers containing the word "Kennedy"');
    var answer = itemResponse.getResponse();
    if (answer != null && answer.includes('Kennedy')) {
      itemResponse.setScore(points / 2);
      formResponse.withItemGrade(itemResponse);
    }
  }
}
form.submitGrades(formResponses);

参数

名称类型说明
gradedResponseItemResponse

弃踢回攻

FormResponse - 此 FormResponse,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

withItemResponse(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();

参数

名称类型说明
responseItemResponse

弃踢回攻

FormResponse - 此 FormResponse,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms