本頁說明如何執行下列表單相關工作:
- 發布表單,讓作答者可以存取
- 找出表單回覆者
- 將表單分享給更多作答者
- 從表單中移除作答者
- 確認表單是否接受「知道連結的任何人」的回覆
- 關閉表單
- 取消發布表單
- 停止接受表單回覆
- 檢查表單是否為舊版表單
事前準備
請先完成下列工作,再繼續執行本頁面的工作:
取得表單 ID。使用
forms.create
建立表單時,回應的formId
欄位會傳回表單 ID。
發布表單,讓作答者可以存取
您可以使用 forms.setPublishSettings
方法發布現有表單。
使用表單 ID 呼叫
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
找出表單作答者
您可以使用 Form.getPublishedReaders() 擷取所有具有回應者存取權 (PUBLISHED_READER 角色) 的使用者清單。這會傳回使用者物件陣列。
REST
將查詢參數 includePermissionsForView=published
附加至要求網址。
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
方法。
使用表單 ID 和存取權設定,呼叫
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
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」並點按「Add」(新增)。
- 按一下 [新增]。
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