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() 类似,但允许评分 缺少答案,但如果对应的 ItemItemResponse 即使没有实际回复,也可以评分(即具有分值)。但是,如果 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()

按照商品显示顺序获取表单回复中包含的所有商品响应 表单。如果表单响应不包含给定 TextItem 的响应, DateItemTimeItemParagraphTextItemItemResponse 将有一个空字符串作为响应。如果表单响应省略了 响应,则此方法会从返回的数组中排除相应商品。

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