包含整體房源和項目的表單。屬性包括標題、設定和回覆儲存位置。項目包括核取方塊或圓形按鈕等問題項目,而版面配置項目則是指分頁等項目。您可以透過 FormApp
存取或建立表單。
// Open a form by ID and create a new spreadsheet. const form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz'); const 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(); 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()
附加新的問題項目,讓作答者選取一或多個核取方塊,以及選用的「其他」欄位。
// 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()
附加新的問題項目,讓作答者從下拉式清單中選取一個選項。
// 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)
removeEditor(user)
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 :表單應顯示「編輯回覆」連結;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;將表單設為測驗時,系統會選擇使用新版 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)
setShowLinkToRespondAgain(enabled)
setShuffleQuestions(shuffle)
setTitle(title)
shortenFormUrl(url)
將表單的長網址轉換為短網址。如果長網址不屬於 Google 表單,則會擲回例外狀況。
參數
名稱 | 類型 | 說明 |
---|---|---|
url | String | 要縮短的網址。 |
回攻員
String
:網址格式為 http://goo.gl/forms/1234
。
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms
submitGrades(responses)
提交指定表單回覆的成績。
如果程式碼包含 onFormSubmit
觸發條件,呼叫 submitGrades()
會觸發 onFormSubmit
條件,並導致無限迴圈。為避免無限迴圈,請新增程式碼,在呼叫 submitGrades()
之前檢查成績是否已存在。
參數
名稱 | 類型 | 說明 |
---|---|---|
responses | FormResponse[] | 包含所有表單回覆的陣列。 |
回攻員
授權
使用這個方法的腳本需要具備下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms