-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duties.gs
158 lines (135 loc) · 3.45 KB
/
Duties.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
function clearDuties() {
var sheet = spreadsheet.getSheetByName('Duties');
sheet.clear();
}
function getNoOfDutiesSetup() {
return dutySetupArray.length;
}
function getDutyByDutyNo(dutyNo) {
return dutyArray.filter(dutyNoFilter(dutyNo));
}
function dutyNoFilter(dutyNo) {
return function(element) {
if (element[0] == dutyNo) {
return true;
}
else {
return false;
}
}
}
function getPlannedDuties() {
return dutyArray.filter(plannedDutyFilter());
}
function plannedDutyFilter() {
return function(element) {
if ((element[4] != "") && (element[4] != "Member")) {
return true;
}
else {
return false;
}
}
}
function getIndexOfDutyNo(dutyNo) {
for (var i = 0; i < dutyArray.length; i++) {
if (dutyArray[i][0] == dutyNo) {
return i;
}
}
}
function getDutyForDutyCodeAndDate(dutyCode,dutyDate) {
return dutyArray.filter(dutyFilter(dutyCode,dutyDate));
}
function dutyFilter(dutyCode,dutyDate) {
return function(element) {
if ((element[1] == dutyCode) && datesAreEqual(element[3],dutyDate)) {
return true;
}
else {
return false;
}
}
}
function getDutiesByDutyFilterAndDateRange(dutyFilter,startDate,endDate) {
return dutyArray.filter(filterDutiesByDutyFilterAndTimePeriod(dutyFilter,startDate,endDate));
}
function filterDutiesByDutyFilterAndTimePeriod(dutyFilter,startDate,endDate) {
return function(element) {
//check this isn't the header row
if (element[0] == 'Entry No.') {
return false;
}
if (!dutyMatchesDutyFilter(element[1],dutyFilter)) {
return false;
}
if (element[3] < startDate) {
return false;
}
if (element[3] > endDate) {
return false;
}
return true;
}
}
function getDutiesSetup(){
var sheet = spreadsheet.getSheetByName('Duty Setup');
return sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
}
function populateDuties(){
var sheet = spreadsheet.getSheetByName('Duties');
sheet.appendRow(['Entry No.','Duty Code','Description','Date','Member','Candidates','No. of Candidates']);
sheet.getRange(1, 1, 1, sheet.getLastColumn()).setFontWeight('bold');
for (var i = 0; i < dutySetupArray.length; i++)
{
duty = dutySetupArray[i];
if (duty[3] == 'Yes')
{
populateRecurringDuty(duty);
}
}
}
function populateRecurringDuty(duty){
var dutyDate = getFirstGivenDayOfWeekAfterDate(getDayOfWeekOfDuty(duty[4]),getStartDate());
var endDate = getEndDate();
while (dutyDate <= endDate)
{
populateDuty(duty[0],dutyDate,duty[1]);
dutyDate = addDaysToDate(7,dutyDate);
}
}
function populateDuty(dutyCode,date,dutyDescription){
var sheet = spreadsheet.getSheetByName('Duties');
dutyNo += 1;
sheet.appendRow([dutyNo,dutyCode,dutyDescription,date]);
}
function getDayOfWeekOfDuty(dayName){
switch(dayName)
{
case 'Sunday':
return 0;
case 'Monday':
return 1;
case 'Tuesday':
return 2;
case 'Wednesday':
return 3;
case 'Thursday':
return 4;
case 'Friday':
return 5;
case 'Saturday':
return 6;
}
}
function dutyMatchesDutyFilter(dutyCode,dutyFilter) {
if (dutyFilter == '*') {
return true;
}
else if (dutyFilter.substr(dutyFilter.length - 1,1) == '*') {
return dutyCode.substring(0,dutyFilter.length - 1) == dutyFilter.substring(0,dutyFilter.length - 1);
}
else {
return dutyCode == dutyFilter;
}
}