This repository has been archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommitteeApplicationSorter.gs
133 lines (122 loc) · 5.08 KB
/
committeeApplicationSorter.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
/*
* This script serves to automatically sort committee applications into separate spreadsheets
* for the respective committees. To use it, create a form for submitting applications (copy an old one),
* connect a spreadsheet to the form, add a "Google Apps-script" extention to the sheet, paste the following
* code into a .gs file and run setUpTrigger() ONCE. The sheets will now automatically update whenever a new
* application is submitted
*/
function updateCommitteesAndGroups() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName("Skjemasvar 1");
var data = mainSheet.getDataRange().getValues();
var headers = data[0];
var applicants = data.slice(1);
/* The columns containing committes the applicant applied for */
var committeeColumns = {
førstevalg: headers.findIndex((col) =>
col.toLowerCase().includes("førstevalg"),
),
andrevalg: headers.findIndex((col) =>
col.toLowerCase().includes("andrevalg"),
),
tredjevalg: headers.findIndex((col) =>
col.toLowerCase().includes("tredjevalg"),
),
Backlog: headers.findIndex((col) =>
col.toLowerCase().includes("backlog"),
),
FeminIT: headers.findIndex((col) =>
col.toLowerCase().includes("feminit"),
),
Revyen: headers.findIndex((col) =>
col.toLowerCase().includes("revyen"),
),
Realfagskjelleren: headers.findIndex((col) =>
col.toLowerCase().includes("realfagskjelleren"),
),
};
processApplicants(applicants, committeeColumns, headers);
}
function processApplicants(applicants, committeeColumns, headers) {
var folder = DriveApp.getFileById(
SpreadsheetApp.getActiveSpreadsheet().getId(),
)
.getParents()
.next();
applicants.forEach(function (applicant) {
var addedCommittees = [];
for (let keyword in committeeColumns) {
let col = committeeColumns[keyword];
let committee = applicant[col];
/* If Backlog, FeminIT, Revyen or Realfagskjelleren is checked off, register the applicant for that committee */
if (keyword === "Backlog" || keyword === "FeminIT" || keyword === "Revyen" || keyword === "Realfagskjelleren") {
if (committee.includes("ønsker å søke verv")) {
committee = keyword;
} else {
continue;
}
}
if (committee && !addedCommittees.includes(committee)) {
let existingFile = folder.getFilesByName(committee).hasNext();
var ss;
if (existingFile) {
ss = SpreadsheetApp.open(
folder.getFilesByName(committee).next(),
);
} else {
ss = SpreadsheetApp.create(committee);
var fileId = ss.getId();
var file = DriveApp.getFileById(fileId);
file.moveTo(folder);
}
let sheets = ss.getSheets();
let sheet = sheets[0];
/* The header filters out which committees an applicant applied for, so the recieving committee can't see the how they're prioritized */
if (sheet.getLastRow() === 0) {
sheet.appendRow(
headers.filter(
(_, idx) =>
!Object.values(committeeColumns).includes(idx),
),
);
}
var emailCol = headers.findIndex((col) =>
col.toLowerCase().includes("e-postadresse"),
);
var numRows = sheet.getLastRow() - 1;
if (numRows >= 1) {
var existingApplicants = sheet
.getRange(2, emailCol + 1, numRows, 1)
.getValues();
if (
!existingApplicants.flat().includes(applicant[emailCol])
) {
sheet.appendRow(
applicant.filter(
(_, idx) =>
!Object.values(committeeColumns).includes(
idx,
),
),
);
addedCommittees.push(committee);
}
} else {
sheet.appendRow(
applicant.filter(
(_, idx) =>
!Object.values(committeeColumns).includes(idx),
),
);
addedCommittees.push(committee);
}
}
}
});
}
function setUpTrigger() {
ScriptApp.newTrigger("updateCommitteesAndGroups")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}