Class FormResponse

FormResponse

Phản hồi cho toàn bộ biểu mẫu. Có thể sử dụng FormResponse theo 3 cách: truy cập vào câu trả lời do người trả lời gửi (xem getItemResponses()), gửi phản hồi cho biểu mẫu theo phương thức lập trình (xem withItemResponse(response)submit()) và tạo URL cho biểu mẫu điền sẵn các trường bằng câu trả lời được cung cấp. Bạn có thể tạo hoặc truy cập FormResponse từ Form.

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

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
getEditResponseUrl()StringTạo URL có thể dùng để chỉnh sửa câu trả lời đã gửi.
getGradableItemResponses()ItemResponse[]Lấy tất cả phản hồi mục có trong một phản hồi biểu mẫu, theo thứ tự các mục xuất hiện trong biểu mẫu.
getGradableResponseForItem(item)ItemResponseNhận phản hồi về mục có trong phản hồi biểu mẫu cho một mục nhất định.
getId()StringLấy mã nhận dạng của câu trả lời trong biểu mẫu.
getItemResponses()ItemResponse[]Lấy tất cả phản hồi mục có trong một phản hồi biểu mẫu, theo thứ tự các mục xuất hiện trong biểu mẫu.
getRespondentEmail()StringLấy địa chỉ email của người đã gửi câu trả lời, nếu bạn bật chế độ cài đặt Form.setCollectEmail(collect).
getResponseForItem(item)ItemResponseNhận phản hồi về mục có trong phản hồi biểu mẫu này cho một mục nhất định.
getTimestamp()DateLấy dấu thời gian của lượt gửi phản hồi trong biểu mẫu.
submit()FormResponseGửi phản hồi.
toPrefilledUrl()StringTạo URL cho biểu mẫu mà trong đó câu trả lời sẽ được điền sẵn dựa trên các câu trả lời trong phản hồi của biểu mẫu này.
withItemGrade(gradedResponse)FormResponseThêm điểm của câu trả lời mục cho trước vào câu trả lời biểu mẫu.
withItemResponse(response)FormResponseThêm phản hồi mục đã cho vào phản hồi biểu mẫu.

Tài liệu chi tiết

getEditResponseUrl()

Tạo URL có thể dùng để chỉnh sửa câu trả lời đã gửi. Nếu chế độ cài đặt Form.setAllowResponseEdits(enabled) bị tắt, đường liên kết này sẽ dẫn đến một trang giải thích rằng tính năng chỉnh sửa phản hồi trong biểu mẫu đã bị tắt. Bất kỳ ai truy cập vào đường liên kết đều có thể chỉnh sửa phản hồi, mặc dù họ cần tài khoản có quyền truy cập vào biểu mẫu nếu bạn bật chế độ cài đặt Form.setRequireLogin(requireLogin). Nếu bạn bật chế độ cài đặt Form.setCollectEmail(collect), biểu mẫu sẽ ghi lại địa chỉ email của người dùng đã chỉnh sửa câu trả lời thay vì địa chỉ email của người trả lời ban đầu.

Đối với một phản hồi biểu mẫu mà tập lệnh đã tạo nhưng chưa được gửi, phương thức này sẽ trả về 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);

Cầu thủ trả bóng

String – URL để thay đổi một câu trả lời đã gửi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getGradableItemResponses()

Lấy tất cả phản hồi mục có trong một phản hồi biểu mẫu, theo thứ tự các mục xuất hiện trong biểu mẫu. Phương thức này hoạt động tương tự như getItemResponses(), nhưng để cho phép chấm điểm câu trả lời bị thiếu, phương thức này vẫn trả về ItemResponse nếu có thể chấm điểm Item tương ứng (tức là có giá trị điểm), ngay cả khi không có câu trả lời thực tế. Tuy nhiên, nếu Item không thể chuyển đổi, thì phương thức này sẽ loại trừ mục đó khỏi mảng được trả về.

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

Cầu thủ trả bóng

ItemResponse[] – Một loạt câu trả lời cho mọi mục câu hỏi trong biểu mẫu mà người trả lời có thể nhận được điểm số.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getGradableResponseForItem(item)

Nhận phản hồi về mục có trong phản hồi biểu mẫu cho một mục nhất định. Phương thức này hoạt động tương tự như getResponseForItem(item), nhưng để cho phép chấm điểm một câu trả lời bị thiếu, phương thức này vẫn trả về ItemResponse nếu có thể chấm điểm Item tương ứng (tức là có một giá trị điểm), ngay cả khi không có câu trả lời thực tế. Tuy nhiên, nếu không thể nâng cấp Item, thì phương thức này sẽ trả về 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()}`);
}

Các tham số

TênLoạiNội dung mô tả
itemItem

Cầu thủ trả bóng

ItemResponse – Phản hồi của một mục nhất định, hoặc null nếu không có mục nào tồn tại và mục đó chưa được chấm điểm.


getId()

Lấy mã nhận dạng của câu trả lời trong biểu mẫu. Phương thức này trả về null nếu bạn chưa gửi phản hồi biểu mẫu.

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

Cầu thủ trả bóng

String – Mã của phản hồi qua biểu mẫu, hoặc null nếu phản hồi qua biểu mẫu chưa được gửi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getItemResponses()

Lấy tất cả phản hồi mục có trong một phản hồi biểu mẫu, theo thứ tự các mục xuất hiện trong biểu mẫu. Nếu phản hồi biểu mẫu không chứa phản hồi cho một TextItem, DateItem, TimeItem hoặc ParagraphTextItem nhất định, thì ItemResponse được trả về cho mục đó sẽ có một chuỗi trống dưới dạng phản hồi. Nếu phản hồi biểu mẫu bỏ qua phản hồi cho mọi loại mục khác, thì phương thức này sẽ loại trừ mục đó khỏi mảng được trả về.

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

Cầu thủ trả bóng

ItemResponse[] – Một mảng câu trả lời cho mọi mục câu hỏi trong biểu mẫu mà người trả lời đưa ra câu trả lời.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getRespondentEmail()

Lấy địa chỉ email của người đã gửi câu trả lời, nếu bạn bật chế độ cài đặt Form.setCollectEmail(collect).

Đối với một phản hồi biểu mẫu mà tập lệnh đã tạo nhưng chưa được gửi, phương thức này sẽ trả về 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()}`);
}

Cầu thủ trả bóng

String – Địa chỉ email của người gửi câu trả lời này, nếu có hoặc null nếu tập lệnh đã tạo câu trả lời này nhưng chưa gửi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getResponseForItem(item)

Nhận phản hồi về mục có trong phản hồi biểu mẫu này cho một mục nhất định.

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

Các tham số

TênLoạiNội dung mô tả
itemItem

Cầu thủ trả bóng

ItemResponse – Phản hồi cho một mục nhất định hoặc null nếu không có mục nào.


getTimestamp()

Lấy dấu thời gian của lượt gửi phản hồi trong biểu mẫu.

Đối với một phản hồi biểu mẫu mà tập lệnh đã tạo nhưng chưa được gửi, phương thức này sẽ trả về 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()}`);
}

Cầu thủ trả bóng

Date – Dấu thời gian mà phản hồi này được gửi, hoặc null nếu tập lệnh đã tạo phản hồi này nhưng chưa gửi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

submit()

Gửi phản hồi. Gửi một ngoại lệ cho tập lệnh nếu phản hồi đã được gửi.

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

Cầu thủ trả bóng

FormResponse – Câu trả lời mới tạo được lưu vào kho phản hồi của biểu mẫu.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

toPrefilledUrl()

Tạo URL cho biểu mẫu mà trong đó câu trả lời sẽ được điền sẵn dựa trên các câu trả lời trong phản hồi của biểu mẫu này.

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

Cầu thủ trả bóng

String – URL cho một biểu mẫu có câu trả lời được điền sẵn.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

withItemGrade(gradedResponse)

Thêm điểm của câu trả lời mục cho trước vào câu trả lời biểu mẫu. Phương thức này chỉ áp dụng cho các câu trả lời trong biểu mẫu đã được gửi và chỉ ảnh hưởng đến điểm đã lưu trữ sau khi bạn gửi các câu trả lời đó. Phương thức này cũng chỉ cập nhật điểm của câu trả lời mục chứ không ảnh hưởng đến câu trả lời thực tế (vì câu trả lời đã được gửi). Nếu phương thức này được gọi nhiều lần cho cùng một mục, thì chỉ có điểm cuối cùng được giữ lại. Nếu ItemResponse không chứa điểm, phương thức này sẽ xoá điểm của mục đó.

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

Các tham số

TênLoạiNội dung mô tả
gradedResponseItemResponse

Cầu thủ trả bóng

FormResponseFormResponse này, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

withItemResponse(response)

Thêm phản hồi mục đã cho vào phản hồi biểu mẫu. Phương thức này chỉ áp dụng cho các câu trả lời trong biểu mẫu mà tập lệnh đã tạo nhưng chưa được gửi. Phương thức này không thể ảnh hưởng đến các câu trả lời đã lưu trữ. Nếu phương thức này được gọi nhiều lần cho cùng một mục, thì chỉ phản hồi của mục cuối cùng được giữ lại.

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

Các tham số

TênLoạiNội dung mô tả
responseItemResponse

Cầu thủ trả bóng

FormResponseFormResponse này, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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