整個表單的回應。FormResponse
支援三種方式:存取作答者提交的答案 (請參閱 getItemResponses()
)、透過程式提交表單回覆 (請參閱 withItemResponse(response)
和 submit()
),以及使用提供的答案在表單中預先填入表單網址。FormResponse
可建立或存取 Form
。
// 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
。
Return 鍵
String
:用於變更已提交回覆的網址
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getGradableItemResponses()
取得表單回應中的所有項目回應,其順序與表單中顯示的項目相同。這個方法的運作方式與 getItemResponses()
類似,但若要為缺少的答案進行評分,即使可以對對應的 Item
(例如分數值) 進行評分,還是會傳回 ItemResponse
。即使並未實際回應也一樣。然而,如果 Item
無法擴充,這個方法會從傳回的陣列中排除該項目。
Return 鍵
ItemResponse[]
:作答者表單中每個問題項目的回覆陣列。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getGradableResponseForItem(item)
取得特定商品表單回應中的商品回應。這個方法的運作方式與 getResponseForItem(item)
類似,但為允許缺少缺少的答案,如果對應的 Item
可以評分 (即使為分數值),則仍會傳回 ItemResponse
,即使沒有實際回應也一樣。不過,如果 Item
無法遷移,這個方法會傳回 null
。
參數
名稱 | 類型 | 說明 |
---|---|---|
item | Item |
Return 鍵
ItemResponse
:特定項目的回應;如果不存在,且此項目尚未評分,則為 null
getId()
取得表單回應的 ID。如果您尚未提交表單回應,這個方法會傳回 null
。
Return 鍵
String
:表單回應的 ID;如果尚未提交表單回應,則為 null
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItemResponses()
取得表單回應中的所有項目回應,其順序與表單中顯示的項目相同。如果表單回應不含特定 TextItem
、DateItem
、TimeItem
或 ParagraphTextItem
的回應,則針對該項目傳回的 ItemResponse
會包含空白字串做為回應。如果表單回應忽略任何其他項目類型的回應,此方法從傳回的陣列中排除該項目。
Return 鍵
ItemResponse[]
:作答者在表單中回答每個問題項目的陣列
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getRespondentEmail()
如果啟用 Form.setCollectEmail(collect)
設定,則取得提交者的電子郵件地址。
如果指令碼已建立,但尚未提交表單回應,這個方法會傳回 null
。
Return 鍵
String
:提交這項回覆者的電子郵件地址 (如果有的話);如果指令碼已建立回覆者,則為 null
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponseForItem(item)
getTimestamp()
取得表單回應的時間戳記。
如果指令碼已建立,但尚未提交表單回應,這個方法會傳回 null
。
Return 鍵
Date
:提交此回應的時間戳記;如果指令碼已建立此回應但尚未提交,則為 null
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
submit()
提交回應。如果已提交回應,則擲回指令碼的例外狀況。
Return 鍵
FormResponse
:新建立的回應儲存至表單的回應存放區
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
toPrefilledUrl()
根據這個表單回應中的答案,為表單預先填入答案網址。
Return 鍵
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
gradedResponse | ItemResponse |
Return 鍵
FormResponse
— 這個 FormResponse
用於鏈結
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
withItemResponse(response)
將特定項目回應新增至表單回應。這個方法僅適用於指令碼已建立但尚未提交的表單回應,不會影響已儲存的回應。如果針對同一商品多次呼叫這個方法,系統只會保留最後一個項目回應。
參數
名稱 | 類型 | 說明 |
---|---|---|
response | ItemResponse |
Return 鍵
FormResponse
— 這個 FormResponse
用於鏈結
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms