-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
178 lines (151 loc) · 6.31 KB
/
main.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
const SESSIONS = 'sessions';
const PARTICIPANTS = 'participants';
const NUM_SESSIONS = 'numSessions';
const NUM_CHOICES = 'numChoices';
const SESSION_SETUP = SpreadsheetApp.getActive().getSheetByName('Session Setup');
const PARTICIPANT_SETUP = SpreadsheetApp.getActive().getSheetByName('Participant Setup');
const PARTICIPANT_ASSIGNMENTS = SpreadsheetApp.getActive().getSheetByName('Participant Assignments');
function getParticipants() {
const data = PARTICIPANT_SETUP.getDataRange().getValues();
const participants = getProperty(PARTICIPANTS);
const numSessions = getProperty(NUM_SESSIONS);
for (let i=1; i < data.length; i++) {
if (data[i][1].trim()) {
participants[data[i][1].trim()] = {
'name': data[i][5].trim(),
'homeroom': data[i][2].trim(),
'choices': [...new Set(data[i].slice(6))],
'assigned': {}
}
for (let n = 0; n < numSessions; n++) participants[data[i][1]].assigned[n+1] = "";
}
}
PARTICIPANT_ASSIGNMENTS.getRange(2,6,PARTICIPANT_ASSIGNMENTS.getLastRow()-1, PARTICIPANT_ASSIGNMENTS.getLastColumn()-5).clearContent();
// console.log(students);
return setProperty(PARTICIPANTS, participants)
}
function getSessions() {
const data = SESSION_SETUP.getDataRange().getValues();
const sessions = getProperty(SESSIONS);
const numSessions = getProperty(NUM_SESSIONS);
for (let i = 1; i < data.length; i++) {
if (data[i][0].trim()) {
sessions[data[i][0].trim()] = {
'room': data[i][1],
'maxParticipants': data[i][4],
'sessions': {}
}
for (let n = 0; n < numSessions; n++) sessions[data[i][0]].sessions[n+1] = {
"active": 'TRUE',
"list": []
};
};
}
// console.log(workshops);
setProperty(SESSIONS, sessions);
displaySessions(sessions, numSessions);
return sessions
}
function assignParticipants() {
const participants = getProperty(PARTICIPANTS);
const sessions = getProperty(SESSIONS);
const numChoices = getProperty(NUM_CHOICES);
const assign = (s, id, num) => {
participants[id].assigned[num] = s;
sessions[s].sessions[num].list.push(participants[id].name);
console.log(`${count} -- Assigning session ${s} ${num} (${sessions[s].sessions[num].list.length}) to ${participants[id].name} -- Choices remaining: ${participants[id].choices} -- ${participants[id].assigned}`);
}
let count = 0;
while (count < numChoices) {
for (s in sessions) {
// console.log(s);
for (id in participants) {
const lowestArr = [];
for (l in sessions[s].sessions) {
lowestArr.push(sessions[s].sessions[l].list.length);
}
let lowest = lowestArr.indexOf(Math.min(...lowestArr))+1;
let second = Math.min(lowestArr.findIndex((v) => v !== lowest)) + 1;
const duplicate = hasSession(participants[id], s);
if (duplicate) continue;
if (participants[id].choices[count] === s) {
if (!participants[id].assigned[lowest] && sessions[s].sessions[lowest].active === 'TRUE' && sessions[s].sessions[lowest].list.length < sessions[s].maxParticipants) {
assign(s,id,lowest);
} else if (second !== 0 && !participants[id].assigned[second] && sessions[s].sessions[second].active === 'TRUE' && sessions[s].sessions[second].list.length < sessions[s].maxParticipants) {
assign(s,id,second);
} else {
for (num in participants[id].assigned) {
if (!participants[id].assigned[num] && sessions[s].sessions[num].active === 'TRUE' && sessions[s].sessions[num].list.length < sessions[s].maxParticipants) {
assign(s,id,num);
break;
};
}
}
}
}
}
count++;
}
setProperty(PARTICIPANTS, participants);
setProperty(SESSIONS, sessions);
displayParticipants(participants);
// console.log(students);
// console.log(workshops);
return "Assigned sessions to workshops. View on 'Participant Assignments' tab."
}
function randomAssign() {
const participants = getProperty(PARTICIPANTS);
const sessions = getProperty(SESSIONS);
const participantArray = [];
for (id in participants) {
for (a in participants[id].assigned) {
if (!participants[id].assigned[a]) {
participantArray.push([id, a]);
}
}
}
const freeSessionsArray = [];
for (s in sessions) {
for (l in sessions[s].sessions) {
if (sessions[s].sessions[l].active === 'TRUE' && sessions[s].sessions[l].list.length < sessions[s].maxParticipants) {
freeSessionsArray.push([s,l]);
}
}
}
// console.log(participantArray.length);
let i = 0;
while (participantArray.length > 0) {
console.log(i);
console.log(participantArray[i][0], participantArray[i][1]);
const relevantSessions = freeSessionsArray.filter((elem) => elem[1] === participantArray[i][1]);
const randomValue = getRandomInt(0, relevantSessions.length);
const randomSession = relevantSessions[randomValue][0];
const sessionNum = relevantSessions[randomValue][1];
const current = participants[participantArray[i][0]];
console.log(randomSession, sessionNum);
// console.log(current, participantArray[i][1]);
const duplicate = hasSession(current, randomSession);
if (duplicate) {
i++;
if (i >= participantArray.length) i=0;
continue;
};
if (sessions[randomSession].sessions[sessionNum].list.length < sessions[randomSession].maxParticipants) {
current.assigned[participantArray[i][1]] = randomSession;
sessions[randomSession].sessions[sessionNum].list.push(current.name);
console.log(`Assigning ${randomSession} to ${current.name} at session ${participantArray[i][1]}`);
participantArray.splice(i, 1);
}
if (sessions[randomSession].sessions[sessionNum].list.length >= sessions[randomSession].maxParticipants) {
console.log(`Removing ${freeSessionsArray[randomValue][0]} from queue.`);
const findSession = freeSessionsArray.findIndex((elem) => elem[0] === randomSession && elem[1] === sessionNum);
freeSessionsArray.splice(findSession,1);
};
i++;
if (i >= participantArray.length) i=0;
};
setProperty(PARTICIPANTS, participants);
setProperty(SESSIONS, sessions);
displayParticipants(participants);
return "Allocated remaining open slots to participants. View on 'Participant Assignments' tab."
}