-
Notifications
You must be signed in to change notification settings - Fork 27
/
#Triggers.gs
47 lines (36 loc) · 1.31 KB
/
#Triggers.gs
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
44
45
46
47
var C_SOURCE_TRIGGER = 'SPREADSHEETS';
/*
--------------------------------------------------------------------
_____ _ _______ _
/ ____| | | |__ __| (_)
| (___ ___| |_ | |_ __ _ __ _ __ _ ___ _ __
\___ \ / _ \ __| | | '__| |/ _` |/ _` |/ _ \ '__|
____) | __/ |_ | | | | | (_| | (_| | __/ |
|_____/ \___|\__| |_|_| |_|\__, |\__, |\___|_|
__/ | __/ |
|___/ |___/
--------------------------------------------------------------------
*/
function setTriggerOnEdit(nameFunction)
{
if (checkTriggerExists(nameFunction, C_SOURCE_TRIGGER)) { return -1; } // trigger exists
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger(nameFunction)
.forSpreadsheet(ss)
.onEdit()
.create();
}
/*
USAGE
var exists = checkTriggerExists('test_getSets', 'SPREADSHEETS')
*/
function checkTriggerExists(nameFunction, triggerSourceType)
{
var triggers = ScriptApp.getProjectTriggers();
var trigger = {};
for (var i = 0; i < triggers.length; i++) {
trigger = triggers[i];
if (trigger.getHandlerFunction() == nameFunction && trigger.getTriggerSource() == triggerSourceType) return true;
}
return false;
}