Script Service

  • The Script service empowers developers to automate tasks and control web app deployments within Google Apps Script.

  • Triggers automate script execution based on events like opening documents, form submissions, or time-based occurrences.

  • Script publishing controls how scripts are deployed as web apps, managing access and properties.

  • Authorization management handles user permissions for script execution, with different authorization levels and status checks.

  • Developers can utilize the ScriptApp class, trigger builders, and authorization classes to build robust and automated solutions.

Script

This service provides access to script triggers and script publishing.

Classes

NameBrief description
AuthMode An enumeration that identifies which categories of authorized services Apps Script is able to execute through a triggered function.
AuthorizationInfo An object that checks if the user has granted authorization for the required scopes of the script.
AuthorizationStatus An enumeration denoting the authorization status of a script.
CalendarTriggerBuilder Builder for calendar triggers.
ClockTriggerBuilder A builder for clock triggers.
DocumentTriggerBuilder A builder for document triggers.
EventType An enumeration denoting the type of triggered event.
FormTriggerBuilder A builder for form triggers.
InstallationSource An enumeration that indicates how the script came to be installed as an add-on for the current user.
ScriptApp Access and manipulate script publishing and triggers.
Service Access and manipulate script publishing.
SpreadsheetTriggerBuilder Builder for spreadsheet triggers.
StateTokenBuilder Allows scripts to create state tokens that can be used in callback APIs (like OAuth flows).
Trigger A script trigger.
TriggerBuilder A generic builder for script triggers.
TriggerSource An enumeration denoting the source of the event that causes the trigger to fire.

AuthMode

Properties

PropertyTypeDescription
NONEEnumA mode that does not allow access to any services that require authorization.
CUSTOM_FUNCTIONEnumA mode that allows access to a limited subset of services for use in custom spreadsheet functions.
LIMITEDEnumA mode that allows access to a limited subset of services.
FULLEnumA mode that allows access to all services that require authorization.

AuthorizationInfo

Methods

MethodReturn typeBrief description
getAuthorizationStatus() AuthorizationStatus Gets a value that indicates whether the user needs to authorize this script to use one or more services (for example, ScriptApp.AuthorizationStatus.REQUIRED).
getAuthorizationUrl() StringGets the authorization URL that can be used to grant access to the script.
getAuthorizedScopes() String[]Gets a list of authorized scopes for the script.

AuthorizationStatus

Properties

PropertyTypeDescription
REQUIREDEnumThe user needs to authorize this script to use one or more services.
NOT_REQUIREDEnumThe user has granted this script all the authorization it currently requires.

CalendarTriggerBuilder

Methods

MethodReturn typeBrief description
create() Trigger Creates the trigger and returns it.
onEventUpdated() CalendarTriggerBuilder Specifies a trigger that fires when a calendar entry is created, updated, or deleted.

ClockTriggerBuilder

Methods

MethodReturn typeBrief description
after(durationMilliseconds) ClockTriggerBuilder Specifies the minimum duration (in milliseconds) after the current time that the trigger runs.
at(date) ClockTriggerBuilder Specifies when the trigger runs.
atDate(year, month, day) ClockTriggerBuilder Specifies that the trigger fires on the given date, by default near midnight (+/- 15 minutes).
atHour(hour) ClockTriggerBuilder Specifies the hour the trigger at which the trigger runs.
create() Trigger Creates the trigger.
everyDays(n) ClockTriggerBuilder Specifies to run the trigger every n days.
everyHours(n) ClockTriggerBuilder Specifies to run the trigger every n hours.
everyMinutes(n) ClockTriggerBuilder Specifies to run the trigger every n minutes.
everyWeeks(n) ClockTriggerBuilder Specifies to run the trigger every n weeks.
inTimezone(timezone) ClockTriggerBuilder Specifies the timezone for the specified dates/time when the trigger runs.
nearMinute(minute) ClockTriggerBuilder Specifies the minute at which the trigger runs (plus or minus 15 minutes).
onMonthDay(day) ClockTriggerBuilder Specifies the date in the month that the trigger runs.
onWeekDay(day) ClockTriggerBuilder Specifies the day of the week that the trigger runs.

DocumentTriggerBuilder

Methods

MethodReturn typeBrief description
create() Trigger Creates and returns the new trigger.
onOpen() DocumentTriggerBuilder Specifies a trigger that will fire when the document is opened.

EventType

Properties

PropertyTypeDescription
CLOCKEnumThe trigger fires once the time-driven event reaches a specific time.
ON_OPENEnumThe trigger fires once the user opens the Google Docs, Sheets, or Forms file.
ON_EDITEnumThe trigger fires once the user edits the Google Sheets file (for example, by entering a new value into a cell, which counts as an edit instead of a change).
ON_FORM_SUBMITEnumThe trigger fires once the user responds to a Google Form.
ON_CHANGEEnumThe trigger fires once the user changes the Google Sheets file (for example, by adding a row, which counts as a change instead of an edit).
ON_EVENT_UPDATEDEnumThe trigger fires once an event gets created, updated, or deleted on the specified Google Calendar.

FormTriggerBuilder

Methods

MethodReturn typeBrief description
create() Trigger Creates and returns the new trigger.
onFormSubmit() FormTriggerBuilder Specifies a trigger that will fire when a response is submitted to the form.
onOpen() FormTriggerBuilder Specifies a trigger that will fire when the form's edit view is opened.

InstallationSource

Properties

PropertyTypeDescription
APPS_MARKETPLACE_DOMAIN_ADD_ONEnumAdd-on was installed by the administrator for the user's domain.
NONEEnumScript is not running as an add-on.
WEB_STORE_ADD_ONEnumAdd-on was installed by the user from the Chrome Web Store.

ScriptApp

Properties

PropertyTypeDescription
AuthModeAuthMode An enumeration that identifies which categories of authorized services Apps Script is able to execute through a triggered function.
AuthorizationStatusAuthorizationStatus An enumeration denoting the authorization status of a script.
EventTypeEventType An enumeration denoting the type of triggered event.
InstallationSourceInstallationSource An enumeration denoting how the script was installed to the user as an add-on.
TriggerSourceTriggerSource An enumeration denoting the source of the event that causes the trigger to fire.
WeekDayWeekday An enumeration representing the days of the week.

Methods

MethodReturn typeBrief description
deleteTrigger(trigger) voidRemoves the given trigger so it no longer runs.
getAuthorizationInfo(authMode) AuthorizationInfo Gets an object that checks if the user has granted authorization for all the script requirements.
getAuthorizationInfo(authMode, oAuthScopes) AuthorizationInfo Gets an object that checks if the user has granted authorization for the requested scopes.
getIdentityToken() StringGets an OpenID Connect identity token for the effective user, if the openid scope has been granted.
getInstallationSource() InstallationSource Returns an enum value that indicates how the script came to be installed as an add-on for the current user (for example, whether the user installed it personally through the Chrome Web Store, or whether a domain administrator installed it for all users).
getOAuthToken() StringGets the OAuth 2.0 access token for the effective user.
getProjectTriggers() Trigger[] Gets all installable triggers associated with the current project and current user.
getScriptId() StringGets the script project's unique ID.
getService() Service Gets an object used to control publishing the script as a web app.
getUserTriggers(document) Trigger[] Gets all installable triggers owned by this user in the given document, for this script or add-on only.
getUserTriggers(form) Trigger[] Gets all installable triggers owned by this user in the given form, for this script or add-on only.
getUserTriggers(spreadsheet) Trigger[] Gets all installable triggers owned by this user in the given spreadsheet, for this script or add-on only.
invalidateAuth() voidInvalidates the authorization the effective user has to execute the current script.
newStateToken() StateTokenBuilder Creates a builder for a state token that can be used in a callback API (like an OAuth flow).
newTrigger(functionName) TriggerBuilder Begins the process of creating an installable trigger that, when fired, calls a given function.
requireAllScopes(authMode) voidValidates if the user has granted consent for all of the scopes requested by the script.
requireScopes(authMode, oAuthScopes) voidValidates if the user has granted consent for the requested scopes.

Service

Methods

MethodReturn typeBrief description
getUrl() StringReturns the URL of the web app, if it has been deployed; otherwise returns null.
isEnabled() BooleanReturns true if the script is accessible as a web app.

SpreadsheetTriggerBuilder

Methods

MethodReturn typeBrief description
create() Trigger Creates the trigger and returns it.
onChange() SpreadsheetTriggerBuilder Specifies a trigger that will fire when the spreadsheet's content or structure is changed.
onEdit() SpreadsheetTriggerBuilder Specifies a trigger that will fire when the spreadsheet is edited.
onFormSubmit() SpreadsheetTriggerBuilder Specifies a trigger that will fire when the spreadsheet has a form submitted to it.
onOpen() SpreadsheetTriggerBuilder Specifies a trigger that will fire when the spreadsheet is opened.

StateTokenBuilder

Methods

MethodReturn typeBrief description
createToken() StringConstructs an encrypted string representation of the state token.
withArgument(name, value) StateTokenBuilder Adds an argument to the token.
withMethod(method) StateTokenBuilder Sets a callback function.
withTimeout(seconds) StateTokenBuilder Sets the duration (in seconds) for which the token is valid.

Trigger

Methods

MethodReturn typeBrief description
getEventType() EventType Returns the event type that the trigger fires on.
getHandlerFunction() StringReturns the function that will be called when the trigger fires.
getTriggerSource() TriggerSource Returns the source of events that will cause the trigger to fire.
getTriggerSourceId() StringReturns the id specific to the source.
getUniqueId() StringReturns a unique identifier that can be used to distinguish triggers from each other.

TriggerBuilder

Methods

MethodReturn typeBrief description
forDocument(document) DocumentTriggerBuilder Creates and returns a DocumentTriggerBuilder tied to the given document.
forDocument(key) DocumentTriggerBuilder Creates and returns a DocumentTriggerBuilder tied to the document with the given ID.
forForm(form) FormTriggerBuilder Creates and returns a FormTriggerBuilder tied to the given form.
forForm(key) FormTriggerBuilder Creates and returns a FormTriggerBuilder tied to the form with the given ID.
forSpreadsheet(sheet) SpreadsheetTriggerBuilder Creates and returns a SpreadsheetTriggerBuilder tied to the given spreadsheet.
forSpreadsheet(key) SpreadsheetTriggerBuilder Creates and returns a SpreadsheetTriggerBuilder tied to the spreadsheet with the given ID.
forUserCalendar(emailId) CalendarTriggerBuilder Returns a builder for building calendar triggers.
timeBased() ClockTriggerBuilder Creates and returns a ClockTriggerBuilder for building time-based triggers.

TriggerSource

Properties

PropertyTypeDescription
SPREADSHEETSEnumGoogle Sheets causes the trigger to fire.
CLOCKEnumA time-driven event causes the trigger to fire.
FORMSEnumGoogle Forms causes the trigger to fire.
DOCUMENTSEnumGoogle Docs causes the trigger to fire.
CALENDAREnumGoogle Calendar causes the trigger to fire.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年01月08日 UTC.