表單的整體回覆。Form
可用於三種用途:存取受訪者提交的答案 (請參閱 get
)、以程式輔助方式提交表單回覆 (請參閱 with
和 submit()
),以及產生表單網址,並使用提供的答案預先填入欄位。Form
可從 Form
建立或存取。
// 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(), ); } }
方法
方法 | 傳回類型 | 簡短說明 |
---|---|---|
get | String | 產生可用於編輯已提交回覆的網址。 |
get | Item | 以表單中項目的顯示順序,取得表單回覆中包含的所有項目回覆。 |
get | Item | 取得指定項目表單回覆中包含的項目回覆。 |
get | String | 取得表單回覆的 ID。 |
get | Item | 以表單中項目的顯示順序,取得表單回覆中包含的所有項目回覆。 |
get | String | 如果啟用 Form.setCollectEmail(collect) 設定,系統會取得提交回覆者的電子郵件地址。 |
get | Item | 取得指定項目在這個表單回覆中包含的項目回覆。 |
get | Date | 取得表單回覆提交的時間戳記。 |
submit() | Form | 提交回覆。 |
to | String | 根據這份表單回覆中的答案,產生預先填入答案的表單網址。 |
with | Form | 將指定項目回覆的分數新增至表單回覆。 |
with | Form | 將指定項目回覆新增至表單回覆。 |
內容詳盡的說明文件
get Edit Response 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
:用於變更已提交回覆的網址。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Gradable Item Responses()
以表單中項目的顯示順序,取得表單回覆中包含的所有項目回覆。這個方法與 get
類似,但為了評分缺少的答案,即使沒有實際的回應,如果對應的 Item
可以評分 (即有分數值),仍會傳回 Item
。不過,如果 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()}`); } }
回攻員
Item
:針對表單中每個可讓受訪者獲得分數的問題項目,提供回覆的陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Gradable Response For Item(item)
取得指定項目表單回覆中包含的項目回覆。這個方法與 get
類似,但為了允許評分缺少的答案,即使沒有實際的回應,如果對應的 Item
可以評分 (即有分數值),仍會傳回 Item
。不過,如果 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()}`, ); }
參數
名稱 | 類型 | 說明 |
---|---|---|
item | Item |
回攻員
Item
:特定項目的回應,或 null
(如果不存在且項目未評分)。
get Id()
取得表單回覆的 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
get Item Responses()
以表單中項目的顯示順序,取得表單回覆中包含的所有項目回覆。如果表單回應不包含特定 Text
、Date
、Time
或 Paragraph
的回應,則為該項目傳回的 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 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()}'`); } }
回攻員
Item
- 針對表單中受訪者提供答案的每個問題項目,回覆的陣列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
get Respondent Email()
如果已啟用 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
get Response For Item(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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
item | Item |
回攻員
Item
:指定項目的回應,如果不存在則為 null
。
get Timestamp()
取得表單回覆提交時間戳記。
如果指令碼已建立表單回覆,但尚未提交,這個方法會傳回 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();
回攻員
Form
:新建立的回覆已儲存至表單的回覆儲存空間。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
to Prefilled 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
:含有預填答案的表單網址。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
with Item Grade(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);
參數
名稱 | 類型 | 說明 |
---|---|---|
graded | Item |
回攻員
Form
- this Form
,用於鏈結
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
with Item Response(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();
參數
名稱 | 類型 | 說明 |
---|---|---|
response | Item |
回攻員
Form
- This Form
,用於鏈結。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms