內含整體屬性和項目的表單。資源包括標題、設定和位置
的回應都會保留下來。項目包括核取方塊或圓形按鈕等問題項目,以及版面配置
項目是指分頁符號之類的項目您可以前往 FormApp
存取或建立表單。
// Open a form by ID and create a new spreadsheet. var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); var ss = SpreadsheetApp.create('Spreadsheet Name'); // Update form properties via chaining. form.setTitle('Form Name') .setDescription('Description of form') .setConfirmationMessage('Thanks for responding!') .setAllowResponseEdits(true) .setAcceptingResponses(false); // Update the form's response destination. form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
方法
內容詳盡的說明文件
addCheckboxGridItem()
附加新的問題項目 (以欄和列格線的形式呈現),讓 使用者會從一系列核取方塊序列中選取多個選項。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a checkbox grid item. const item = form.addCheckboxGridItem(); // Sets the title 'Where did you celebrate New Year's?' item.setTitle('Where did you celebrate New Year's?'); // Sets the grid's rows and columns. item.setRows(['New York', 'San Francisco', 'London']) .setColumns(['2014', '2015', '2016', '2017']);
回攻員
CheckboxGridItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addCheckboxItem()
附加新的問題項目,讓作答者勾選一或多個核取方塊,例如 還有選擇性的 [other]] 欄位。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a checkbox item. const item = form.addCheckboxItem(); // Sets the title of the checkbox item to 'Do you prefer cats or dogs?' item.setTitle('Do you prefer cats or dogs?'); // Sets the choices. item.setChoiceValues(['Cats', 'Dogs']);
回攻員
CheckboxItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addDateItem()
附加新的問題項目,可讓作答者指定日期。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a date item. const item = form.addDateItem(); // Sets the title to 'When were you born?' item.setTitle('When were you born?'); // Sets the description for the date item. item.setHelpText('Some helper text.');
回攻員
DateItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addDateTimeItem()
加入新的問題項目,方便作答者選擇日期和時間。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a question with date and time inputs. const item = form.addDateTimeItem(); // Sets the title to 'When were you born?' item.setTitle('When were you born?'); // Sets the question as required. item.setRequired(true);
回攻員
DateTimeItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addDurationItem()
加入新的問題項目,讓作答者指定時間長度。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a question with a duration input. const item = form.addDurationItem(); // Sets the title to 'How long can you hold your breath?' item.setTitle('How long can you hold your breath?'); // Sets the question as required. item.setRequired(true);
回攻員
DurationItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addGridItem()
附加新的問題項目 (以欄和列格線的形式呈現),讓 作答者需要從一系列圓形按鈕中,為每列選取一個選項。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a multiple choice grid. const item = form.addGridItem(); // Sets the title to 'Rate your interests.' item.setTitle('Rate your interests'); // Sets the grid's rows and columns. item.setRows(['Cars', 'Computers', 'Celebrities']) .setColumns(['Boring', 'So-so', 'Interesting']);
回攻員
GridItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addImageItem()
附加會顯示圖片的新版面配置項目。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds an image item. const item = form.addImageItem(); // Gets the Google icon to use as the image. const img = UrlFetchApp.fetch('https://fonts.gstatic.com/s/i/productlogos/googleg/v6/web-24dp/logo_googleg_color_1x_web_24dp.png'); // Sets the image, title, and description for the item. item.setTitle('Google icon').setHelpText('Google icon').setImage(img);
回攻員
ImageItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addListItem()
加入新的問題項目,讓作答者可從下拉式選單中選取一個選項 請參考閱讀清單,進一步瞭解 如何選擇 Kubeflow Pipelines SDK 或 TFX
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a dropdown list to the form. const item = form.addListItem(); // Sets the title to 'Do you prefer cats or dogs?' item.setTitle('Do you prefer cats or dogs?'); // Sets the description to 'This is description text...' item.setHelpText('This is description text...'); // Creates and adds choices to the dropdown list. item.setChoices([ item.createChoice('dog'), item.createChoice('cat') ]);
回攻員
ListItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addMultipleChoiceItem()
新增一個問題項目,讓作答者從電台清單中選取一個選項 按鈕或選用的「其他」] 欄位。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a multiple choice item to the form. const item = form.addMultipleChoiceItem(); // Sets the title. item.setTitle('What is your favorite ice cream flavor?'); // Creates some choice items. const vanilla = item.createChoice('vanilla'); const chocolate = item.createChoice('chocolate'); const strawberry = item.createChoice('strawberry'); // Sets the choices. item.setChoices([vanilla, chocolate, strawberry]);
回攻員
MultipleChoiceItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addPageBreakItem()
新增版面配置項目,用於標示頁面開頭。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds page break items to create a second and third page for the form. const pageTwo = form.addPageBreakItem(); const pageThree = form.addPageBreakItem(); // Sets the titles for the pages. pageTwo.setTitle('Page two'); pageThree.setTitle('Page three'); // Upon completion of the first page, sets the form to navigate to the third page. pageTwo.setGoToPage(pageThree); // Upon completion of the second page, sets the form to navigate back to the first page. pageThree.setGoToPage(FormApp.PageNavigationType.RESTART);
回攻員
PageBreakItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addParagraphTextItem()
新增一個問題項目,讓作答者可以輸入一段文字。
// Opens the form by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds the paragraph text item. const item = form.addParagraphTextItem(); // Sets the title to 'What is your address?' item.setTitle('What is your address?');
回攻員
ParagraphTextItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addScaleItem()
附加新的問題項目,讓作答者從編號中選擇一個選項 圓形按鈕序列。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds the scale item. const item = form.addScaleItem(); // Sets the title of the scale item to 'Choose a number.' item.setTitle('Choose a number'); // Sets the scale to 1-5. item.setBounds(1, 5); // Sets the label for the lower and upper bounds. item.setLabels('Lowest', 'Highest');
回攻員
ScaleItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addSectionHeaderItem()
附加新的版面配置項目,以視覺化的方式呈現區段的開頭。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds the section heading item. const item = form.addSectionHeaderItem(); // Sets the title to 'Title of new section.' item.setTitle('Title of new section'); // Sets the description. item.setHelpText('Description of new section');
回攻員
SectionHeaderItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addTextItem()
附加新的問題項目,讓作答者可以輸入一行文字。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a single-line text item. const item = form.addTextItem(); // Sets the title to 'What is your name?' item.setTitle('What is your name?');
回攻員
TextItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addTimeItem()
加入新的問題項目,讓作答者選擇時段。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a question with a time input. const item = form.addTimeItem(); // Sets the title to 'What time do you usually wake up in the morning?' item.setTitle('What time do you usually wake up in the morning?');
回攻員
TimeItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
addVideoItem()
附加會顯示影片的新版面配置項目。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Adds a video item. const item = form.addVideoItem(); // Sets the title, description, and video. item.setTitle('YouTube video') .setHelpText('Send content automatically via Google Sheets and Apps Script') .setVideoUrl('https://youtu.be/xxgQr-jSu9o'); // Sets the alignment to the center. item.setAlignment(FormApp.Alignment.CENTER);
回攻員
VideoItem
:新建立的項目。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
canEditResponse()
決定表單是否在提交回覆後顯示用來編輯回覆的連結。
無論這項設定為何,FormResponse.getEditResponseUrl()
方法都允許
具有表單編輯權限的指令碼作者,以產生可用於編輯表單的
回應。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Checks if the form displays a link to edit a response after submitting it. // The default is false. To let people edit their responses, use // form.setAllowResponseEdits(true). const edit = form.canEditResponse(); // If the form doesn't let people edit responses, logs false to the console. console.log(edit);
回攻員
Boolean
- 如果表單顯示「編輯回覆」訊息,值為 true
連結;如果符合,則false
但實際上並非如此
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
collectsEmail()
決定表單是否收集作答者電子郵件地址。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to not collect respondents' email addresses. form.setCollectEmail(false); // Checks whether the form collects respondents' email addresses and logs it to the console. const bool = form.collectsEmail(); console.log(bool);
回攻員
Boolean
- true
(如果表單會收集電子郵件地址);如果不是,則設為 false
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
createResponse()
為表單建立新回應。如要回答問題項目,請建立ItemResponse
,然後呼叫 FormResponse.withItemResponse(response)
,將此項目附加至這個表單回應。如要儲存組合的回應,請呼叫 FormResponse.submit()
。
回攻員
FormResponse
:新建立的表單回應。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
deleteAllResponses()
deleteItem(index)
從表單中的所有項目中刪除特定索引的項目。擲回指令碼例外狀況 則傳回的結果。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets all the items from the form. const items = form.getItems(); // Finds the index of a paragraph text item and deletes it by the item's index. const index = items.findIndex(item => item.getType() === FormApp.ItemType.PARAGRAPH_TEXT); if (index !== -1) { form.deleteItem(index); }
參數
名稱 | 類型 | 說明 |
---|---|---|
index | Integer | 表單中所有項目的項目索引。 |
擲回
Error
:如果指定索引中沒有任何項目
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
deleteItem(item)
刪除指定項目。如果項目已遭刪除,就會擲回指令碼例外狀況。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets all of the items from the form. const items = form.getItems(); // Finds a paragraph text item and deletes it. const item = items.find(item => item.getType() === FormApp.ItemType.PARAGRAPH_TEXT); if (item) { form.deleteItem(item); }
參數
名稱 | 類型 | 說明 |
---|---|---|
item | Item | 要刪除的項目。 |
擲回
Error
:如果表單中沒有該項目
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
deleteResponse(responseId)
從表單的回應儲存庫中刪除單一回應。這個方法不會刪除
儲存於外部回應目的地 (例如試算表) 的回應,但會移除
回覆。您可以使用 FormResponse.getId()
擷取回應 ID。
參數
名稱 | 類型 | 說明 |
---|---|---|
responseId | String | 要刪除的表單回應 ID。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getConfirmationMessage()
取得表單的確認訊息。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the confirmation message to display after someone submits the form. form.setConfirmationMessage('You successfully submitted the form.'); // Gets the confirmation message and logs it to the console. const message = form.getConfirmationMessage(); console.log(message);
回攻員
String
:表單的確認訊息。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getCustomClosedFormMessage()
取得在表單不接受回應或空白時顯示的自訂訊息 字串 (如果沒有設定自訂訊息)。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets a custom closed form message to display to the user when the form // no longer accepts responses. form.setCustomClosedFormMessage('The form is no longer accepting responses.'); // Gets the custom message set for the form and logs it to the console. const message = form.getCustomClosedFormMessage(); console.log(message);
回攻員
String
:在表單不接受回應時顯示的自訂訊息或空白
字串 (如果沒有設定自訂訊息)。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getDescription()
取得表單的說明。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form description. form.setDescription('This is the form description.'); // Gets the form description and logs it to the console. const description = form.getDescription(); console.log(description);
回攻員
String
:表單的說明。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getDestinationId()
取得表單回應目的地的 ID。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Creates a spreadsheet to use as the response destination. const ss = SpreadsheetApp.create('Test_Spreadsheet'); // Updates the form's response destination. form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); // Gets the ID of the form's response destination and logs it to the console. const destinationId = form.getDestinationId(); console.log(destinationId);
回攻員
String
:表單回應目的地的 ID。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getDestinationType()
取得表單回應目的地的類型。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc1234556/edit'); // Gets the type of the form's response destination and logs it to the console. const destinationType = form.getDestinationType().name(); console.log(destinationType);
回攻員
DestinationType
:表單回應目的地的類型。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getEditUrl()
取得可用於存取表單編輯模式的網址。
// Opens the form by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the URL that accesses the form's edit mode and logs it to the console. const url = form.getEditUrl(); console.log(url);
回攻員
String
:用於編輯表單的網址。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getEditors()
getId()
取得表單 ID。
// Opens the form by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the ID of the form and logs it to the console. const id = form.getId(); console.log(id);
回攻員
String
:表單 ID。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItemById(id)
取得具有指定 ID 的項目。如果 ID 與下列項目沒有相對應的項目,則傳回 null
該表單。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the ID of the first item on the form. const itemId = form.getItems()[0].getId(); // Gets the item from the ID. const item = form.getItemById(itemId); // Gets the name of the item type and logs it to the console. const type = item.getType().name(); console.log(type);
參數
名稱 | 類型 | 說明 |
---|---|---|
id | Integer | 項目的 ID。 |
回攻員
Item
:具有指定 ID 的項目;如果表單中沒有項目,則為 null
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItems()
取得表單中所有項目的陣列。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the list of items in the form. const items = form.getItems(); // Gets the type for each item and logs them to the console. const types = items.map((item) => item.getType().name()); console.log(types);
回攻員
Item[]
— 表單中所有項目的陣列。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getItems(itemType)
取得指定類型所有項目的陣列。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets a list of all checkbox items on the form. const items = form.getItems(FormApp.ItemType.CHECKBOX); // Gets the title of each checkbox item and logs them to the console. const checkboxItemsTitle = items.map((item) => item.asCheckboxItem().getTitle()); console.log(checkboxItemsTitle);
參數
名稱 | 類型 | 說明 |
---|---|---|
itemType | ItemType | 要擷取的項目類型。 |
回攻員
Item[]
— 該類型所有項目的陣列。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getPublishedUrl()
取得可用來回覆表單的網址。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the URL to respond to the form and logs it to the console. const url = form.getPublishedUrl(); console.log(url);
回攻員
String
:表單回應的網址。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponse(responseId)
根據回應 ID 取得單一表單回應。您可以從 FormResponse.getId()
擷取回應 ID。
參數
名稱 | 類型 | 說明 |
---|---|---|
responseId | String | 表單回應的 ID。 |
回攻員
FormResponse
:表單回覆。
擲回
Error
:如果回應不存在
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponses()
取得所有表單回應的陣列。
回攻員
FormResponse[]
— 所有表單回應的陣列。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getResponses(timestamp)
取得在指定日期和時間之後,所有表單回應的陣列。
參數
名稱 | 類型 | 說明 |
---|---|---|
timestamp | Date | 應傳回表單回應的最早日期和時間。 |
回攻員
FormResponse[]
:表單回應清單。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getShuffleQuestions()
決定表單每一頁的問題順序是否隨機。
回攻員
Boolean
— true
如果表單每一頁的問題順序都是隨機排序;
false
表示不需要。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getSummaryUrl()
取得可用於查看表單回應摘要的網址。除非將 setPublishingSummary(enabled)
設為 true
,否則只有具備
表單即可存取網址。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // Opens the form by its URL. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the URL to view a summary of the form's responses and logs it to the console. const url = form.getSummaryUrl(); console.log(url);
回攻員
String
:查看回應摘要的網址。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
getTitle()
取得表單的標題。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the title of the form to 'For_Testing.' form.setTitle('For_Testing'); // Gets the title of the form and logs it to the console. const title = form.getTitle(); console.log(title);
回攻員
String
:表單的標題。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
hasLimitOneResponsePerUser()
決定表單是否能讓每位作答者只能回應一次。如果值為 true
,則指令碼完全無法提交表單回應。
回攻員
Boolean
- 如果表單只允許每位作答者回覆一次,則為 true
;false
表示不需要。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
hasProgressBar()
決定表單是否顯示進度列。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // Opens the form by its URL. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Displays the progress bar on the form. form.setProgressBar(true); // Checks if the form displays a progress bar and logs it to the console. console.log(form.hasProgressBar());
回攻員
Boolean
— true
(如果表單顯示進度列);如果不是,則設為 false
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
hasRespondAgainLink()
決定表單在作答者後是否要顯示用來提交其他回覆的連結 填寫表單。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to display a link to submit another // response after someone submits the form. form.setShowLinkToRespondAgain(true); // Checks if the form displays a 'Submit another response' link and logs it to the console. console.log(form.hasRespondAgainLink());
回攻員
Boolean
- 如果表單顯示「提交其他回覆」訊息,則為 true
連結;如果符合,則false
但實際上並非如此
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
isAcceptingResponses()
判斷表單目前是否接受回應。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to accept responses. form.setAcceptingResponses(true); // Checks if the form is accepting responses or not and logs it to the console. const accepting = form.isAcceptingResponses(); console.log(accepting);
回攻員
Boolean
— true
表示表單接受回應;如果不是,則請false
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
isPublishingSummary()
決定表單是否顯示連結,方便在作答者後查看回應摘要 填寫表單。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to display a link to a summary of // the responses after someone submits the form. form.setPublishingSummary(true); // Checks if the form displays a "See previous responses" link and logs it to the console. const publishingLink = form.isPublishingSummary(); console.log(publishingLink);
回攻員
Boolean
- 如果表單顯示「查看先前的回覆」,則為 true
連結;如果符合,則false
但實際上並非如此
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
isQuiz()
判斷該表單是否為測驗。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form as a quiz. form.setIsQuiz(true); // Checks if the form is a quiz or not and logs it to the console. console.log(form.isQuiz());
回攻員
Boolean
— true
表示表單接受回應;如果不是,則請false
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
moveItem(from, to)
將位於指定索引的項目在表單中的所有項目移至另一個指定的索引。擲回
to
索引超出邊界時會出現指令碼例外狀況。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Moves the first item to be the last item. form.moveItem(0, form.getItems().length - 1);
參數
名稱 | 類型 | 說明 |
---|---|---|
from | Integer | 表單中所有項目目前的索引。 |
to | Integer | 表單中所有項目的新索引。 |
回攻員
Item
:已移動的項目。
擲回
Error
:如果其中一個索引超出邊界,
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
moveItem(item, toIndex)
將指定項目移至表單中所有項目的指定索引。擲回指令碼 例外狀況。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Gets the first item. const item = form.getItems()[0]; // Moves the item to be the last item. form.moveItem(item, form.getItems().length - 1);
參數
名稱 | 類型 | 說明 |
---|---|---|
item | Item | 要移動的項目。 |
toIndex | Integer | 表單中所有項目的新索引。 |
回攻員
Item
:已移動的項目。
擲回
Error
:如果索引超出邊界。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
removeDestination()
將表單與目前的回應目的地取消連結。已取消連結的舊目的地仍然存在 保留先前所有回覆的副本。所有表單,包括沒有 已明確設定目的地,儲存 表單回應儲存庫中的回應副本。如果表單目前沒有 回應目的地,此方法沒有作用。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Opens a spreadsheet to use for the response destination. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Updates the form's response destination to the spreadsheet. form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId()); // Unlinks the form from the spreadsheet. form.removeDestination();
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
removeEditor(emailAddress)
將特定使用者從 Form
的編輯者清單中移除。這個方法
如果使用者所屬的類別含有以下使用者類別,則禁止使用者存取 Form
一般存取權,例如 Form
已與整個使用者共用
網域,或是 Form
位於使用者可以存取的共用雲端硬碟中。
針對雲端硬碟檔案,這麼做也會將使用者從檢視者清單中移除。
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddress | String | 要移除的使用者電子郵件地址。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
removeEditor(user)
requiresLogin()
決定表單是否要求作答者登入相同網域中的帳戶或 再透過子網域回應
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Checks if the form requires respondents to log in to a Google Workspace account // before responding and logs it to the console. const login = form.requiresLogin(); console.log(login);
回攻員
Boolean
— true
(如果表單要求使用者登入);如果不是,則設為 false
。
setAcceptingResponses(enabled)
設定表單目前是否接受回應。新表單的預設值是 true
。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to accept responses. form.setAcceptingResponses(true); // Checks whether the form is accepting responses or not and logs it to the console. console.log(form.isAcceptingResponses());
參數
名稱 | 類型 | 說明 |
---|---|---|
enabled | Boolean | true (如果表單應接受回應);false 。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setAllowResponseEdits(enabled)
設定表單是否在提交回應後顯示連結,讓您編輯回覆內容。預設值
新表單為false
。
無論這項設定為何,FormResponse.getEditResponseUrl()
方法都允許
具有表單編輯權限的指令碼作者,才能產生可用於編輯表單
回應。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Shows "Edit your response" link after someone submits the form. form.setAllowResponseEdits(true); // Checks whether the option to edit the form after a user submits it is set to true or not // and logs it to the console. console.log(form.canEditResponse());
參數
名稱 | 類型 | 說明 |
---|---|---|
enabled | Boolean | true :如果表單應顯示「Edit your response」連結;false 表示不滿意。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setCollectEmail(collect)
設定表單是否收集作答者電子郵件地址。新表單的預設方式為
false
。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to collect respondents' email addresses. form.setCollectEmail(true); // Checks whether the value is set to true or false and logs it to the console. const collect = form.collectsEmail(); console.log(collect);
參數
名稱 | 類型 | 說明 |
---|---|---|
collect | Boolean | true (如果表單必須收集電子郵件地址);如果是,請false
但實際上並非如此 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setConfirmationMessage(message)
設定表單的確認訊息。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets a custom confirmation message to display after someone submits the form. form.setConfirmationMessage('Your form has been successfully submitted.'); // Gets the confirmation message set for the form and logs it to the console. const message = form.getConfirmationMessage(); console.log(message);
參數
名稱 | 類型 | 說明 |
---|---|---|
message | String | 表單的新確認訊息。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setCustomClosedFormMessage(message)
設定表單不接受回應時要顯示的訊息。如果沒有設定訊息, 表單使用預設訊息。
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form to not accept responses. form.setAcceptingResponses(false); // Sets a custom closed form message to display to the user. form.setCustomClosedFormMessage('The form is no longer accepting responses.'); // Gets the custom message set for the form and logs it to the console. const message = form.getCustomClosedFormMessage(); console.log(message);
參數
名稱 | 類型 | 說明 |
---|---|---|
message | String | 表單不接受回應時顯示的訊息。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setDescription(description)
setDestination(type, id)
設定表單回應的儲存目的地。所有表單,包括: 預先設定目的地,請將回應副本儲存在表單的 回應存放區。
參數
名稱 | 類型 | 說明 |
---|---|---|
type | DestinationType | 表單回應目的地的類型。 |
id | String | 表單回應目的地的 ID。 |
回攻員
擲回
Error
:如果指定目的地 ID 無效
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setIsQuiz(enabled)
設定表單是否為測驗。新表單的預設值是 false
。
已評分的問題只適用於測驗,因此將這項設定設為 false
即可讓所有問題
。
只有新版表單 UI 提供測驗設定。填寫測驗選項。 成功運用新的使用者介面
// Opens the Forms file by its URL. If you created your script from within a // Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Makes the form a quiz. form.setIsQuiz(true); // Checks whether the form is a quiz or not and logs it to the console. console.log(form.isQuiz());
參數
名稱 | 類型 | 說明 |
---|---|---|
enabled | Boolean | 如果表單應啟用測驗功能,則為 true ;如果符合條件,則設為 false
而不是 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setLimitOneResponsePerUser(enabled)
setProgressBar(enabled)
設定表單是否包含進度列。新表單的預設值是 false
。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Displays the progress bar on the form. form.setProgressBar(true); // Checks whether the form has a progress bar and logs it to the console. console.log(form.hasProgressBar());
參數
名稱 | 類型 | 說明 |
---|---|---|
enabled | Boolean | 如果表單顯示進度列,則為 true ;如果不是,false 。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setPublishingSummary(enabled)
setRequireLogin(requireLogin)
設定表單作答者必須登入相同網域或
再進行回應。除非網域內,否則新表單的預設值是 false
系統管理員變更預設設定。
這項功能僅適用於 Google Workspace 使用者建立的表單。其他其他使用者 不可登入的 Google 帳戶類型。
// Opens the Forms file by its URL. If you created your script from within // a Google Forms file, you can use FormApp.getActiveForm() instead. // TODO(developer): Replace the URL with your own. const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit'); // Sets the form so that users must log in to their Google Workspace account. form.setRequireLogin(true); // Checks whether the form requires login or not and logs it to the console. console.log(form.requiresLogin());
參數
名稱 | 類型 | 說明 |
---|---|---|
requireLogin | Boolean | 如果表單要求使用者登入,則為 true ;如果是,請false
但實際上並非如此 |
回攻員
Form
:目前的格式 (用於鏈結)。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
setShowLinkToRespondAgain(enabled)
setShuffleQuestions(shuffle)
setTitle(title)
shortenFormUrl(url)
將表單的長網址轉換為短網址。如果長網址沒有 資料。
參數
名稱 | 類型 | 說明 |
---|---|---|
url | String | 要縮短的網址。 |
回攻員
String
:採用 http://goo.gl/forms/1234
格式的網址。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
submitGrades(responses)
提交特定 FormResponse 的成績。
如果程式碼包含 onFormSubmit
觸發條件,請呼叫 submitGrades()
觸發 onFormSubmit
條件並造成無限迴圈。為了避免
無限迴圈,在呼叫 submitGrades()
前新增程式碼,檢查成績是否已存在。
參數
名稱 | 類型 | 說明 |
---|---|---|
responses | FormResponse[] | 所有表單回應的陣列。 |
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms