-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.js
163 lines (147 loc) · 4.33 KB
/
Main.js
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
function getCalendars(sheet) {
var calendars = [];
if (Settings.getGoogleCalendarEnabled()) {
calendars.push(new GoogleCalendar(CalendarApp.getDefaultCalendar()));
}
if (Settings.getOffice365CalendarEnabled()) {
calendars.push(new Office365Calendar());
}
if (Settings.getHrCalendarEnabled()) {
calendars.push(new HrCalendar(sheet));
}
return calendars;
}
function checkEventForActiveRow() {
if (!authorizeIfRequired()) {
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
var rowIndex = sheet.getActiveCell().getRowIndex();
var entry = new AbsenceEntry(sheet, rowIndex);
var calendars = getCalendars(sheet);
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
new Synchronizer(entry, calendar).markSynchronized();
}
}
function checkEventsForAllRows() {
if (!authorizeIfRequired()) {
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var numRows = range.getNumRows();
var rows = range.getValues();
for (var rowIndex = 0; rowIndex < numRows; rowIndex++) {
var row = rows[rowIndex];
switch (row[0]) {
case "Type":
case "":
break;
case "Bank holiday":
var entry = new AbsenceEntry(sheet, rowIndex + 1);
var calendars = getCalendars(sheet);
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
entry.clearCalendarConflict(calendar.getType());
}
break;
default:
var entry = new AbsenceEntry(sheet, rowIndex + 1);
var calendars = getCalendars(sheet);
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
new Synchronizer(entry, calendar).markSynchronized();
}
break;
}
}
}
function syncEventForActiveRow() {
if (!authorizeIfRequired()) {
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
var rowIndex = sheet.getActiveCell().getRowIndex();
var entry = new AbsenceEntry(sheet, rowIndex);
var calendars = getCalendars(sheet);
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
new Synchronizer(entry, calendar).synchronize();
}
}
function deleteEventForActiveRow() {
if (!authorizeIfRequired()) {
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
var rowIndex = sheet.getActiveCell().getRowIndex();
var entry = new AbsenceEntry(sheet, rowIndex);
var calendars = getCalendars(sheet);
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
deleteEventIfRequired(calendar, entry);
}
}
function deleteEventIfRequired(calendar, entry) {
if (calendar.isReadOnly()) {
return;
}
var event = entry.findEvent(calendar);
if (event !== null) {
event.deleteEvent();
}
var calendarType = calendar.getType();
entry.setCalendarId(calendarType, null);
entry.markCalendarConflict(calendarType, "Event missing");
}
function configureActiveRow() {
var sheet = SpreadsheetApp.getActiveSheet();
var rowIndex = sheet.getActiveCell().getRowIndex();
var entry = new AbsenceEntry(sheet, rowIndex);
entry.configure();
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [
{
name: "Check all rows",
functionName: "checkEventsForAllRows",
},
{
name: "Configure active row",
functionName: "configureActiveRow",
},
{
name: "Check event for active row",
functionName: "checkEventForActiveRow",
},
{
name: "Sync event for active row",
functionName: "syncEventForActiveRow",
},
{
name: "Delete event for active row",
functionName: "deleteEventForActiveRow",
},
];
if (Settings.getOffice365CalendarEnabled()) {
entries.push({
name: "Authorize access to Office 365",
functionName: "authorizeIfRequired",
});
entries.push({
name: "Logout from Office 365",
functionName: "logout",
});
}
sheet.addMenu("Calendar", entries);
}