คําตอบแบบฟอร์มทั้งหมด 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 | คำอธิบาย |
---|---|---|
item | Item |
ขากลับ
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 | คำอธิบาย |
---|---|---|
item | Item |
ขากลับ
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 | คำอธิบาย |
---|---|---|
gradedResponse | ItemResponse |
ขากลับ
FormResponse
— FormResponse
นี้ สําหรับเชน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 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 | คำอธิบาย |
---|---|---|
response | ItemResponse |
ขากลับ
FormResponse
— FormResponse
นี้สําหรับเชน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms