תשובה לטופס כולו. אפשר להשתמש ב-FormResponse
בשלוש דרכים: כדי לגשת לתשובות שנשלחו על ידי המשיב (מידע נוסף: getItemResponses()
), לשלוח תשובות לטופס באופן פרוגרמטי (פרטים נוספים ב-withItemResponse(response)
וב-submit()
) וליצור כתובת URL לטופס שממלא מראש שדות באמצעות התשובות שסופקו. ניתן ליצור תשובות מסוג FormResponse
או לגשת אליהן דרך Form
.
// Open a form by ID and log the responses to each question. var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); var formResponses = form.getResponses(); for (var i = 0; i < formResponses.length; i++) { var formResponse = formResponses[i]; var itemResponses = formResponse.getItemResponses(); for (var j = 0; j < itemResponses.length; j++) { var itemResponse = itemResponses[j]; Logger.log('Response #%s to the question "%s" was "%s"', (i + 1).toString(), itemResponse.getItem().getTitle(), itemResponse.getResponse()); } }
שיטות
שיטה | סוג הערך שמוחזר | תיאור קצר |
---|---|---|
getEditResponseUrl() | String | יוצר כתובת URL שאפשר להשתמש בה כדי לערוך תשובה שכבר נשלחה. |
getGradableItemResponses() | ItemResponse[] | הפונקציה מקבלת את כל התגובות לפריטים שכלולים בתגובה לטופס, באותו סדר שבו הפריטים מופיעים בטופס. |
getGradableResponseForItem(item) | ItemResponse | הפונקציה מקבלת את התגובה לפריט שכלולה בתגובה לטופס של פריט נתון. |
getId() | String | הפונקציה מקבלת את המזהה של התגובה לטופס. |
getItemResponses() | ItemResponse[] | הפונקציה מקבלת את כל התגובות לפריטים שכלולים בתגובה לטופס, באותו סדר שבו הפריטים מופיעים בטופס. |
getRespondentEmail() | String | מקבלת את כתובת האימייל של האדם ששלח את התשובה, אם ההגדרה Form.setCollectEmail(collect) מופעלת. |
getResponseForItem(item) | ItemResponse | הפונקציה מקבלת את התגובה לפריט שכלול בתשובה לטופס הזה לגבי פריט נתון. |
getTimestamp() | Date | הפונקציה מקבלת את חותמת הזמן של שליחת תגובה לטופס. |
submit() | FormResponse | שליחת התשובה. |
toPrefilledUrl() | String | הפונקציה יוצרת כתובת URL לטופס שבו התשובות שמולאו מראש מבוססות על התשובות בטופס הזה. |
withItemGrade(gradedResponse) | FormResponse | הוספת הציונים של התגובה הנתונה לתשובה לטופס. |
withItemResponse(response) | FormResponse | הוספת התגובה הנתונה לפריט בתגובה לטופס. |
תיעוד מפורט
getEditResponseUrl()
יוצר כתובת URL שאפשר להשתמש בה כדי לערוך תשובה שכבר נשלחה. אם ההגדרה Form.setAllowResponseEdits(enabled)
מושבתת, הקישור מוביל לדף שמוסבר בו שהאפשרות לערוך תשובות לטופס מושבתת. כל מי שמבקר בקישור יכול לערוך את התגובה, למרות שיש לו צורך בחשבון עם גישה לטופס אם ההגדרה Form.setRequireLogin(requireLogin)
מופעלת. אם ההגדרה Form.setCollectEmail(collect)
מופעלת, בטופס מתעד את כתובת האימייל של המשתמש שערך את התשובה ולא את כתובת האימייל של המשיב המקורי.
בתגובה לטופס שהסקריפט יצר אבל עדיין לא נשלח, השיטה הזו מחזירה את הערך null
.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first form response. const formResponse = form.getResponses()[0]; // Gets the edit URL for the first form response and logs it to the console. const editUrl = formResponse.getEditResponseUrl(); console.log(editUrl);
החזרות
String
– כתובת ה-URL לשינוי התשובה שנשלחה.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getGradableItemResponses()
הפונקציה מקבלת את כל התגובות לפריטים שכלולים בתגובה לטופס, באותו סדר שבו הפריטים מופיעים בטופס. השיטה הזו פועלת בדומה ל-getItemResponses()
, אבל כדי לתת ניקוד לתשובה חסרה, היא עדיין מחזירה ItemResponse
אם אפשר לתת ציון לItem
הרלוונטי (כלומר יש לו ערך ניקוד), גם אם אין תשובה בפועל. עם זאת, אם אי אפשר לתת ציון ל-Item
, השיטה הזו מחריגה את הפריט מהמערך המוחזר.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Gets the item responses contained in each form response. for (const formResponse of formResponses){ const gradableItemsResponses = formResponse.getGradableItemResponses(); // Logs the title and score for each item response to the console. for (const gradableItemsResponse of gradableItemsResponses) { console.log(`${gradableItemsResponse.getItem().getTitle()} score ${gradableItemsResponse.getScore()}`); } }
החזרות
ItemResponse[]
– מערך של תשובות לכל פריט שאלה בטופס שהמשיב יכול לקבל ציון עבורו.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getGradableResponseForItem(item)
הפונקציה מקבלת את התגובה לפריט שכלולה בתגובה לטופס של פריט נתון. השיטה הזו פועלת בדומה ל-getResponseForItem(item)
, אבל כדי לתת ניקוד על תשובה חסרה, היא עדיין מחזירה ItemResponse
אם אפשר לתת ציון ל-Item
התואם (כלומר, יש לו ערך ניקוד), גם אם אין תשובה בפועל. עם זאת, אם אי אפשר לתת ציון ב-Item
, השיטה הזו תחזיר את הערך null
.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Gets the item responses contained in a form response. for (const formResponse of formResponses) { const formItemResponses = formResponse.getGradableItemResponses(); // Logs the title and score for responses to the first item of the form. const itemResponse = formResponse.getGradableResponseForItem(formItemResponses[0].getItem()); console.log(`${itemResponse.getItem().getTitle()} score ${itemResponse.getScore()}`); }
פרמטרים
שם | תיאור | התיאור |
---|---|---|
item | Item |
החזרות
ItemResponse
– התשובה לפריט נתון או null
אם לא קיימת ולפריט אין ציון.
getId()
הפונקציה מקבלת את המזהה של התגובה לטופס. השיטה הזו מחזירה את הערך null
אם התשובה לטופס לא נשלחה.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the form responses and logs the ID for each form response to the console. for (const formResponse of formResponses) { console.log(`Response ID: ${formResponse.getId()}`); }
החזרות
String
– המזהה של התגובה לטופס, או null
אם התשובה לטופס לא נשלחה.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItemResponses()
הפונקציה מקבלת את כל התגובות לפריטים שכלולים בתגובה לטופס, באותו סדר שבו הפריטים מופיעים בטופס. אם התשובה לטופס לא מכילה תגובה ל-TextItem
, ל-DateItem
, ל-TimeItem
או ל-ParagraphTextItem
, ב-ItemResponse
שתוחזר לאותו פריט תופיע מחרוזת ריקה בתור התגובה. אם התשובה לטופס לא מוסיפה תגובה לסוג פריט אחר כלשהו, השיטה הזו מחריגה את הפריט מהמערך המוחזר.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the responses to the form. const formResponses = form.getResponses(); // Iterates over the responses. for (const formResponse of formResponses) { // Gets the item responses from each form response. const itemResponses = formResponse.getItemResponses(); // Iterates over the item responses. for (const itemResponse of itemResponses) { // Logs the items' questions and responses to the console. console.log(`Response to the question '${itemResponse.getItem().getTitle()}' was '${itemResponse.getResponse()}'`); } }
החזרות
ItemResponse[]
– מערך של תגובות לכל פריט לשאלה בטופס שהמשיב סיפק לו תשובה.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getRespondentEmail()
מקבלת את כתובת האימייל של האדם ששלח את התשובה, אם ההגדרה Form.setCollectEmail(collect)
מופעלת.
בתגובה לטופס שהסקריפט יצר אבל עדיין לא נשלח, השיטה הזו מחזירה את הערך null
.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs each respondent's email to the console. // To collect respondent emails, ensure that Form.setCollectEmail(collect) is set to true. for (const formResponse of formResponses) { console.log(`Respondent Email: ${formResponse.getRespondentEmail()}`); }
החזרות
String
– כתובת האימייל של האדם ששלח את התשובה הזו, אם היא זמינה, או null
אם הסקריפט יצר את התגובה הזו אבל עדיין לא שלח אותה.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponseForItem(item)
הפונקציה מקבלת את התגובה לפריט שכלול בתשובה לטופס הזה לגבי פריט נתון.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first item on the form. const item = form.getItems()[0]; // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs each response to the first item to the console. for (const formResponse of formResponses) { const itemResponse = formResponse.getResponseForItem(item); console.log(itemResponse.getResponse()); }
פרמטרים
שם | תיאור | התיאור |
---|---|---|
item | Item |
החזרות
ItemResponse
- התשובה לפריט נתון או null
אם לא קיימת.
getTimestamp()
הפונקציה מקבלת את חותמת הזמן של שליחת תגובה לטופס.
בתגובה לטופס שהסקריפט יצר אבל עדיין לא נשלח, השיטה הזו מחזירה את הערך null
.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets an array of the form's responses. const formResponses = form.getResponses(); // Loops through the responses and logs the timestamp of each response to the console. for (const formResponse of formResponses) { console.log(`Timestamp: ${formResponse.getTimestamp()}`); }
החזרות
Date
– חותמת הזמן שבה התשובה הזו נשלחה, או null
אם הסקריפט יצר את התגובה הזו אבל עדיין לא שלח אותה.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
submit()
שליחת התשובה. אם התשובה כבר נשלחה, הפונקציה הזו מוסיפה חריגת סקריפט.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Creates an empty response for the form. const formResponse = form.createResponse(); // Submits an empty response. formResponse.submit();
החזרות
FormResponse
- תגובה חדשה שנוצרה נשמרה במאגר התגובות של הטופס.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
toPrefilledUrl()
הפונקציה יוצרת כתובת URL לטופס שבו התשובות שמולאו מראש מבוססות על התשובות בטופס הזה.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Gets the first form response. const formResponse = form.getResponses()[0]; // Generates and logs the URL of a pre-filled form response based on the answers // of the first form response. const prefilledUrl = formResponse.toPrefilledUrl(); console.log(prefilledUrl);
החזרות
String
– כתובת ה-URL של טופס עם תשובות שמולאו מראש.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
withItemGrade(gradedResponse)
הוספת הציונים של התגובה הנתונה לתשובה לטופס. השיטה הזו חלה רק על תשובות לטפסים שכבר נשלחו, ומשפיעה רק על ציונים שמורים אחרי שהם נשלחים. כמו כן, השיטה הזו מעדכנת רק את הציונים של התשובות שניתנו על הפריט, ולא משפיעה על התשובה בפועל (כי התשובה כבר נשלחה). אם מפעילים את השיטה הזו כמה פעמים לאותו פריט, רק הציון האחרון נשמר. אם התגובה לפריט כוללת אין ציונים, השיטה הזו תסיר את הציונים של הפריט.
// Programmatically award partial credit for a given response var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); var formResponses = form.getResponses(); var formItems = form.getItems(); for (var i = 0; i < formResponses.length; i++) { var formResponse = formResponses[i]; for (var j = 0; j < formItems.length; j++) { var item = formItems[j]; var points = item.asMultipleChoiceItem().getPoints(); var itemResponse = formResponse.getGradableResponseForItem(item); Logger.log('Award half credit for answers containing the word "Kennedy"'); var answer = itemResponse.getResponse(); if (answer != null && answer.includes('Kennedy')) { itemResponse.setScore(points / 2); formResponse.withItemGrade(itemResponse); } } } form.submitGrades(formResponses);
פרמטרים
שם | תיאור | התיאור |
---|---|---|
gradedResponse | ItemResponse |
החזרות
FormResponse
— FormResponse
, לשרשור
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
withItemResponse(response)
הוספת התגובה הנתונה לפריט בתגובה לטופס. השיטה הזו מתאימה רק לתגובות לטופס שהסקריפט יצר אבל עדיין לא נשלח. היא לא יכולה להשפיע על תשובות שמורות. אם מפעילים את השיטה הזו כמה פעמים לאותו פריט, רק התגובה האחרונה של הפריט נשמרת.
// Opens the Forms file by its ID. // If you created your script from within a Google Forms file, you can // use FormApp.getActiveForm() instead. // TODO(developer): Replace the ID with your own. const form = FormApp.openById('abc123456'); // Creates a response for the form. const formResponse = form.createResponse(); // Appends a checkbox item to the form. const item = form.addCheckboxItem(); // Sets the title of the item to 'Which items are ice cream flavors?' item.setTitle('Which items are ice cream flavors?'); // Sets choices for the item. item.setChoices([ item.createChoice('Vanilla'), item.createChoice('Strawberry'), item.createChoice('Brick') ]); // Creates a response for the item. const response = item.createResponse(['Vanilla', 'Strawberry']); // Adds the item response to the form response. formResponse.withItemResponse(response); // Submits the form response. formResponse.submit();
פרמטרים
שם | תיאור | התיאור |
---|---|---|
response | ItemResponse |
החזרות
FormResponse
– FormResponse
, לשרשור.
הרשאות
לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms