> For the complete documentation index, see [llms.txt](https://yo-sarawut.gitbook.io/snippet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yo-sarawut.gitbook.io/snippet/google-workspace-1/apps-script/google-sheets/built-in-google-services.md).

# 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 [`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) object. For example, just as `Math` offers methods like `random()` and constants like `PI`, Apps Script's [Spreadsheet service](https://developers.google.com/apps-script/reference/spreadsheet) offers methods like [`openById(id)`](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openById\(String\)), classes (child objects) like [`Range`](https://developers.google.com/apps-script/reference/spreadsheet/range), and enums like [`DataValidationCriteria`](https://developers.google.com/apps-script/reference/spreadsheet/data-validation-criteria).

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 <a href="#modern_javascript_features" id="modern_javascript_features"></a>

Apps Script supports two JavaScript runtimes: the modern [**V8**](https://v8.dev/) runtime and an older one powered by Mozilla's [**Rhino JavaScript interpreter**](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino).

The [V8 runtime](https://developers.google.com/apps-script/guides/v8-runtime) supports modern [ECMAScript](https://en.wikipedia.org/wiki/ECMAScript) syntax and features. The Rhino runtime is based on the older [JavaScript 1.6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.6) standard, plus a few features from [1.7](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7) and [1.8](https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.8). You can [freely choose which runtime](https://developers.google.com/apps-script/guides/v8-runtime#enabling_the_v8_runtime) 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 services](https://developers.google.com/apps-script/guides/services/advanced). Your scripts can use common objects like [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp), [and so forth](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference), as well as the [`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) and [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) global objects.**Note:** Because Apps Script code runs on Google's servers (with the exception of [HTML-service](https://developers.google.com/apps-script/guides/html) pages), browser-based JavaScript features like DOM manipulation or the [`Window`](https://developer.mozilla.org/en-US/docs/Web/API/Window) API are not available in Apps Script.

### Using autocomplete <a href="#using_autocomplete" id="using_autocomplete"></a>

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 <a href="#understanding_global_objects" id="understanding_global_objects"></a>

Each service provides at least one global (top-level) object; for example, the [Gmail service](https://developers.google.com/apps-script/reference/gmail) is accessed solely from the [`GmailApp`](https://developers.google.com/apps-script/reference/gmail/gmail-app) object. Some services provide multiple global objects; for example, the [Base service](https://developers.google.com/apps-script/reference/base) includes four global objects: [`Browser`](https://developers.google.com/apps-script/reference/base/browser), [`Logger`](https://developers.google.com/apps-script/reference/base/logger), [`MimeType`](https://developers.google.com/apps-script/reference/base/mime-type), and [`Session`](https://developers.google.com/apps-script/reference/base/session).

### Calling methods <a href="#calling_methods" id="calling_methods"></a>

The global objects of nearly all built-in or [advanced services](https://developers.google.com/apps-script/guides/services/advanced) include methods that return data or an Apps Script class. Scripts make method calls in this format:

```javascript
GlobalObjectName.methodName(argument1, argument2, ..., argumentN);
```

For example, a script can send an email by calling the [`sendEmail(recipient, subject, body)`](https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail\(String,String,String\)) method of the Gmail service like so:

```javascript
GmailApp.sendEmail('claire@example.com', 'Subject line', 'This is the body.');
```

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()`](https://developers.google.com/apps-script/reference/document/document-app#create\(String\)) returns a [`Document`](https://developers.google.com/apps-script/reference/document/document); thus, the following two sections of code are equivalent:

```javascript
var doc = DocumentApp.create('New document');
var body = doc.getBody();
body.appendParagraph('New paragraph.');

// Same result as above.
DocumentApp.create('New document').getBody().appendParagraph('New paragraph.');
```

### Accessing child classes <a href="#accessing_child_classes" id="accessing_child_classes"></a>

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 [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date); 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 href="#dealing_with_interfaces" id="dealing_with_interfaces"></a>

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 service](https://developers.google.com/apps-script/reference/document) method [`Body.getChild(childIndex)`](https://developers.google.com/apps-script/reference/document/body#getChild\(Integer\)) returns a generic [`Element`](https://developers.google.com/apps-script/reference/document/element) object. `Element` is an interface that represents some other class, possibly a [`Paragraph`](https://developers.google.com/apps-script/reference/document/paragraph) or [`Table`](https://developers.google.com/apps-script/reference/document/table). Interface objects are rarely useful on their own; instead, you usually want to call a method like [`Element.asParagraph()`](https://developers.google.com/apps-script/reference/document/element#asParagraph\(\)) to cast the object back to a precise class.

### Working with enums <a href="#working_with_enums" id="working_with_enums"></a>

Most services include a few enums (enumerated types) of named values. For example, the [Drive service](https://developers.google.com/apps-script/reference/drive) uses the enums [`Access`](https://developers.google.com/apps-script/reference/drive/access) and [`Permission`](https://developers.google.com/apps-script/reference/drive/permission) 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)`](https://developers.google.com/apps-script/reference/drive/folder#setSharing\(Access,Permission\)) looks like this:

```javascript
// Creates a folder that anyone on the Internet can read from and write to. (Domain administrators can
// prohibit this setting for Google Workspace users.)
var folder = DriveApp.createFolder('Shared Folder');
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
```

{% hint style="info" %}
Reference : <https://developers.google.com/apps-script/guides/services>
{% endhint %}
