Class FormResponse

Phản hồibiểu mẫu

Câu trả lời cho toàn bộ biểu mẫu. Bạn 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 câu trả lời cho biểu mẫu theo cách 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 vào FormResponse từ 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(),
    );
  }
}

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
getEditResponseUrl()StringTạo một URL có thể dùng để chỉnh sửa câu trả lời đã được gửi.
getGradableItemResponses()ItemResponse[]Lấy tất cả câu trả lời cho các mục có trong một câu trả lời của biểu mẫu, theo cùng thứ tự mà các mục xuất hiện trong biểu mẫu.
getGradableResponseForItem(item)ItemResponseLấy câu trả lời của mục có trong câu trả lời của 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ả câu trả lời cho các mục có trong một câu trả lời của biểu mẫu, theo cùng thứ tự mà 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)ItemResponseLấy câu trả lời của mục có trong câu trả lời của biểu mẫu này cho một mục nhất định.
getTimestamp()DateLấy dấu thời gian của một câu trả lời trong biểu mẫu.
submit()FormResponseGửi câu trả lời.
toPrefilledUrl()StringTạo một URL cho biểu mẫu mà các câu trả lời được điền sẵn dựa trên các câu trả lời trong phản hồi biểu mẫu này.
withItemGrade(gradedResponse)FormResponseThêm điểm của câu trả lời cho mục đã cho vào một câu trả lời của biểu mẫu.
withItemResponse(response)FormResponseThêm câu trả lời của mục đã cho vào câu trả lời của biểu mẫu.

Tài liệu chi tiết

getEditResponseUrl()

Tạo một URL có thể dùng để chỉnh sửa câu trả lời đã được gửi. Nếu chế độ cài đặt Form.setAllowResponseEdits(enabled) bị tắt, thì đường liên kết sẽ dẫn đến một trang giải thích rằng bạn không thể chỉnh sửa câu trả lời trong biểu mẫu. Bất kỳ ai truy cập vào đường liên kết đều có thể chỉnh sửa câu trả lời, mặc dù họ cần có tài khoản có quyền truy cập vào biểu mẫu nếu chế độ cài đặt Form.setRequireLogin(requireLogin) được bật. 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 câu trả lời trong biểu mẫu mà tập lệnh đã tạo nhưng chưa 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 câu trả lời đã gửi.

Ủy quyền

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

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

getGradableItemResponses()

Lấy tất cả câu trả lời cho các mục có trong một câu trả lời của biểu mẫu, theo cùng thứ tự mà 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 Item tương ứng có thể được chấm điểm (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 phân loại được, 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 mảng các câu trả lời cho mọi câu hỏi trong biểu mẫu mà người trả lời có thể nhận được điểm.

Ủy quyền

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

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

getGradableResponseForItem(item)

Lấy câu trả lời của mục có trong câu trả lời của 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 câu trả lời bị thiếu, phương thức này vẫn trả về ItemResponse nếu Item tương ứng có thể được chấm điểm (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 chấm điểm được, 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()}`,
  );
}

Thông số

TênLoạiMô 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ó phản hồi nào 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 câu trả lời biểu mẫu chưa đượ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');

// 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ã nhận dạng của câu trả lời trong biểu mẫu hoặc null nếu câu trả lời trong biểu mẫu chưa được gửi.

Ủy quyền

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

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

getItemResponses()

Lấy tất cả câu trả lời cho các mục có trong một câu trả lời của biểu mẫu, theo cùng thứ tự mà các mục xuất hiện trong biểu mẫu. Nếu câu trả lời trong biểu mẫu không chứa câu trả lờ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 làm câu trả lời. Nếu phản hồi biểu mẫu bỏ qua một phản hồi cho bất kỳ loại mục nào khác, 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ác 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 đã cung cấp câu trả lời.

Ủy quyền

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

  • 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 câu trả lời trong biểu mẫu mà tập lệnh đã tạo nhưng chưa 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

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

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

getResponseForItem(item)

Lấy câu trả lời của mục có trong câu trả lời của 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());
}

Thông số

TênLoạiMô 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ó phản hồi nào.


getTimestamp()

Lấy dấu thời gian của một câu trả lời trong biểu mẫu.

Đối với một câu trả lời trong biểu mẫu mà tập lệnh đã tạo nhưng chưa 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 cho biết thời điể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

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

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

submit()

Gửi câu trả lời. Đưa ra một ngoại lệ về tập lệnh nếu câu trả lờ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 – Một câu trả lời mới được tạo và lưu vào kho câu trả lời của biểu mẫu.

Ủy quyền

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

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

toPrefilledUrl()

Tạo một URL cho biểu mẫu mà các câu trả lời được điền sẵn dựa trên các câu trả lời trong phản hồi 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 của một biểu mẫu có các câu trả lời được điền sẵn.

Ủy quyền

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

  • 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 cho mục đã cho vào một câu trả lời của 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 số đã lưu sau khi được gửi. Phương thức này cũng chỉ cập nhật điểm số của câu trả lời cho câu hỏi; phương thức này 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ỉ điểm số sau cùng được giữ lại. Nếu ItemResponse không chứa điểm nào, thì phương thức này sẽ xoá điểm cho mục đó.

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

Thông số

TênLoạiMô tả
gradedResponseItemResponse

Cầu thủ trả bóng

FormResponseFormResponse này, để xâu chuỗi

Ủy quyền

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

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

withItemResponse(response)

Thêm câu trả lời của mục đã cho vào câu trả lời của 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 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 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();

Thông số

TênLoạiMô tả
responseItemResponse

Cầu thủ trả bóng

FormResponseFormResponse này, để xâu chuỗi.

Ủy quyền

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

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