-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.gs
196 lines (172 loc) · 5.76 KB
/
utilities.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Initializes the script using data from the Google sheet.
*
* @returns {object} The sheet data: setup, defaults, and specific group info
*/
function init() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetData = ss.getSheetByName("Groups")
var lastRow = sheetData.getLastRow();
var rangeBoundaries = "A2:K" + lastRow;
var range = sheetData.getRange(rangeBoundaries);
var allGroups = range.getValues();
var groups = {};
var data = [];
function stringToArray(s) {
if (s) {
var array = [];
array = s.split(',');
} else {
array = "";
}
return array;
}
for (var j = 0; j < allGroups.length; j++) {
var set = {
group: allGroups[j][0],
pCal: "",
sCal: allGroups[j][2],
recipients: allGroups[j][3],
share: stringToArray(allGroups[j][4]),
label: allGroups[j][5],
eDays: stringToArray(allGroups[j][6]),
window: allGroups[j][7],
webhook: allGroups[j][8],
wDays: stringToArray(allGroups[j][9]),
skip: allGroups[j][10]
};
data.push(set);
}
groups["data"] = data;
sheetData = ss.getSheetByName("Setup");
var setupValues = sheetData.getRange("A2:F2").getValues();
groups.maintainer = setupValues[0][0];
groups.eDaysDefault = stringToArray(setupValues[0][1]);
groups.windowDefault = setupValues[0][2];
groups.wDaysDefault = stringToArray(setupValues[0][3]);
var d = new Date();
groups.updateTime = setupValues[0][4];
groups.updateTime.setFullYear(d.getFullYear());
groups.updateTime.setMonth(d.getMonth());
groups.updateTime.setDate(d.getDate());
groups.timezone = setupValues[0][5];
groups.domain = Session.getActiveUser().getEmail().split("@")[1];
groups.count = Object.keys(groups.data).length;
return groups;
}
/**
* Creates the static object used for all groups' calendar updates and notifications.
*
* @param {object} g (groups) The groups object creates at initialization
* @param {integer} c (calendar update) The number of days to loop over
* @param {integer} b (backward) Start the calendar update from this many days in the past
*
* @returns {object} Properties that apply to all groups
*/
function setStatic(g, c, b) {
var startDate = new Date().toLocaleString("en-US", {timeZone: g.timezone})
startDate = new Date(startDate);
startDate.setHours(0);
startDate.setMinutes(0);
startDate.setSeconds(0);
startDate.setMilliseconds(0);
startDate.setDate(startDate.getDate() - b);
var a = {};
a.startDate = startDate;
a.endDate = addDays(startDate, (c - .0001));
a.calUpdate = c;
a.offset = "GMT-" + startDate.getTimezoneOffset() / 60;
a.timezone = g.timezone;
a.todayShort = Utilities.formatDate(startDate, a.offset, "M/d/yyyy");
a.domainName = g.domain;
a.maintainer = g.maintainer;
return a;
}
/**
* Creates the active object used for a specific group's calendar update and notifications.
*
* @param {object} g (groups) The groups object creates at initialization
* @param {integer} i (increment) The iterator from the parent function's loop, refers to a specific group
*
* @returns {object} Properties that apply to the specific group
*/
function setActive(g, i) {
function createIfNeeded(c) {
var cal = CalendarApp.getCalendarsByName(c)[0];
cal = (cal === undefined) ? CalendarApp.createCalendar(c, {timeZone: g.timezone}) : cal;
return c;
}
function labeler(label) {
label != "" ? label = label + " - " : label = "";
return label;
}
var a = {};
a.group = g.data[i].group;
a.pCal = "";
a.sCal = createIfNeeded(g.data[i].sCal);
a.recipients = g.data[i].recipients;
a.share = g.data[i].share;
a.prefix = (g.data[i].label === "") ? "" : labeler(g.data[i].label);
a.eDays = (g.data[i].eDays === "") ? g.eDaysDefault : g.data[i].eDays;
a.window = (g.data[i].window === "") ? g.windowDefault : g.data[i].window;
a.webhook = g.data[i].webhook;
a.wDays = (g.data[i].wDays === "") ? g.wDaysDefault : g.data[i].wDays;
a.skip = g.data[i].skip;
return a;
}
function containedGroups(group) {
var parent = group;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetData = ss.getSheetByName("Flattened Groups")
var lastRow = sheetData.getLastRow();
var fullRange = "A2:B" + lastRow;
var groupPairs = sheetData.getRange(fullRange).getValues();
var matchedGroups = [];
for (var i = 0; i < groupPairs.length; i++) {
if (groupPairs[i][0] === parent) {
matchedGroups.push(groupPairs[i][1]);
}
}
return matchedGroups;
}
/**
* Adds or subtracts days from a starting date.
*
* @param {object} date The starting date
* @param {integer} days The number of days to count forward. 1 = 24 hours.
*
* @returns {object} The new date
*/
function addDays(date, days) {
var dat = new Date(date);
dat.setTime(dat.getTime() + (days * 86400000));
return dat;
}
/**
* Checks whether any values in one array are found in another array.
*
* @param {array} target The source array of values to look for
* @param {array} toMatch The target array to search through
*
* @returns {boolean} True if any value from the source is found in the target
*/
function compareArrays(target, toMatch) {
var found, targetMap, i, j, cur;
found = false;
targetMap = {};
// Put all values in the `target` array into a map, where
// the keys are the values from the array
for (i = 0, j = target.length; i < j; i++) {
cur = target[i];
targetMap[cur] = true;
}
// Loop over all items in the `toMatch` array and see if any of
// their values are in the map from before
for (i = 0, j = toMatch.length; !found && (i < j); i++) {
cur = toMatch[i];
found = !!targetMap[cur];
// If found, `targetMap[cur]` will return true, otherwise it
// will return `undefined`...that's what the `!!` is for
}
return found;
}