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 vào biểu mẫu
- Tìm người trả lời biểu mẫu
- Chia sẻ biểu mẫu 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 biểu mẫu
- Kiểm tra xem biểu mẫu có phải là biểu mẫu cũ hay không
Trước khi bắt đầu
Hãy thực hiện các việc sau trước khi tiếp tục thực hiện các việc 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 vào biểu mẫu
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ã nhận dạng 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 của 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 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 hoặc bằng cách sử dụng đối tượng User. Khi bạn xoá một người trả lời, người đó sẽ không thể xem và gửi biểu mẫu nữa, trừ phi họ có quyền truy cập thông qua các phương thức 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 phản hồ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ê các đối tượng phản hồ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 của bạn.
- 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 thiết lập để "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 cứ ai có đường liên kết đều có thể phản hồ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 có thể 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 phản hồ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 phản hồ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 sẽ thấy trang và thông báo biểu mẫu đã đóng.
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 biểu mẫu có phải là biểu mẫu cũ hay không
Biểu mẫu cũ là biểu mẫu không có trường publishSettings, trong khi tất cả biểu mẫu mới được tạo đều hỗ trợ chế độ cài đặt xuất bản.
Kiểm tra xem 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ợ việc xuất bản hay không. Phương thức này được dùng để xác định xem các phương thức setPublished(enabled)
và isPublished()
cũng như quyền của người 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