Class FormResponse

FormResponse

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

วิธีการ

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
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()}`);
}

พารามิเตอร์

ชื่อTypeคำอธิบาย
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());
}

พารามิเตอร์

ชื่อTypeคำอธิบาย
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
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);

พารามิเตอร์

ชื่อTypeคำอธิบาย
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();

พารามิเตอร์

ชื่อTypeคำอธิบาย
responseItemResponse

รีเทิร์น

FormResponseFormResponse นี้สำหรับการทำเชน

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

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

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