Class FormResponse

FormResponse

フォーム全体に対する回答。FormResponse は、回答者が送信した回答にアクセスする(getItemResponses() を参照)、フォームにプログラムで回答を送信する(withItemResponse(response)submit() を参照)、指定された回答を使用してフィールドを事前入力するフォームの URL を生成する、という 3 つの方法で使用できます。FormResponseForm から作成またはアクセスできます。

// 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フォームの回答の ID を取得します。
getItemResponses()ItemResponse[]フォーム レスポンスに含まれるすべての項目レスポンスを、フォーム内の項目と同じ順序で取得します。
getRespondentEmail()StringForm.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。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getGradableItemResponses()

フォーム レスポンスに含まれるすべての項目レスポンスを、フォーム内の項目と同じ順序で取得します。このメソッドは getItemResponses() と同様に機能しますが、回答がない場合でも、対応する Item が採点可能(つまり、点数がある)であれば、ItemResponse を返します。ただし、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[] - 回答者がスコアを受け取ることができるフォーム内のすべての質問項目に対する回答の配列。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getGradableResponseForItem(item)

指定された項目のフォームの回答に含まれる項目の回答を取得します。このメソッドは getResponseForItem(item) と同様に機能しますが、回答がない場合でも、対応する Item を採点できる(つまり、点数がある)場合は ItemResponse を返します。ただし、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()

フォームの回答の ID を取得します。フォームの回答が送信されていない場合、このメソッドは 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 - フォームの回答の ID。フォームの回答が送信されていない場合は null

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getItemResponses()

フォーム レスポンスに含まれるすべての項目レスポンスを、フォーム内の項目と同じ順序で取得します。フォームの回答に、特定の TextItemDateItemTimeItemParagraphTextItem の回答が含まれていない場合、そのアイテムに対して返される 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[] - 回答者が回答したフォーム内のすべての質問項目に対する回答の配列。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - フォームの回答ストアに保存された、新しく作成された回答。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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