این صفحه نحوه انجام این وظایف شامل فرم ها را شرح می دهد:
- فرم را منتشر کنید تا پاسخ دهندگان بتوانند به آن دسترسی داشته باشند
- پاسخگوی فرم خود پیدا کنید
- فرم خود را با پاسخ دهندگان بیشتری به اشتراک بگذارید
- پاسخگوها را از فرم خود حذف کنید
- بررسی کنید آیا فرم پاسخهایی را از «هر کسی با پیوند» میپذیرد
- یک فرم را ببندید
- لغو انتشار یک فرم
- پذیرش پاسخ به یک فرم را متوقف کنید
- بررسی کنید که آیا یک فرم یک فرم قدیمی است
قبل از شروع
قبل از ادامه کار در این صفحه، کارهای زیر را انجام دهید:
شناسه فرم را دریافت کنید. وقتی فرمی را با استفاده از
forms.create
ایجاد میکنید، شناسه فرم در قسمتformId
پاسخ برگردانده میشود.
فرم را منتشر کنید تا پاسخ دهندگان بتوانند به آن دسترسی داشته باشند
می توانید یک فرم موجود را با روش forms.setPublishSettings
منتشر کنید.
متد
forms.setPublishSettings
را با شناسه فرم فراخوانی کنید.
استراحت
نمونه بدنه درخواست
{
"publishSettings": {
"isPublished": true,
"isAcceptingResponses": true
}
}
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
پاسخ دهندگان فرم خود را پیدا کنید
با استفاده از Form.getPublishedReaders() میتوانید فهرستی از تمام کاربرانی که به پاسخدهنده دسترسی دارند (نقش PUBLISHED_READER) بازیابی کنید. این آرایه ای از اشیاء کاربر را برمی گرداند.
استراحت
پارامتر query includePermissionsForView=published
به URL درخواست اضافه کنید.
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
فرم خود را با پاسخ دهندگان بیشتری به اشتراک بگذارید
برای افزودن پاسخگوها به فرم به طوری که بتوانند آن را باز کنند و به آن پاسخ دهند، میتوانید از روش Drive permissions.create
استفاده کنید.
با شناسه فرم و تنظیمات دسترسی، متد
permissions.create
را فراخوانی کنید.
استراحت
نمونه بدنه درخواست
{
"view": "published",
"role": "reader",
"type": "user",
"emailAddress": "user@example.com"
}
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
پاسخگوها را از فرم خود حذف کنید
میتوانید تک تک پاسخدهندگان را با آدرس ایمیل یا با استفاده از شی کاربر حذف کنید. حذف پاسخدهنده توانایی آنها را برای مشاهده و ارسال فرم لغو میکند، مگر اینکه از راههای دیگری مانند اشتراک دامنه یا دسترسی به درایو مشترک دسترسی داشته باشند.
یک پاسخ دهنده را از طریق آدرس ایمیل حذف کنید
استراحت
DELETE https://www.googleapis.com/drive/v3/files/{fileId}/permissions/ PERMISSION
permissionID را می توان با "فهرست کردن پاسخ دهندگان" همانطور که قبلا توضیح داده شد پیدا کرد
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
بررسی کنید آیا فرم پاسخهای «هر کسی با پیوند» را میپذیرد
برای بررسی اینکه آیا فرم پاسخهای افراد دارای پیوند را میپذیرد، باید سرویس پیشرفته Drive را روشن کنید.
- سرویس پیشرفته Drive را روشن کنید:
- پروژه Apps Script خود را باز کنید.
- روی Services (نماد مثبت در کنار سرویس ) کلیک کنید.
- Drive API را پیدا کنید و روی Add کلیک کنید.
- روی افزودن کلیک کنید.
اسکریپت برنامه ها
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;
}
پایتون
Node.js
برای تنظیم «هر کسی که پیوند دارد» میتواند به فرم پاسخ دهد:
اسکریپت برنامه ها
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
پایتون
Node.js
برای حذف «هر کسی که پیوند دارد» میتواند به فرم پاسخ دهد:
اسکریپت برنامه ها
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);
}
}
}
}
پایتون
Node.js
یک فرم را ببندید
برای لغو انتشار یک فرم، از روش Forms.setPublished(false) استفاده می کنید. {/apps-script/reference/forms/form#setpublishedenabled} لغو انتشار یک فرم، آن را از دسترس خارج میکند و بهطور خودکار از پذیرش پاسخها جلوگیری میکند.
استراحت
نمونه بدنه درخواست
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": false
}
}
}
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
برای توقف پذیرش پاسخ برای یک فرم بدون لغو انتشار، می توانید از روش Form.setAcceptingResponses(false)
استفاده کنید. پاسخ دهندگان به فرم شما صفحه فرم بسته و پیام را می بینند.
استراحت
نمونه بدنه درخواست
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": true,
"isAcceptingResponses": false
}
}
}
اسکریپت برنامه ها
/**
* 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}`);
}
}
پایتون
Node.js
بررسی کنید که آیا یک فرم یک فرم قدیمی است
فرمهای قدیمی فرمهایی هستند که فیلد publicSettings ندارند، در حالی که همه فرمهای جدید ایجاد شده از تنظیمات انتشار پشتیبانی میکنند.
با تعیین اینکه آیا فرم از انتشار پشتیبانی می کند، بررسی کنید که آیا یک فرم قدیمی است یا نه. این روش برای تعیین اینکه آیا متدهای setPublished(enabled)
و isPublished()
و مجوزهای پاسخ دهنده فعال هستند یا خیر استفاده می شود.
اسکریپت برنامه ها
/**
* 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}`);
}
}