Trigger

A script trigger.

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/script/trigger#getEventType()` | `https://developers.google.com/apps-script/reference/script/event-type` | Returns the event type that the trigger fires on. |
| `https://developers.google.com/apps-script/reference/script/trigger#getHandlerFunction()` | `String` | Returns the function that is called when the trigger fires. |
| `https://developers.google.com/apps-script/reference/script/trigger#getTriggerSource()` | `https://developers.google.com/apps-script/reference/script/trigger-source` | Returns the source of events that causes the trigger to fire. |
| `https://developers.google.com/apps-script/reference/script/trigger#getTriggerSourceId()` | `String` | Returns the id specific to the source. |
| `https://developers.google.com/apps-script/reference/script/trigger#getUniqueId()` | `String` | Returns a unique identifier that can be used to distinguish triggers from each other. |

## Detailed documentation

### `getEventType()`

Returns the event type that the trigger fires on.

```javascript
const triggers = ScriptApp.getProjectTriggers();
for (let i = 0; i < triggers.length; i++) {
  if (triggers[i].getEventType() === ScriptApp.EventType.CLOCK) {
    // Some code here - other options are:
    // ScriptApp.EventType.ON_EDIT
    // ScriptApp.EventType.ON_FORM_SUBMIT
    // ScriptApp.EventType.ON_OPEN
  }
}
```

#### Return


`https://developers.google.com/apps-script/reference/script/event-type` --- The event type that this is a trigger for.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/script.scriptapp`

*** ** * ** ***

### `getHandlerFunction()`

Returns the function that is called when the trigger fires.

```javascript
// Create a trigger for the script.
ScriptApp.newTrigger('myFunction')
    .forSpreadsheet('id of my spreadsheet')
    .onEdit()
    .create();
Logger.log(ScriptApp.getProjectTriggers()[0]
               .getHandlerFunction());  // logs "myFunction"
```

#### Return


`String` --- The method name.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/script.scriptapp`

*** ** * ** ***

### `getTriggerSource()`

Returns the source of events that causes the trigger to fire.

For example, a spreadsheet onEdit trigger returns SPREADSHEETS, or a time based trigger
returns CLOCK.

```javascript
const triggers = ScriptApp.getProjectTriggers();
for (let i = 0; i < triggers.length; i++) {
  if (triggers[i].getTriggerSource() === ScriptApp.TriggerSource.CLOCK) {
    Logger.log(`${triggers[i].getUniqueId()} source is clock`);
  } else if (
      triggers[i].getTriggerSource() === ScriptApp.TriggerSource.SPREADSHEETS) {
    Logger.log(`${triggers[i].getUniqueId()} source is spreadsheets`);
  }
}
```

#### Return


`https://developers.google.com/apps-script/reference/script/trigger-source` --- The publisher this is a trigger for.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/script.scriptapp`

*** ** * ** ***

### `getTriggerSourceId()`

Returns the id specific to the source.

For example, if the trigger source is a spreadsheet, this is the id of the spreadsheet. For
clock events this returns `null`.

#### Return


`String` --- The id of the entity in the publisher that this is a trigger for.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/script.scriptapp`

*** ** * ** ***

### `getUniqueId()`

Returns a unique identifier that can be used to distinguish triggers from each other.

#### Return


`String` --- The unique identifier of the trigger.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/script.scriptapp`