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

الطُرق

الطريقةنوع القيمة التي يتم إرجاعهاوصف قصير
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)

تضيف هذه الطريقة درجات ردّ العنصر المحدّد إلى ردّ النموذج. لا تنطبق هذه الطريقة إلا على ردود النماذج التي تم إرسالها، ولا تؤثّر إلا في الدرجات المخزّنة بعد إرسالها. لا تعدّل هذه الطريقة أيضًا سوى درجات ردّ العنصر، ولا تؤثّر في الرد الفعلي (بما أنّ الرد قد تم إرساله). إذا تم استدعاء هذه الطريقة عدة مرات للعنصر نفسه، سيتم الاحتفاظ بالدرجة الأخيرة فقط. إذا كان 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

الإرجاع

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

المعلمات

الاسمالنوعالوصف
responseItemResponse

الإرجاع

FormResponse: هذا FormResponse، يُستخدَم للربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

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