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

รีเทิร์น

FormResponseFormResponse นี้สําหรับการต่อเชื่อม

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

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

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