本页介绍了如何执行以下与表单相关的任务:
- 发布表单,以便回复者可以访问该表单
- 查找表单回复者
- 与更多回复者共享您的表单
- 从表单中移除回复者
- 检查表单是否接受来自“知道链接的任何人”的回复
- 关闭表单
- 取消发布表单
- 停止接受表单回复
- 检查表单是否为旧版表单
准备工作
在继续执行本页面上的任务之前,请先执行以下任务:
获取表单 ID。当您使用
forms.create
创建表单时,表单 ID 会在响应的formId
字段中返回。
发布表单,以便回复者可以访问该表单
您可以使用 forms.setPublishSettings
方法发布现有表单。
使用表单 ID 调用
forms.setPublishSettings
方法。
REST
请求正文示例
{
"publishSettings": {
"isPublished": true,
"isAcceptingResponses": true
}
}
Apps 脚本
/**
* 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 脚本
/**
* 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 脚本
/**
* 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 脚本
/**
* 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 脚本项目。
- 点击服务(服务旁边的加号图标)。
- 找到 Drive API,然后点击添加。
- 点击添加。
Apps 脚本
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 脚本
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
Python
Node.js
如要移除“知道链接的任何人”选项,请执行以下操作:
Apps 脚本
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 脚本
/**
* 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 脚本
/**
* 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 脚本
/**
* 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