本頁面說明如何執行下列與表單相關的作業:
- 發布表單,讓作答者存取
- 尋找表單作答者
- 與更多作答者分享表單
- 從表單中移除作答者
- 確認表單是否接受「任何知道連結的使用者」的回覆
- 關閉表單
- 取消發布表單
- 停止接受表單回覆
- 檢查表單是否為舊版表單
事前準備
請先完成下列工作,再繼續執行本頁中的任務:
取得表單 ID。使用
forms.create
建立表單時,表單 ID 會傳回至回應的formId
欄位。
發布表單,讓作答者存取
您可以使用 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」,然後點選「新增」。
- 按一下 [新增]。
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