Built-in Google Services

Built-in Google Services

Google Apps Script provides more than 30 built-in services for interacting with user data, other Google systems, and external systems. These services are provided as global objects akin to JavaScript's standard Matharrow-up-right object. For example, just as Math offers methods like random() and constants like PI, Apps Script's Spreadsheet servicearrow-up-right offers methods like openById(id)arrow-up-right, classes (child objects) like Rangearrow-up-right, and enums like DataValidationCriteriaarrow-up-right.

The reference documentation for services that control Google Workspace products are collected in the "Google Workspace Services" section under the "Reference" header in the sidebar of this site. Utility services (for things like creating user interfaces, parsing XML, or writing log data) are collected in the "Script Services" section.

Modern JavaScript features

Apps Script supports two JavaScript runtimes: the modern V8arrow-up-right runtime and an older one powered by Mozilla's Rhino JavaScript interpreterarrow-up-right.

The V8 runtimearrow-up-right supports modern ECMAScriptarrow-up-right syntax and features. The Rhino runtime is based on the older JavaScript 1.6arrow-up-right standard, plus a few features from 1.7arrow-up-right and 1.8arrow-up-right. You can freely choose which runtimearrow-up-right to use with your script, but the V8 runtime is strongly recommended.

Each runtime supports JavaScript classes and objects that are available to your script in addition to the built-in and advanced Google servicesarrow-up-right. Your scripts can use common objects like Arrayarrow-up-right, Datearrow-up-right, RegExparrow-up-right, and so fortharrow-up-right, as well as the Matharrow-up-right and Objectarrow-up-right global objects.Note: Because Apps Script code runs on Google's servers (with the exception of HTML-servicearrow-up-right pages), browser-based JavaScript features like DOM manipulation or the Windowarrow-up-right API are not available in Apps Script.

Using autocomplete

The script editor provides a "content assist" feature, more commonly called "autocomplete," which reveals the global objects as well as methods and enums that are valid in the script's current context. Autocomplete suggestions appear automatically whenever you type a period after a global object, enum, or method call that returns an Apps Script class. For example:

  • If you type the full name of a global object or select one from autocomplete, then type . (a period), you will see all methods and enums for that class.

  • If you type a few characters, you'll see all valid suggestions that begin with those characters.

Understanding global objects

Each service provides at least one global (top-level) object; for example, the Gmail servicearrow-up-right is accessed solely from the GmailApparrow-up-right object. Some services provide multiple global objects; for example, the Base servicearrow-up-right includes four global objects: Browserarrow-up-right, Loggerarrow-up-right, MimeTypearrow-up-right, and Sessionarrow-up-right.

Calling methods

The global objects of nearly all built-in or advanced servicesarrow-up-right include methods that return data or an Apps Script class. Scripts make method calls in this format:

For example, a script can send an email by calling the sendEmail(recipient, subject, body)arrow-up-right method of the Gmail service like so:

If a method returns another Apps Script class, you can chain method calls on one line. (Return types are shown both in autocomplete and in a method's reference documentation.) For example, the method DocumentApp.create()arrow-up-right returns a Documentarrow-up-right; thus, the following two sections of code are equivalent:

Accessing child classes

Every service includes one or more child classes that cannot be accessed from the top level as a global object can. You cannot use the new keyword to construct these classes, as you can with standard JavaScript classes like Datearrow-up-right; you can only access a child class by calling a method that returns it. If you're not sure how to access a certain class, visit the root page for the service's reference documentation and look for a method that returns the class you want.

Dealing with interfaces

A handful of services include special classes that are labeled as "interfaces" in the reference documentation. These are generic classes used as return types for methods that cannot determine the precise type in advance; for example, the Document servicearrow-up-right method Body.getChild(childIndex)arrow-up-right returns a generic Elementarrow-up-right object. Element is an interface that represents some other class, possibly a Paragrapharrow-up-right or Tablearrow-up-right. Interface objects are rarely useful on their own; instead, you usually want to call a method like Element.asParagraph()arrow-up-right to cast the object back to a precise class.

Working with enums

Most services include a few enums (enumerated types) of named values. For example, the Drive servicearrow-up-right uses the enums Accessarrow-up-right and Permissionarrow-up-right to determine which users have access to a file or folder. In almost all cases, you access these enums from the global object. For example, a call to the method Folder.setSharing(accessType, permissionType)arrow-up-right looks like this:

circle-info

Reference : https://developers.google.com/apps-script/guides/services

Last updated