包含整体属性和项的表单。属性包括标题、设置以及回答的存储位置。项包括复选框或单选项等题目项,而布局项则是指分页符等内容。您可以通过 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
会导致所有题目中的所有评分选项都被移除。
测验设置仅在 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', ); // 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)
提交给定 FormResponse 的成绩。
如果您的代码包含 onFormSubmit
触发器,调用 submitGrades()
会触发 onFormSubmit
条件并导致无限循环。为防止出现无限循环,请在调用 submitGrades()
之前添加用于检查成绩是否已存在的代码。
参数
名称 | 类型 | 说明 |
---|---|---|
responses | FormResponse[] | 表单的所有回复的数组。 |
返回
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/forms.currentonly
-
https://www.googleapis.com/auth/forms