-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtriggers.gs
118 lines (117 loc) · 3.84 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START apps_script_triggers_onopen]
/**
* The event handler triggered when opening the spreadsheet.
* @param {Event} e The onOpen event.
* @see https://developers.google.com/apps-script/guides/triggers#onopene
*/
function onOpen(e) {
// Add a custom menu to the spreadsheet.
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addToUi();
}
// [END apps_script_triggers_onopen]
// [START apps_script_triggers_onedit]
/**
* The event handler triggered when editing the spreadsheet.
* @param {Event} e The onEdit event.
* @see https://developers.google.com/apps-script/guides/triggers#onedite
*/
function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
const range = e.range;
range.setNote('Last modified: ' + new Date());
}
// [END apps_script_triggers_onedit]
// [START apps_script_triggers_onselectionchange]
/**
* The event handler triggered when the selection changes in the spreadsheet.
* @param {Event} e The onSelectionChange event.
* @see https://developers.google.com/apps-script/guides/triggers#onselectionchangee
*/
function onSelectionChange(e) {
// Set background to red if a single empty cell is selected.
const range = e.range;
if (range.getNumRows() === 1 &&
range.getNumColumns() === 1 &&
range.getCell(1, 1).getValue() === '') {
range.setBackground('red');
}
}
// [END apps_script_triggers_onselectionchange]
// [START apps_script_triggers_oninstall]
/**
* The event handler triggered when installing the add-on.
* @param {Event} e The onInstall event.
* @see https://developers.google.com/apps-script/guides/triggers#oninstalle
*/
function onInstall(e) {
onOpen(e);
}
// [END apps_script_triggers_oninstall]
// [START apps_script_triggers_time]
/**
* Creates two time-driven triggers.
* @see https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers
*/
function createTimeDrivenTriggers() {
// Trigger every 6 hours.
ScriptApp.newTrigger('myFunction')
.timeBased()
.everyHours(6)
.create();
// Trigger every Monday at 09:00.
ScriptApp.newTrigger('myFunction')
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(9)
.create();
}
// [END apps_script_triggers_time]
// [START apps_script_triggers_open]
/**
* Creates a trigger for when a spreadsheet opens.
* @see https://developers.google.com/apps-script/guides/triggers/installable
*/
function createSpreadsheetOpenTrigger() {
const ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('myFunction')
.forSpreadsheet(ss)
.onOpen()
.create();
}
// [END apps_script_triggers_open]
// [START apps_script_triggers_delete]
/**
* Deletes a trigger.
* @param {string} triggerId The Trigger ID.
* @see https://developers.google.com/apps-script/guides/triggers/installable
*/
function deleteTrigger(triggerId) {
// Loop over all triggers.
const allTriggers = ScriptApp.getProjectTriggers();
for (let index = 0; index < allTriggers.length; index++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[index].getUniqueId() === triggerId) {
ScriptApp.deleteTrigger(allTriggers[index]);
break;
}
}
}
// [END apps_script_triggers_delete]