Class FormResponse

FormResponse

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

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

Methods

メソッド戻り値の型概要
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()}`);
}

パラメータ

Name説明
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()

フォームのレスポンスに含まれるすべてのアイテムのレスポンスを、フォームに表示されるアイテムと同じ順序で取得します。フォームの 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[] - 回答者が回答を提供したフォーム内のすべての質問アイテムに対する回答の配列。

認証

この方法を使用するスクリプトには、次の 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());
}

パラメータ

Name説明
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
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);

パラメータ

Name説明
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();

パラメータ

Name説明
responseItemResponse

復路

FormResponse - チェーン用の FormResponse

認証

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

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