Class Form

表单

包含整体属性和项目的表单。属性包括标题、设置和响应的存储位置。项包括复选框或单选项等问题项,而布局项是指分页符等项。您可以通过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()CheckboxGridItem附加一个新的问题项,表示为由列和行组成的网格,使回复者能够从一系列复选框中逐行选择多个选项。
addCheckboxItem()CheckboxItem附加新的问题项,使回复者能够选择一个或多个复选框,以及可选的“其他”字段。
addDateItem()DateItem附加新的问题项,以便受访者指定日期。
addDateTimeItem()DateTimeItem附加新的问题项,以便回复者指明日期和时间。
addDurationItem()DurationItem附加新的问题项,以便受访者指明答题时长。
addEditor(emailAddress)Form将指定用户添加到 Form 的编辑者列表中。
addEditor(user)Form将指定用户添加到 Form 的编辑者列表中。
addEditors(emailAddresses)Form将给定的用户数组添加到 Form 的编辑者列表中。
addGridItem()GridItem附加一个新问题项,表示为由列和行组成的网格,使受访者能够在一行中从一系列单选按钮中选择一个选项。
addImageItem()ImageItem用于附加显示图片的新布局项。
addListItem()ListItem附加一个新的问题项,使受访者能够从下拉列表中选择一个选项。
addMultipleChoiceItem()MultipleChoiceItem添加新的问题项,使受访者能够从单选按钮列表或可选的“其他”字段中选择一个选项。
addPageBreakItem()PageBreakItem用于添加新的布局项,用于标记页面的开头。
addParagraphTextItem()ParagraphTextItem添加新的问题项,使回复者能够输入一段文本。
addScaleItem()ScaleItem附加新的问题项,使受访者能够从一系列有编号的单选按钮中选择一个选项。
addSectionHeaderItem()SectionHeaderItem用于附加新的布局项,以视觉方式指示某个部分的开头。
addTextItem()TextItem附加新的问题项,允许回复者输入单行文本。
addTimeItem()TimeItem附加新的问题项,让受访者能够指明一天中的时间。
addVideoItem()VideoItem用于附加显示视频的新布局项。
canEditResponse()Boolean确定表单在提交后是否显示用于修改回复的链接。
collectsEmail()Boolean确定表单是否收集回复者的电子邮件地址。
createResponse()FormResponse创建对表单的新回复。
deleteAllResponses()Form从表单的响应存储区中删除所有已提交的响应。
deleteItem(index)void从表单的所有项中删除位于指定索引处的项。
deleteItem(item)void删除给定项。
deleteResponse(responseId)Form从表单的响应存储区中删除单个回复。
getConfirmationMessage()String获取表单的确认消息。
getCustomClosedFormMessage()String获取在表单不接受响应时显示的自定义消息,或在未设置自定义消息时获取空字符串。
getDescription()String获取表单的说明。
getDestinationId()String获取表单的响应目标的 ID。
getDestinationType()DestinationType获取表单响应目标的类型。
getEditUrl()String获取可用于访问表单编辑模式的网址。
getEditors()User[]获取此 Form 的编辑者列表。
getId()String获取表单的 ID。
getItemById(id)Item获取具有指定 ID 的项。
getItems()Item[]获取表单中所有项的数组。
getItems(itemType)Item[]获取由指定类型的所有项组成的数组。
getPublishedUrl()String获取可用于回复表单的网址。
getResponse(responseId)FormResponse根据回复 ID 获取单个表单回复。
getResponses()FormResponse[]获取表单的所有回复的数组。
getResponses(timestamp)FormResponse[]获取指定日期和时间之后的所有表单回复的数组。
getShuffleQuestions()Boolean确定表单每个页面上的问题顺序是否随机。
getSummaryUrl()String获取可用于查看表单响应摘要的网址。
getTitle()String获取表单的标题。
hasLimitOneResponsePerUser()Boolean确定表单是否只允许每个受访者有一个回复。
hasProgressBar()Boolean确定表单是否显示进度条。
hasRespondAgainLink()Boolean确定在回复者填写表单后表单是否显示用于提交另一份回复的链接。
isAcceptingResponses()Boolean确定表单当前是否接受回复。
isPublishingSummary()Boolean确定在回复者填写表单后表单是否显示用于查看回复摘要的链接。
isQuiz()Boolean确定表单是否为测验。
moveItem(from, to)Item将表单中所有项中位于指定索引处的项移到另一个指定索引处。
moveItem(item, toIndex)Item将给定项移到表单所有项中的指定索引。
removeDestination()Form取消表单与其当前回复目标的关联。
removeEditor(emailAddress)FormForm 的编辑者列表中移除指定用户。
removeEditor(user)FormForm 的编辑者列表中移除指定用户。
requiresLogin()Boolean确定表单是否要求受访者必须登录同一网域或子网域中的帐号才能回复。
setAcceptingResponses(enabled)Form设置表单当前是否接受回复。
setAllowResponseEdits(enabled)Form设置表单在提交后是否显示用于修改回复的链接。
setCollectEmail(collect)Form设置表单是否收集回复者的电子邮件地址。
setConfirmationMessage(message)Form设置表单的确认消息。
setCustomClosedFormMessage(message)Form设置在表单不接受回复时显示的消息。
setDescription(description)Form设置表单的说明。
setDestination(type, id)Form设置保存表单回复的目标位置。
setIsQuiz(enabled)Form设置表单是否为测验。
setLimitOneResponsePerUser(enabled)Form设置表单是否只允许每个回复者发送一条回复。
setProgressBar(enabled)Form设置表单是否有进度条。
setPublishingSummary(enabled)Form设置在回复者提交表单后,表单是否显示用于查看回复摘要的链接。
setRequireLogin(requireLogin)Form设置表单是否要求受访者必须登录同一网域或子网域中的账号才能回复。
setShowLinkToRespondAgain(enabled)Form设置在回复者填好表单后,表单是否显示用于提交另一份回复的链接。
setShuffleQuestions(shuffle)Form设置表单各页面上的问题顺序是否随机。
setTitle(title)Form设置表单的标题。
shortenFormUrl(url)String将表单的长网址转换为短网址。
submitGrades(responses)Form提交指定 FormResponse 的成绩。

详细文档

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()

附加新的问题项,使回复者能够选择一个或多个复选框,以及可选的“其他”字段。

// 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)

将指定用户添加到 Form 的编辑者列表中。如果用户已在观看者列表中,则此方法会将用户从观看者列表中提升出来。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

addEditor(user)

将指定用户添加到 Form 的编辑者列表中。如果用户已在观看者列表中,则此方法会将用户从观看者列表中提升出来。

参数

名称类型说明
userUser表示要添加的用户。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

addEditors(emailAddresses)

将给定的用户数组添加到 Form 的编辑者列表中。如果有任何用户已位于查看者列表中,则此方法会将这些用户从查看者列表中移出。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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()

从表单的响应存储区中删除所有已提交的响应。此方法不会删除存储在外部响应目标(如电子表格)中的响应副本,但会清除表单的摘要视图。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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);
}

参数

名称类型说明
indexInteger表单项中所有项的索引。

抛出

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);
}

参数

名称类型说明
itemItem要删除的项。

抛出

Error - 如果表单中不存在相应项

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

deleteResponse(responseId)

从表单的响应存储区中删除单个回复。此方法不会删除存储在外部回复目标(如电子表格)中的回复的副本,但会从表单的摘要视图中移除回复。响应 ID 可通过 FormResponse.getId() 检索。

参数

名称类型说明
responseIdString要删除的表单回复的 ID。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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()

获取此 Form 的编辑者列表。

弃踢回攻

User[] - 具有修改权限的用户数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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);

参数

名称类型说明
idInteger商品的 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);

参数

名称类型说明
itemTypeItemType要检索的项目的类型。

弃踢回攻

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。

参数

名称类型说明
responseIdString表单回复的 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)

获取指定日期和时间之后的所有表单回复的数组。

参数

名称类型说明
timestampDate应返回表单回复的最早日期和时间。

弃踢回攻

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

确定在受访者填完表单后表单是否显示用于提交另一份回复的链接。

// 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);

参数

名称类型说明
fromInteger表单项中所有项的当前索引。
toInteger表单中的所有项中的项的新索引。

弃踢回攻

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);

参数

名称类型说明
itemItem要移动的内容。
toIndexInteger表单中的所有项中的项的新索引。

弃踢回攻

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();

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

removeEditor(emailAddress)

Form 的编辑者列表中移除指定用户。如果用户属于具有常规访问权限的用户类(例如,与用户的整个网域共享 Form,或者 Form 位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Form

对于云端硬盘文件,此操作会同时将用户从查看者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

removeEditor(user)

Form 的编辑者列表中移除指定用户。如果用户属于具有常规访问权限的用户类(例如,与用户的整个网域共享 Form,或者 Form 位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Form

对于云端硬盘文件,此操作会同时将用户从查看者列表中移除。

参数

名称类型说明
userUser表示要移除的用户。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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());

参数

名称类型说明
enabledBoolean如果表单应接受响应,则为 true;如果不应接受,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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());

参数

名称类型说明
enabledBoolean如果表单应显示“编辑您的回复”链接,则为 true;如果不需要,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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);

参数

名称类型说明
collectBoolean如果表单应收集电子邮件地址,则为 true;如果不需要,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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);

参数

名称类型说明
messageString表单的新确认消息。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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);

参数

名称类型说明
messageString在表单不接受回复时显示的消息。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setDescription(description)

设置表单的说明。

参数

名称类型说明
descriptionString表单的新说明。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setDestination(type, id)

设置保存表单回复的目标位置。所有表单(包括未明确设置目标的表单)都会在表单的响应存储区中保存响应的副本

参数

名称类型说明
typeDestinationType表单的响应目标的类型。
idString表单的响应目标 ID。

弃踢回攻

Form - 此 Form,用于链接。

抛出

Error - 如果指定的目标帐号 ID 无效

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setIsQuiz(enabled)

设置表单是否为测验。新表单的默认值为 false

已评分的题目只能在测验中使用,因此将此设置为 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');

// 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());

参数

名称类型说明
enabledBoolean如果应为表单启用测验功能,则为 true;如果不应启用,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setLimitOneResponsePerUser(enabled)

设置表单是否只允许每个回复者发送一条回复。新表单的默认值为 false。如果该值设置为 true,则脚本根本无法提交表单回复。

参数

名称类型说明
enabledBoolean如果表单只允许每位受访者有一个回复,则为 true;如果不允许,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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());

参数

名称类型说明
enabledBoolean如果表单显示进度条,则为 true;如果未显示进度条,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setPublishingSummary(enabled)

设置在回复者提交表单后,表单是否显示用于查看回复摘要的链接。新表单的默认值为 false

参数

名称类型说明
enabledBoolean如果表单应显示“查看以前的回复”链接,则为 true;如果不需要,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

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());

参数

名称类型说明
requireLoginBoolean如果表单要求用户登录,则为 true;如果不需要,则为 false

弃踢回攻

Form - 当前形式(用于链接)。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setShowLinkToRespondAgain(enabled)

设置在回复者填好表单后,表单是否显示用于提交另一份回复的链接。新表单的默认值为 true

参数

名称类型说明
enabledBoolean如果表单应显示“再提交一个回复”链接,则为 true;如果不需要,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setShuffleQuestions(shuffle)

设置表单各页面上的问题顺序是否随机。

参数

名称类型说明
shuffleBoolean如果表单每个页面上的问题顺序应该是随机的,则为 true;如果不需要,则为 false

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

setTitle(title)

设置表单的标题。

参数

名称类型说明
titleString表单的新标题。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms

shortenFormUrl(url)

将表单的长网址转换为短网址。如果长网址不属于 Google 表单,则会抛出异常。

参数

名称类型说明
urlString要缩短的网址。

弃踢回攻

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() 之前检查成绩是否已存在的代码。

参数

名称类型说明
responsesFormResponse[]一个数组,包含表单的所有回复。

弃踢回攻

Form - 此 Form,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/forms.currentonly
  • https://www.googleapis.com/auth/forms