בדף הזה נסביר איך לבצע את המשימות הבאות שקשורות לטפסים:
- פרסום הטופס כדי שהמשיבים יוכלו לגשת אליו
- איך מוצאים משיבים לטופס
- שיתוף הטופס עם עוד משיבים
- הסרת משיבים מהטופס
- בודקים אם הטופס מקבל תשובות מכל מי שיש לו קישור
- סגירת טופס
- ביטול הפרסום של הטופס
- הפסקת קבלת תשובות לטופס
- איך בודקים אם טופס הוא טופס מדור קודם
לפני שמתחילים
לפני שממשיכים למשימות שבדף הזה, צריך לבצע את המשימות הבאות:
מוצאים את מזהה הטופס. מזהה הטופס מוחזר בשדה
formId
של התגובה כשיוצרים טופס באמצעותforms.create
.
פרסום הטופס כדי שהמשיבים יוכלו לגשת אליו
אפשר לפרסם טופס קיים באמצעות השיטה forms.setPublishSettings
.
מפעילים את השיטה
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
איך מוצאים את המשיבים לטופס
אפשר לאחזר רשימה של כל המשתמשים שיש להם גישת השבה (התפקיד PUBLISHED_READER) באמצעות Form.getPublishedReaders(). הפונקציה הזו מחזירה מערך של אובייקטים של משתמשים.
REST
מוסיפים את פרמטר השאילתה includePermissionsForView=published
לכתובת ה-URL של הבקשה.
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
שיתוף הטופס עם עוד משיבים
כדי להוסיף משיבים לטופס כך שהם יוכלו לפתוח אותו ולהשיב עליו, אפשר להשתמש בשיטה של Drive permissions.create
.
קוראים לשיטה
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. הסרת משיב מבטלת את היכולת שלו לצפות בטופס ולשלוח אותו, אלא אם יש לו גישה באמצעים אחרים, כמו שיתוף דומיין או גישה לתיקייה משותפת ב-Drive.
הסרת משיב אוטומטי יחיד לפי כתובת אימייל
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
בודקים אם הטופס מקבל תשובות מכל מי שיש לו את הקישור
כדי לבדוק אם הטופס מקבל תשובות מכל מי שיש לו קישור, צריך להפעיל את השירות המתקדם של Drive.
- מפעילים את השירות המתקדם של Drive:
- פותחים את פרויקט Apps Script.
- לוחצים על Services (שירותים) (סמל הפלוס לצד Service (שירות)).
- מאתרים את 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