Class FormResponse

คำตอบของแบบฟอร์ม

คำตอบของแบบฟอร์มโดยรวม FormResponseใช้ได้ 3 วิธี ได้แก่ เข้าถึงคำตอบที่ผู้ตอบส่ง (ดู getItemResponses()) ส่งคำตอบไปยังแบบฟอร์มโดยอัตโนมัติ (ดู withItemResponse(response) และ submit()) และสร้าง URL สำหรับแบบฟอร์มซึ่งจะกรอกข้อมูลในช่องล่วงหน้าโดยใช้คำตอบที่ระบุ FormResponseสร้างหรือเข้าถึงได้จาก 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(),
    );
  }
}

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
getEditResponseUrl()Stringสร้าง URL ที่ใช้แก้ไขคำตอบที่ส่งแล้วได้
getGradableItemResponses()ItemResponse[]รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม
getGradableResponseForItem(item)ItemResponseรับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มสำหรับรายการที่ระบุ
getId()Stringรับรหัสคำตอบของแบบฟอร์ม
getItemResponses()ItemResponse[]รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม
getRespondentEmail()Stringรับอีเมลของผู้ที่ส่งคำตอบ หากเปิดใช้การตั้งค่า Form.setCollectEmail(collect)
getResponseForItem(item)ItemResponseรับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มนี้สำหรับรายการที่ระบุ
getTimestamp()Dateรับการประทับเวลาสำหรับการส่งคำตอบของแบบฟอร์ม
submit()FormResponseส่งคำตอบ
toPrefilledUrl()Stringสร้าง URL สำหรับแบบฟอร์มที่มีการกรอกคำตอบไว้ล่วงหน้าโดยอิงตามคำตอบในคำตอบของแบบฟอร์มนี้
withItemGrade(gradedResponse)FormResponseเพิ่มคะแนนของคำตอบรายการที่ระบุลงในคำตอบของแบบฟอร์ม
withItemResponse(response)FormResponseเพิ่มคำตอบของรายการที่ระบุลงในคำตอบของแบบฟอร์ม

เอกสารโดยละเอียด

getEditResponseUrl()

สร้าง URL ที่ใช้แก้ไขคำตอบที่ส่งแล้วได้ หากปิดใช้การตั้งค่า 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 — URL สำหรับเปลี่ยนคำตอบที่ส่ง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getGradableItemResponses()

รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม วิธีนี้ทำงานคล้ายกับ getItemResponses() แต่เพื่อให้สามารถให้คะแนน คำตอบที่ขาดหายไปได้ ระบบจะยังคงแสดงผล ItemResponse หาก Item ที่เกี่ยวข้อง สามารถให้คะแนนได้ (นั่นคือมีค่าคะแนน) แม้ว่าจะไม่มีคำตอบจริงก็ตาม อย่างไรก็ตาม หาก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[] — อาร์เรย์ของคำตอบสำหรับคำถามทุกข้อในแบบฟอร์มที่ผู้ตอบ อาจได้รับคะแนน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getGradableResponseForItem(item)

รับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มสำหรับรายการที่ระบุ เมธอดนี้ทำงานคล้ายกับ getResponseForItem(item) แต่เพื่อให้ให้คะแนนคำตอบที่ขาดหายไปได้ เมธอดนี้จะยังคง แสดงผล ItemResponse หากสามารถให้คะแนน Item ที่เกี่ยวข้องได้ (นั่นคือมี ค่าคะแนน) แม้ว่าจะไม่มีคำตอบจริงก็ตาม อย่างไรก็ตาม หาก 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()

รับรหัสคำตอบของแบบฟอร์ม เมธอดนี้จะแสดงผล 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 — รหัสคำตอบของแบบฟอร์ม หรือ null หากยังไม่ได้ส่งคำตอบของแบบฟอร์ม

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getItemResponses()

รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม หากคำตอบของแบบฟอร์มไม่มีคำตอบสำหรับ TextItem, DateItem, TimeItem หรือ ParagraphTextItem ที่ระบุ 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[] — อาร์เรย์ของคำตอบสำหรับรายการคำถามทุกรายการในแบบฟอร์มที่ผู้ตอบ ให้คำตอบ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 หากสคริปต์สร้างคำตอบนี้แต่ยังไม่ได้ส่ง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 หากสคริปต์ สร้างคำตอบนี้แต่ยังไม่ได้ส่ง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 — คำตอบที่สร้างใหม่ซึ่งบันทึกไว้ในที่เก็บคำตอบของแบบฟอร์ม

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

toPrefilledUrl()

สร้าง URL สำหรับแบบฟอร์มที่มีการกรอกคำตอบไว้ล่วงหน้าโดยอิงตามคำตอบในคำตอบของแบบฟอร์มนี้

// 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 — URL ของแบบฟอร์มที่มีคำตอบที่กรอกไว้ล่วงหน้า

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

withItemGrade(gradedResponse)

เพิ่มคะแนนของคำตอบรายการที่ระบุลงในคำตอบของแบบฟอร์ม วิธีนี้ใช้ได้กับคำตอบในแบบฟอร์มที่ส่งแล้วเท่านั้น และจะมีผลกับคะแนนที่จัดเก็บไว้เมื่อมีการส่งคำตอบเท่านั้น นอกจากนี้ วิธีนี้จะอัปเดตเฉพาะคะแนนของคำตอบในรายการเท่านั้น โดยจะไม่มีผลกับคำตอบจริง (เนื่องจากมีการส่งคำตอบไปแล้ว) หากเรียกใช้เมธอดนี้หลายครั้งสำหรับรายการเดียวกัน ระบบจะเก็บเฉพาะเกรดล่าสุด หาก ItemResponse ไม่มีคะแนน วิธีนี้จะนำคะแนนของรายการออก

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
gradedResponseItemResponse

รีเทิร์น

FormResponseFormResponse นี้สำหรับการเชื่อมโยง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 — This FormResponse, for chaining.

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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