Trang này mô tả cách thực hiện các thao tác liên quan đến biểu mẫu sau:
- Xuất bản biểu mẫu để người trả lời có thể truy cập
- Tìm người trả lời biểu mẫu
- Chia sẻ biểu mẫu của bạn với nhiều người trả lời hơn
- Xoá người trả lời khỏi biểu mẫu
- Kiểm tra xem biểu mẫu có chấp nhận câu trả lời của "Bất kỳ ai có đường liên kết" hay không
- Đóng biểu mẫu
- Huỷ xuất bản biểu mẫu
- Dừng chấp nhận câu trả lời cho một biểu mẫu
- Kiểm tra xem một biểu mẫu có phải là biểu mẫu cũ hay không
Trước khi bắt đầu
Hãy làm các việc sau trước khi tiếp tục thực hiện các nhiệm vụ trên trang này:
Lấy mã biểu mẫu. Mã biểu mẫu được trả về trong trường
formId
của phản hồi khi bạn tạo biểu mẫu bằngforms.create
.
Xuất bản biểu mẫu để người trả lời có thể truy cập
Bạn có thể xuất bản một biểu mẫu hiện có bằng phương thức forms.setPublishSettings
.
Gọi phương thức
forms.setPublishSettings
bằng mã biểu mẫu.
REST
Nội dung yêu cầu mẫu
{
"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
Tìm người trả lời biểu mẫu
Bạn có thể truy xuất danh sách tất cả người dùng có quyền truy cập vào người trả lời (vai trò PUBLISHED_READER) bằng cách sử dụng Form.getPublishedReaders(). Thao tác này sẽ trả về một mảng các đối tượng người dùng.
REST
Thêm tham số truy vấn includePermissionsForView=published
vào URL yêu cầu.
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
Chia sẻ biểu mẫu của bạn với nhiều người trả lời hơn
Để thêm người trả lời vào biểu mẫu để họ có thể mở và trả lời biểu mẫu, bạn có thể sử dụng phương thức permissions.create
của Drive.
Gọi phương thức
permissions.create
bằng mã biểu mẫu và chế độ cài đặt quyền truy cập.
REST
Nội dung yêu cầu mẫu
{
"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
Xoá người trả lời khỏi biểu mẫu
Bạn có thể xoá từng người trả lời theo địa chỉ email của họ hoặc sử dụng đối tượng Người dùng. Việc xoá người trả lời sẽ thu hồi quyền xem và gửi biểu mẫu của họ, trừ phi họ có quyền truy cập thông qua các phương tiện khác như chia sẻ miền hoặc quyền truy cập vào ổ đĩa dùng chung.
Xoá một người trả lời theo địa chỉ email
REST
DELETE https://www.googleapis.com/drive/v3/files/{fileId}/permissions/PERMISSION
Bạn có thể tìm thấy permissionID bằng cách "liệt kê người trả lời" như mô tả trước đó
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
Kiểm tra xem biểu mẫu có chấp nhận câu trả lời của "Bất kỳ ai có đường liên kết" hay không
Để kiểm tra xem biểu mẫu có chấp nhận câu trả lời của bất kỳ ai có đường liên kết hay không, bạn cần bật Dịch vụ nâng cao của Drive.
- Bật Dịch vụ nâng cao của Drive:
- Mở dự án Apps Script.
- Nhấp vào Dịch vụ (biểu tượng dấu cộng bên cạnh Dịch vụ).
- Tìm Drive API rồi nhấp vào Thêm.
- Nhấp vào Thêm.
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
Cách đặt chế độ "Bất kỳ ai có đường liên kết" đều có thể trả lời biểu mẫu:
Apps Script
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
Python
Node.js
Cách xoá chế độ "Bất kỳ ai có đường liên kết" có thể trả lời biểu mẫu:
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
Đóng biểu mẫu
Để huỷ xuất bản biểu mẫu, bạn sử dụng phương thức Forms.setPublished(false). {/apps-script/reference/forms/form#setpublishedenabled} Việc huỷ xuất bản biểu mẫu sẽ khiến biểu mẫu đó không hoạt động và tự động ngừng chấp nhận câu trả lời.
REST
Nội dung yêu cầu mẫu
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
Để ngừng chấp nhận câu trả lời cho một biểu mẫu mà không cần huỷ xuất bản biểu mẫu đó, bạn có thể sử dụng phương thức Form.setAcceptingResponses(false)
.
Người trả lời biểu mẫu của bạn sẽ thấy trang biểu mẫu đã đóng và thông báo.
REST
Nội dung yêu cầu mẫu
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
Kiểm tra xem một biểu mẫu có phải là biểu mẫu cũ hay không
Biểu mẫu cũ là những biểu mẫu không có trường publishSettings, trong khi tất cả biểu mẫu mới tạo đều hỗ trợ chế độ cài đặt phát hành.
Kiểm tra xem một biểu mẫu có phải là biểu mẫu cũ hay không bằng cách xác định xem biểu mẫu đó có hỗ trợ tính năng phát hành hay không. Phương thức này dùng để xác định xem các phương thức setPublished(enabled)
và isPublished()
cũng như quyền của trình phản hồi có được bật hay không.
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