Access and manipulate script publishing and triggers. This class allows users to create script triggers and control publishing the script as a service.
Properties
Property | Type | Description |
---|---|---|
AuthMode | AuthMode | |
EventType | EventType | |
TriggerSource | TriggerSource | |
WeekDay | Weekday |
Methods
Method | Return type | Brief description |
---|---|---|
deleteTrigger(trigger) | void | Removes the given trigger so it will no longer run. |
getOAuthToken() | String | Gets the OAuth 2.0 access token for the current user. |
getProjectTriggers() | Trigger[] | Returns an array of triggers associated with the current project. |
getService() | Service | Returns a Service object to manage publishing the script as a web app. |
invalidateAuth() | void | Invalidates the authorization this 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 a trigger that when fired, will call the passed in method. |
Detailed documentation
deleteTrigger(trigger)
Removes the given trigger so it will no longer run.
// This code deletes all the triggers
var triggers = ScriptApp.getProjectTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
Parameters
Name | Type | Description |
---|---|---|
trigger | Trigger | the trigger to delete |
getOAuthToken()
Gets the OAuth 2.0 access token for the current user. If the script's OAuth scopes are sufficient to authorize another Google API that normally requires its own OAuth flow (like Google Picker), scripts can bypass the second authorization prompt by passing this token instead. However, not all Google OAuth scopes are available in Apps Script. The token expires after a time (a few minutes at minimum); scripts should handle authorization failures and call this method to obtain a fresh token when needed.
Return
String
— a string representation of the OAuth 2.0 token
getProjectTriggers()
Returns an array of triggers associated with the current project.
Logger.log("current project has " + ScriptApp.getProjectTriggers().length + " triggers");
Return
Trigger[]
— all the triggers that this script owns
getService()
Returns a Service object to manage publishing the script as a web app.
// get the url of the published service
var url = ScriptApp.getService().getUrl();
Return
Service
— an object used to observe and control script service publishing
invalidateAuth()
Invalidates the authorization this user has to execute the current script. Used to invalidate any permissions for the current script. This is especially useful for functions tagged as one-shot authorization. Since one-shot authorization functions can only be called the first run after the script has acquired authorization, if you wish to perform an action afterwards, you will have to revoke any authorization the script had, so the user can see the authorization dialog again.
ScriptApp.invalidateAuth();
newStateToken()
Creates a builder for a state token that can be used in a callback API (like an OAuth flow).
// Generate a callback URL, given the name of a callback function. The script does not need to
// be published as a web app; the /usercallback URL suffix replaces /edit in any script's URL.
function getCallbackURL(callbackFunction) {
// IMPORTANT: Replace string below with the URL from your script, minus the /edit at the end.
var scriptUrl = 'https://script.google.com/macros/d/1234567890abcdefghijklmonpqrstuvwxyz';
var urlSuffix = '/usercallback?state=';
var stateToken = ScriptApp.newStateToken()
.withMethod(callbackFunction)
.withTimeout(120)
.createToken();
return scriptUrl + urlSuffix + stateToken;
}
Return
StateTokenBuilder
— an object used to continue the state token building process
newTrigger(functionName)
Begins the process of creating a trigger that when fired, will call the passed in method.
// creates an onEdit trigger for a spreadsheet identified by id
ScriptApp.newTrigger("myFunction").forSpreadsheet("id of my spreadsheet").onEdit().create();
Parameters
Name | Type | Description |
---|---|---|
functionName | String | the function to call when the trigger fires |
Return
TriggerBuilder
— an object used to continue the trigger building process