This repository was archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriggerManager.js
More file actions
43 lines (36 loc) · 1.35 KB
/
TriggerManager.js
File metadata and controls
43 lines (36 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function installTriggers() {
var userProperties = PropertiesService.getUserProperties();
var properties = userProperties.getProperties();
//Check if each of the triggers have an ID (are currently installed)
var syncTriggerID = properties["syncTrigger"];
if (!syncTriggerID) {
// Sync every 12 hours.
var syncTrigger = ScriptApp.newTrigger('updateCalendarsForCourses')
.timeBased()
.everyHours(12)
.create();
userProperties.setProperty("syncTrigger", syncTrigger.getUniqueId()); //Remember the ID for the user to delete it later.
}
else {throw ("Trigger is already installed. Stopping.");}
}
function deleteTriggers() {
var userProperties = PropertiesService.getUserProperties();
var properties = userProperties.getProperties();
var syncTriggerID = properties["syncTrigger"];
if (syncTriggerID) {
deleteTriggerById(syncTriggerID);
userProperties.deleteProperty("syncTrigger");
}
else {throw ("Trigger is not currently installed. Stopping.");}
}
function deleteTriggerById(triggerId) {
// Loop over all triggers.
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[i].getUniqueId() === triggerId) {
ScriptApp.deleteTrigger(allTriggers[i]);
break;
}
}
}