หน้านี้จะอธิบายวิธีทํางานต่อไปนี้ที่เกี่ยวข้องกับแบบฟอร์ม
- เผยแพร่แบบฟอร์มเพื่อให้ผู้ตอบเข้าถึงได้
- ค้นหาผู้ตอบแบบฟอร์ม
- แชร์แบบฟอร์มกับผู้ตอบจำนวนมากขึ้น
- นำผู้ตอบออกจากแบบฟอร์ม
- ตรวจสอบว่าแบบฟอร์มรับคำตอบจาก "ทุกคนที่มีลิงก์" หรือไม่
- ปิดแบบฟอร์ม
- เลิกเผยแพร่แบบฟอร์ม
- หยุดรับคำตอบของแบบฟอร์ม
- ตรวจสอบว่าแบบฟอร์มเป็นแบบฟอร์มเดิมหรือไม่
ก่อนเริ่มต้น
ทํางานต่อไปนี้ก่อนดําเนินการต่อในหน้านี้
รับรหัสแบบฟอร์ม ระบบจะแสดงรหัสแบบฟอร์มในช่อง
formId
ของคําตอบเมื่อคุณสร้างแบบฟอร์มโดยใช้forms.create
เผยแพร่แบบฟอร์มเพื่อให้ผู้ตอบเข้าถึงได้
คุณสามารถเผยแพร่แบบฟอร์มที่มีอยู่ด้วยวิธี forms.setPublishSettings
เรียกใช้เมธอด
forms.setPublishSettings
ที่มีรหัสแบบฟอร์ม
REST
ตัวอย่างเนื้อหาของคำขอ
{
"publishSettings": {
"isPublished": true,
"isAcceptingResponses": true
}
}
Apps Script
/**
* Publishes a Google Form using its URL.
*/
function publishMyForm() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Publish the form. This also enables accepting responses.
form.setPublished(true);
Logger.log(`Form "${form.getTitle()}" published successfully.`);
// Optional: Verify the state
if (form.isPublished()) {
Logger.log('Form is now published.');
}
if (form.isAcceptingResponses()) {
Logger.log('Form is now accepting responses.')
}
} catch (error) {
Logger.log(`Error publishing form: ${error}`);
}
}
Python
Node.js
ค้นหาผู้ตอบแบบฟอร์ม
คุณสามารถเรียกข้อมูลรายชื่อผู้ใช้ทั้งหมดที่มีสิทธิ์เข้าถึงผู้ตอบ (บทบาท PUBLISHED_READER) โดยใช้ Form.getPublishedReaders() ซึ่งจะแสดงผลอาร์เรย์ของออบเจ็กต์ผู้ใช้
REST
เพิ่มพารามิเตอร์การค้นหา includePermissionsForView=published
ต่อท้าย URL คำขอ
Apps Script
/**
* Gets and logs the email addresses of all responders for a form.
*/
function listResponders() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Get the array of User objects representing responders
const responders = form.getPublishedReaders();
// Log the responders
Logger.log("Following can respond to the form");
responders.forEach(responder => Logger.log(responder.getEmail()));
return responders;
} catch (error) {
Logger.log(`Error getting responders: ${error}`);
}
}
Python
Node.js
แชร์แบบฟอร์มกับผู้ตอบจำนวนมากขึ้น
หากต้องการเพิ่มผู้ตอบลงในแบบฟอร์มเพื่อให้ผู้ตอบเปิดและตอบแบบฟอร์มได้ ให้ใช้วิธีการpermissions.create
ในไดรฟ์
เรียกใช้เมธอด
permissions.create
ด้วยรหัสแบบฟอร์มและการตั้งค่าการเข้าถึง
REST
ตัวอย่างเนื้อหาของคำขอ
{
"view": "published",
"role": "reader",
"type": "user",
"emailAddress": "user@example.com"
}
Apps Script
/**
* Adds a single responder to a form using their email address.
*/
function `addSingleResponderByEmail()` {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
// Replace with the responder's email address
const responderEmail = 'responder@example.com';
try {
const form = FormApp.openByUrl(formUrl);
// Add the user as a responder
form.addPublishedReader(responderEmail);
Logger.log(`Added ${responderEmail} as a responder to form "${
form.getTitle()}".`);
} catch (error) {
Logger.log(`Error adding responder: ${error}`);
}
}
Python
Node.js
นำผู้ตอบออกจากแบบฟอร์ม
คุณนําผู้ตอบแต่ละรายออกได้โดยใช้อีเมลหรือออบเจ็กต์ User การนําผู้ตอบออกจะเป็นการเพิกถอนสิทธิ์ของผู้ตอบในการดูและส่งแบบฟอร์ม เว้นแต่ผู้ตอบจะมีสิทธิ์เข้าถึงผ่านช่องทางอื่นๆ เช่น การแชร์โดเมนหรือการเข้าถึงไดรฟ์ที่แชร์
นำผู้ตอบรายเดียวออกโดยใช้อีเมล
REST
DELETE https://www.googleapis.com/drive/v3/files/{fileId}/permissions/PERMISSION
คุณดู permissionID ได้โดย 'listing responders' ตามที่อธิบายไว้ก่อนหน้านี้
Apps Script
/**
* Removes a single responder from a form using their email address.
*/
function `removeSingleResponderByEmail()` {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
// Replace with the responder's email address to remove
const responderEmailToRemove = 'responder-to-remove@example.com';
try {
const form = FormApp.openByUrl(formUrl);
// Remove the user as a responder
form.removePublishedReader(responderEmailToRemove);
Logger.log(`Removed ${responderEmailToRemove} as a responder from form "${
form.getTitle()}".`);
} catch (error) {
Logger.log(`Error removing responder: ${error}`);
}
}
Python
Node.js
ตรวจสอบว่าแบบฟอร์มยอมรับคําตอบจาก "ทุกคนที่มีลิงก์" หรือไม่
หากต้องการตรวจสอบว่าแบบฟอร์มยอมรับคำตอบจากทุกคนที่มีลิงก์หรือไม่ คุณต้องเปิดบริการขั้นสูงของไดรฟ์
- เปิดบริการขั้นสูงของไดรฟ์ โดยทำดังนี้
- เปิดโปรเจ็กต์ Apps Script
- คลิกบริการ (ไอคอนเครื่องหมายบวกข้างบริการ)
- ค้นหา Drive API แล้วคลิกเพิ่ม
- คลิกเพิ่ม
Apps Script
function `isAnyoneWithLinkResponder`(formId) {
let permissions = Drive.Permissions.list(formId, { includePermissionsForView: 'published' }).permissions;
if (permissions) {
for (const permission of permissions) {
if (permission.type === 'anyone' && permission.view === 'published' && permission.role === 'reader') {
return true;
}
}
}
return false;
}
Python
Node.js
วิธีตั้งค่าให้ "ทุกคนที่มีลิงก์" ตอบแบบฟอร์มได้
Apps Script
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
Python
Node.js
วิธีนำ "ทุกคนที่มีลิงก์" ออกเพื่อไม่ให้ตอบแบบฟอร์มได้
Apps Script
function `removeAnyoneWithLinkResponder`(formId) {
let permissions = Drive.Permissions.list(formId, { includePermissionsForView: 'published' }).permissions;
if (permissions) {
for (const permission of permissions) {
if (permission.type === 'anyone' && permission.role === 'reader') {
Drive.Permissions.remove(formId, permission.id);
}
}
}
}
Python
Node.js
ปิดแบบฟอร์ม
หากต้องการเลิกเผยแพร่แบบฟอร์ม ให้ใช้เมธอด Forms.setPublished(false) {/apps-script/reference/forms/form#setpublishedenabled} การเลิกเผยแพร่แบบฟอร์มจะทำให้แบบฟอร์มไม่พร้อมใช้งานและหยุดรับคำตอบโดยอัตโนมัติ
REST
ตัวอย่างเนื้อหาของคำขอ
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": false
}
}
}
Apps Script
/**
* Unpublishes a Google Form using its URL.
*/
function unpublishMyForm() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Unpublish the form. This also disables accepting responses.
form.setPublished(false);
Logger.log(`Form "${form.getTitle()}" unpublished successfully.`);
// Optional: Verify the state
if (!form.isPublished()) {
Logger.log('Form is now unpublished.');
}
if (!form.isAcceptingResponses()) {
Logger.log('Form is no longer accepting responses.');
}
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js
หากต้องการหยุดรับคำตอบของแบบฟอร์มโดยไม่เลิกเผยแพร่ ให้ใช้เมธอด Form.setAcceptingResponses(false)
ผู้ตอบแบบฟอร์มจะเห็นหน้าแบบฟอร์มที่ปิดอยู่และข้อความ
REST
ตัวอย่างเนื้อหาของคำขอ
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": true,
"isAcceptingResponses": false
}
}
}
Apps Script
/**
* Stop a Google Form from accepting responses using its URL.
*/
function closeMyFormForAcceptingResponses() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// This disables the form for accepting responses.
form.setAcceptingResponses(false);
Logger.log(`Form "${form.getTitle()}" closed for accepting responses successfully.`);
// Optional: Verify the state
if (form.isPublished()) {
Logger.log('Form is still published.');
}
if (!form.isAcceptingResponses()) {
Logger.log('Form is no longer accepting responses.');
}
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js
ตรวจสอบว่าแบบฟอร์มเป็นแบบฟอร์มเดิมหรือไม่
แบบฟอร์มเดิมคือแบบฟอร์มที่ไม่มีช่อง publishSettings ส่วนแบบฟอร์มที่สร้างใหม่ทั้งหมดรองรับการตั้งค่าการเผยแพร่
ตรวจสอบว่าแบบฟอร์มเป็นแบบเดิมหรือไม่โดยดูว่าแบบฟอร์มรองรับการเผยแพร่หรือไม่ วิธีนี้ใช้เพื่อระบุว่าได้เปิดใช้เมธอด setPublished(enabled)
และ isPublished()
รวมถึงสิทธิ์ของผู้ตอบหรือไม่
Apps Script
/**
* Checks if a form supports advanced responder permissions (i.e., is not a legacy form).
*/
function `checkIfFormSupportsPublishing()` {
// TODO(developer): Replace the URL with your own.
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Checks whether the form supports publishing or not and logs it to the console.
const supportsPublishing = form.supportsAdvancedResponderPermissions();
if (supportsPublishing) {
Logger.log(`Form "${form.getTitle()}" supports publishing (not a legacy
form).`);
} else {
Logger.log(`Form "${form.getTitle()}" is a legacy form (does not support
publishing).`);
}
return supportsPublishing;
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js