Class Form

表單

包含整體屬性和項目的表單。屬性包括標題、設定和回應儲存位置。項目包括核取方塊或圓形按鈕等問題項目,而版面配置項目則是指分頁符等項目。您可以透過 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()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新增問題項目,讓受訪者輸入一段文字。
addPublishedReader(emailAddress)Form將指定使用者新增至 Form 的回覆者清單。
addPublishedReader(user)Form將指定使用者新增至 Form 的回覆者清單。
addPublishedReaders(emailAddresses)Form將指定的使用者陣列新增至 Form 的回覆者清單。
addRatingItem()RatingItem附加新問題項目,讓受訪者提供評分。
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|null取得具有指定 ID 的項目。
getItems()Item[]取得表單中所有項目的陣列。
getItems(itemType)Item[]取得指定類型所有項目的陣列。
getPublishedReaders()User[]取得這個 Form 的回覆者清單。
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判斷表單目前是否接受回覆。
isPublished()Boolean決定表單是否發布。
isPublishingSummary()Boolean決定表單是否要在作答者填寫完表單後,顯示查看回覆摘要的連結。
isQuiz()Boolean判斷表單是否為測驗。
moveItem(from, to)Item將表單中指定索引的項目移至另一個指定索引。
moveItem(item, toIndex)Item將指定項目移至表單中所有項目的指定索引。
removeDestination()Form取消表單與目前回應目的地的連結。
removeEditor(emailAddress)FormForm 的編輯者清單中移除指定使用者。
removeEditor(user)FormForm 的編輯者清單中移除指定使用者。
removePublishedReader(emailAddress)FormForm 的回覆者清單中移除指定使用者。
removePublishedReader(user)FormForm 的回覆者清單中移除指定使用者。
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設定表單是否顯示進度列。
setPublished(enabled)Form設定表單是否已發布。
setPublishingSummary(enabled)Form設定表單是否要在受訪者提交表單後,顯示查看回覆摘要的連結。
setShowLinkToRespondAgain(enabled)Form設定表單是否要在受訪者填寫完表單後,顯示提交其他回覆的連結。
setShuffleQuestions(shuffle)Form設定表單每頁的問題順序是否隨機。
setTitle(title)Form設定表單標題。
shortenFormUrl(url)String將表單的長網址轉換為短網址。
submitGrades(responses)Form為指定 FormResponses 提交成績。
supportsAdvancedResponderPermissions()Boolean判斷表單是否支援發布。

內容詳盡的說明文件

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)

將指定使用者新增至 Form 的編輯者清單。如果使用者已在觀眾或回覆者名單中,這個方法會將使用者從名單中移除。

// 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 editor to the form.
// TODO(developer): replace the emailAddress.
form.addEditor('editor@uni.edu');

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

addEditor(user)

將指定使用者新增至 Form 的編輯者清單。如果使用者已在觀眾或回覆者名單中,這個方法會將使用者從名單中移除。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const oldForm = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Gets the editors from the old form.
const users = oldForm.getEditors();

// Creates a new form.
const newForm = FormApp.create('New form');

// Adds the editors to a new form.
users.forEach(user => newForm.addEditor(user));

參數

名稱類型說明
userUser要新增的使用者代表。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

addEditors(emailAddresses)

將指定的使用者陣列新增至 Form 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將他們從觀眾名單中升級。

// 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 editors to the form.
// TODO(developer): replace the emailAddress.
form.addPublishedReaders(['editor1@uni.edu', 'editor2@uni.edu']);

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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

addPublishedReader(emailAddress)

將指定使用者新增至 Form 的回覆者清單。如果使用者已在編輯者或檢視者名單中,這個方法不會有任何作用。

// 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 responder to the form.
// TODO(developer): replace the emailAddress.
form.addPublishedReader('responder@uni.edu');

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

addPublishedReader(user)

將指定使用者新增至 Form 的回覆者清單。如果使用者已在編輯者或檢視者名單中,這個方法不會有任何作用。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const oldForm = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Gets the responders from the old form.
const users = oldForm.getPublishedReaders();

// Creates a new form.
const newForm = FormApp.create('New form');

// Adds the responders to a new form.
users.forEach(user => newForm.addPublishedReader(user));

參數

名稱類型說明
userUser要新增的使用者代表。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

addPublishedReaders(emailAddresses)

將指定的使用者陣列新增至 Form 的回覆者清單。如果使用者已在編輯者或檢視者清單中,這個方法不會有任何作用。

// 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 responders to the form.
// TODO(developer): replace the emailAddress.
form.addPublishedReaders(['responder1@uni.edu', 'responder2@uni.edu']);

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

addRatingItem()

附加新的問題項目,讓受訪者提供評分。

// 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 rating item.
const item = form.addRatingItem();

回攻員

RatingItem:新建立的項目。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

Appends a new layout item that displays a video.

// 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 — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

從表單的回覆儲存空間中刪除單一回覆。這個方法不會刪除儲存在外部回覆目的地 (例如試算表) 的回覆副本,但會從表單的摘要檢視畫面中移除回覆。您可以使用 FormResponse.getId() 擷取回覆 ID。

參數

名稱類型說明
responseIdString要刪除的表單回覆 ID。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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|null - 具有指定 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

getPublishedReaders()

取得這個 Form 的回覆者清單。

// 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 responders for the form.
const users = form.getPublishedReaders();
users.forEach(user => console.log(user.getEmail()));

回攻員

User[]:具有回覆者權限的使用者陣列。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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,指令碼就完全無法提交表單回覆。

回攻員

Booleantrue 如果表單只允許每位受訪者提交一次回覆;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 (如果表單顯示進度列);false (如果表單未顯示進度列)。true

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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

isPublished()

決定是否發布表單。

這項功能僅適用於支援發布的表單。使用 supportsAdvancedResponderPermissions() 檢查表單是否支援發布。

// 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 whether the form is published or not and logs it to the console.
console.log(form.isPublished());

回攻員

Boolean:如果表單已發布,則為 true;如果表單尚未發布,則為 false

擲回

Error:在不支援的表單上呼叫時。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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 — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

removeEditor(emailAddress)

Form 的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別 (例如 Form 與使用者的整個網域共用,或 Form 位於使用者可存取的共用雲端硬碟中),這個方法不會禁止使用者存取 Form

如果是雲端硬碟檔案,系統也會從檢視者和回覆者清單中移除使用者。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Removes the editor from the form.
// TODO(developer): replace the emailAddress.
form.removeEditor('editor@uni.edu');

參數

名稱類型說明
emailAddressString要移除的使用者電子郵件地址。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

removeEditor(user)

Form 的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別 (例如 Form 與使用者的整個網域共用,或 Form 位於使用者可存取的共用雲端硬碟中),這個方法不會禁止使用者存取 Form

如果是雲端硬碟檔案,系統也會從檢視者清單中移除使用者。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const form1 = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Gets the editors from the form.
const users = form1.getEditors();

// Opens another form.
// TODO(developer): Replace the URL with your own.
const form2 = FormApp.openByUrl('https://docs.google.com/forms/d/efg123456/edit');

// Removes editors from the form.
users.forEach(user => form2.removeEditor(user));

參數

名稱類型說明
userUser要移除的使用者代表。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

removePublishedReader(emailAddress)

Form 的回覆者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別 (例如 Form 與使用者的整個網域共用,或是 Form 位於使用者可存取的共用雲端硬碟中),這個方法不會禁止使用者存取 Form

如果是雲端硬碟檔案,系統也會從檢視者和編輯者清單中移除該使用者。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const form = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Removes the responders from the form.
// TODO(developer): replace the emailAddress.
form.removePublishedReader('responder1@uni.edu');

參數

名稱類型說明
emailAddressString要移除的使用者電子郵件地址。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

removePublishedReader(user)

Form 的回覆者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別 (例如 Form 與使用者的整個網域共用,或是 Form 位於使用者可存取的共用雲端硬碟中),這個方法不會禁止使用者存取 Form

如果是雲端硬碟檔案,系統也會從檢視者和編輯者清單中移除該使用者。

// Opens the Forms file by its URL.
// TODO(developer): Replace the URL with your own.
const form1 = FormApp.openByUrl('https://docs.google.com/forms/d/abc123456/edit');

// Gets the responders from the form.
const users = form1.getPublishedReaders();

// Opens another form.
// TODO(developer): Replace the URL with your own.
const form2 = FormApp.openByUrl('https://docs.google.com/forms/d/efg123456/edit');

// Removes responders from the form.
users.forEach(user => form2.removePublishedReader(user));

參數

名稱類型說明
userUser要移除的使用者代表。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

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

參數

名稱類型說明
enabledBooleantrue 表單應接受回覆;false 表單不應接受回覆。

回攻員

Form — This Form, for chaining.

擲回

Error:如果未發布的表單已啟用這項功能。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

參數

名稱類型說明
enabledBooleantrue 表單是否應顯示「編輯回覆」連結;false 否則。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

參數

名稱類型說明
collectBooleantrue (表單應收集電子郵件地址);false (表單不應收集電子郵件地址)。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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 — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

  • 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 — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setDescription(description)

設定表單說明。

參數

名稱類型說明
descriptionString表單的新說明。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setDestination(type, id)

設定表單回覆的儲存位置。所有表單 (包括未明確設定目的地的表單) 都會將回應副本儲存在表單的回應儲存空間

參數

名稱類型說明
typeDestinationType表單回覆目標的類型。
idString表單回覆目標的 ID。

回攻員

Form — This Form, for chaining.

擲回

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

參數

名稱類型說明
enabledBooleantrue 表示應為表單啟用測驗功能;false 表示不應啟用。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setLimitOneResponsePerUser(enabled)

設定表單是否只允許每位受訪者提交一次回覆。新表單的預設值為 false。如果將值設為 true,指令碼就完全無法提交表單回覆。

參數

名稱類型說明
enabledBooleantrue如果表單應只允許每位受訪者提交一次回覆;false如果不是。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

參數

名稱類型說明
enabledBooleantrue 如果表單顯示進度列;false 如果表單未顯示進度列。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setPublished(enabled)

設定表單是否已發布。新表單的預設值為 true

這項功能僅適用於支援發布的表單。使用 supportsAdvancedResponderPermissions() 檢查表單是否支援發布。

您必須先發布表單,作答者才能存取這份表單。這個方法會取代對 setAcceptingResponses(enabled) 的呼叫。

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

// Publishes the form before sharing it.
form.setPublished(true);

// Checks whether the form is published or not and logs it to the console.
console.log(form.isPublished());

參數

名稱類型說明
enabledBoolean如果表單應發布,則為 true,否則為 false

回攻員

Form — This Form, for chaining.

擲回

Error:在不支援的表單上呼叫時。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setPublishingSummary(enabled)

設定表單是否要在受訪者提交表單後,顯示查看回覆摘要的連結。新表單的預設值為 false

參數

名稱類型說明
enabledBooleantrue 表單是否應顯示「查看先前的回覆」連結;false 否則請選取「否」。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setShowLinkToRespondAgain(enabled)

設定表單是否要在受訪者填寫完表單後,顯示提交其他回覆的連結。新表單的預設值為 true

參數

名稱類型說明
enabledBooleantrue 表單是否應顯示「提交其他回覆」連結;false 否則請選取「否」。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setShuffleQuestions(shuffle)

設定表單每頁的問題順序是否隨機。

參數

名稱類型說明
shuffleBooleantrue 表單各頁面的問題順序是否應隨機排列;false如果不是,

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

setTitle(title)

設定表單標題。

參數

名稱類型說明
titleString表單的新標題。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

為指定的 FormResponses 提交成績。

如果程式碼包含 onFormSubmit 觸發程序,呼叫 submitGrades() 會觸發 onFormSubmit 條件,並導致無限迴圈。為避免無限迴圈,請在呼叫 submitGrades() 前新增程式碼,檢查成績是否已存在。

參數

名稱類型說明
responsesFormResponse[]表單的所有回覆陣列。

回攻員

Form — This Form, for chaining.

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

supportsAdvancedResponderPermissions()

判斷表單是否支援發布。這個方法用於判斷 setPublished(enabled)isPublished() 方法,以及回應者權限是否可用。

// 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 whether the form supports publishing or not and logs it to the
// console.
console.log(form.supportsAdvancedResponderPermissions());

回攻員

Boolean - true (如果表單支援發布);false (如果表單不支援發布)。

授權

使用這個方法的指令碼需要透過下列一或多個範圍授權:

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

已淘汰的方法