Class FormResponse

טופסתשובה

תשובה לטופס כולו. אפשר להשתמש בFormResponse בשלוש דרכים: כדי לגשת לתשובות שנשלחו על ידי המשיב (ראו getItemResponses()), כדי לשלוח תשובה לטופס באופן פרוגרמטי (ראו withItemResponse(response) ו-submit()) וכדי ליצור כתובת URL לטופס שבה השדות ימולאו מראש באמצעות התשובות שסופקו. אפשר ליצור FormResponse או לגשת אליהם מתוך Form.

// Open a form by ID and log the responses to each question.
const form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
const formResponses = form.getResponses();
for (let i = 0; i < formResponses.length; i++) {
  const formResponse = formResponses[i];
  const itemResponses = formResponse.getItemResponses();
  for (let j = 0; j < itemResponses.length; j++) {
    const itemResponse = itemResponses[j];
    Logger.log(
        'Response #%s to the question "%s" was "%s"',
        (i + 1).toString(),
        itemResponse.getItem().getTitle(),
        itemResponse.getResponse(),
    );
  }
}

Methods

שיטהסוג הערך שמוחזרתיאור קצר
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()}`,
  );
}

פרמטרים

שםסוגתיאור
itemItem

חזרה

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());
}

פרמטרים

שםסוגתיאור
itemItem

חזרה

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)

הפונקציה מוסיפה את הציונים של תשובת הפריט שצוינה לתשובה בטופס. השיטה הזו חלה רק על תשובות בטופס שכבר נשלחו, ומשפיעה רק על ציונים שנשמרו אחרי שהם נשלחו. בנוסף, ה-method הזה מעדכן רק את הציונים של התשובה לפריט, ולא משפיע על התשובה עצמה (כי התשובה כבר נשלחה). אם קוראים לשיטה הזו כמה פעמים עבור אותו פריט, רק הציון האחרון נשמר. אם ה-ItemResponse לא מכיל ציונים, השיטה הזו מסירה את הציונים של הפריט.

// Programmatically award partial credit for a given response
const form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
const formResponses = form.getResponses();
const formItems = form.getItems();
for (const formResponse of formResponses) {
  for (const item of formItems) {
    const points = item.asMultipleChoiceItem().getPoints();
    const itemResponse = formResponse.getGradableResponseForItem(item);
    Logger.log('Award half credit for answers containing the word "Kennedy"');
    const answer = itemResponse.getResponse();

    if (answer?.includes('Kennedy')) {
      itemResponse.setScore(points / 2);
      formResponse.withItemGrade(itemResponse);
    }
  }
}
form.submitGrades(formResponses);

פרמטרים

שםסוגתיאור
gradedResponseItemResponse

חזרה

FormResponseFormResponse הזה, לשרשור

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

  • 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();

פרמטרים

שםסוגתיאור
responseItemResponse

חזרה

FormResponseFormResponse הזה, לשרשור.

אישור

סקריפטים שמשתמשים בשיטה הזו דורשים הרשאה עם אחת או יותר מההיקפים הבאים:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms