Class FormResponse

FormResponse

對整個表單的回應。您可以透過三種方式使用 FormResponse:存取作答者提交的答案 (請參閱 getItemResponses())、以程式輔助方式提交表單回應 (請參閱 withItemResponse(response)submit()),以及產生表單網址,以便使用所提供的答案預先填入欄位。您可以透過 Form 建立或存取 FormResponse

// 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產生網址,可用於編輯已提交的回應。
getGradableItemResponses()ItemResponse[]取得表單回應中包含的所有項目回應,順序與表單中顯示的項目順序相同。
getGradableResponseForItem(item)ItemResponse取得特定項目表單回應中包含的項目回應。
getId()String取得表單回應的 ID。
getItemResponses()ItemResponse[]取得表單回應中包含的所有項目回應,順序與表單中顯示的項目順序相同。
getRespondentEmail()String取得提交回覆者的電子郵件地址 (如果已啟用 Form.setCollectEmail(collect) 設定)。
getResponseForItem(item)ItemResponse取得特定項目在此表單回應中包含的項目回應。
getTimestamp()Date取得提交表單回應的時間戳記。
submit()FormResponse提交回應。
toPrefilledUrl()String產生表單網址,用於根據此表單回應中的答案預先填入答案。
withItemGrade(gradedResponse)FormResponse將指定項目回覆的成績新增至表單回覆中。
withItemResponse(response)FormResponse在表單回應中新增指定項目回應。

內容詳盡的說明文件

getEditResponseUrl()

產生網址,可用於編輯已提交的回應。如果停用 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:用於變更已提交回覆的網址。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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[]:表單中每個問題項目的回應陣列,作答者可能會收到分數。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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[]:表單中每個問題項目的回應陣列,作答者會提供答案。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

產生表單網址,用於根據此表單回應中的答案預先填入答案。

// 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:已預先填入答案的表單網址。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

參數

名稱類型說明
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