คำตอบของแบบฟอร์มโดยรวม Form
ใช้ได้ 3 วิธี ได้แก่ เข้าถึงคำตอบที่ผู้ตอบส่ง (ดู get
) ส่งคำตอบไปยังแบบฟอร์มโดยอัตโนมัติ (ดู with
และ submit()
) และสร้าง URL สำหรับแบบฟอร์มซึ่งจะกรอกข้อมูลในช่องล่วงหน้าโดยใช้คำตอบที่ระบุ Form
สร้างหรือเข้าถึงได้จาก 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(), ); } }
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
---|---|---|
get | String | สร้าง URL ที่ใช้แก้ไขคำตอบที่ส่งแล้วได้ |
get | Item | รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม |
get | Item | รับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มสำหรับรายการที่ระบุ |
get | String | รับรหัสคำตอบของแบบฟอร์ม |
get | Item | รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ ในแบบฟอร์ม |
get | String | รับอีเมลของผู้ที่ส่งคำตอบ หากเปิดใช้การตั้งค่า Form.setCollectEmail(collect) |
get | Item | รับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มนี้สำหรับรายการที่ระบุ |
get | Date | รับการประทับเวลาสำหรับการส่งคำตอบของแบบฟอร์ม |
submit() | Form | ส่งคำตอบ |
to | String | สร้าง URL สำหรับแบบฟอร์มที่มีการกรอกคำตอบไว้ล่วงหน้าโดยอิงตามคำตอบในคำตอบของแบบฟอร์มนี้ |
with | Form | เพิ่มคะแนนของคำตอบรายการที่ระบุลงในคำตอบของแบบฟอร์ม |
with | Form | เพิ่มคำตอบของรายการที่ระบุลงในคำตอบของแบบฟอร์ม |
เอกสารโดยละเอียด
get Edit Response Url()
สร้าง 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
get Gradable Item Responses()
รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ
ในแบบฟอร์ม วิธีนี้ทำงานคล้ายกับ get
แต่เพื่อให้สามารถให้คะแนน
คำตอบที่ขาดหายไปได้ ระบบจะยังคงแสดงผล Item
หาก 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()}`); } }
รีเทิร์น
Item
— อาร์เรย์ของคำตอบสำหรับคำถามทุกข้อในแบบฟอร์มที่ผู้ตอบ
อาจได้รับคะแนน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Gradable Response For Item(item)
รับคำตอบของรายการที่มีอยู่ในคำตอบของแบบฟอร์มสำหรับรายการที่ระบุ เมธอดนี้ทำงานคล้ายกับ get
แต่เพื่อให้ให้คะแนนคำตอบที่ขาดหายไปได้ เมธอดนี้จะยังคง
แสดงผล Item
หากสามารถให้คะแนน 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()}`, ); }
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
item | Item |
รีเทิร์น
Item
— คำตอบสำหรับรายการที่ระบุ หรือ null
หากไม่มีคำตอบและรายการยังไม่ได้รับการให้คะแนน
get Id()
รับรหัสคำตอบของแบบฟอร์ม เมธอดนี้จะแสดงผล 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
get Item Responses()
รับคำตอบของรายการทั้งหมดที่อยู่ในคำตอบของแบบฟอร์มตามลำดับเดียวกันกับที่รายการปรากฏ
ในแบบฟอร์ม หากคำตอบของแบบฟอร์มไม่มีคำตอบสำหรับ Text
,
Date
, Time
หรือ Paragraph
ที่ระบุ 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 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()}'`); } }
รีเทิร์น
Item
— อาร์เรย์ของคำตอบสำหรับรายการคำถามทุกรายการในแบบฟอร์มที่ผู้ตอบ
ให้คำตอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Respondent Email()
รับอีเมลของผู้ที่ส่งคำตอบ หากเปิดใช้การตั้งค่า 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
get Response For 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 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()); }
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
item | Item |
รีเทิร์น
Item
— การตอบกลับสำหรับรายการที่ระบุ หรือ null
หากไม่มี
get Timestamp()
รับการประทับเวลาสำหรับการส่งคำตอบของแบบฟอร์ม
สำหรับคำตอบของแบบฟอร์มที่สคริปต์สร้างขึ้นแต่ยังไม่ได้ส่ง วิธีนี้จะแสดงผล
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();
รีเทิร์น
Form
— คำตอบที่สร้างใหม่ซึ่งบันทึกไว้ในที่เก็บคำตอบของแบบฟอร์ม
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
to Prefilled Url()
สร้าง 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
with Item Grade(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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
graded | Item |
รีเทิร์น
Form
— Form
นี้สำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
with Item Response(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();
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
response | Item |
รีเทิร์น
Form
— This Form
, for chaining.
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms