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

取得表單回應中所包含的所有項目回應 (與項目顯示順序相同) 。如果表單回應未包含特定 TextItem 的回應, DateItemTimeItemParagraphTextItemItemResponse 針對該項目,傳回的回應將包含空字串做為回應。如果表單回應省略了 其他項目類型的回應,此方法會從傳回的陣列中排除該項目。

// 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