-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret-santa.gs
93 lines (84 loc) · 2.44 KB
/
secret-santa.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
function sendmail() {
var MAIL_SENT = "done";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var shuffled = iterate();
if (shuffled) {
var row = 7;
for(row=2;row<12;row++) {
var present_giver = sheet.getSheetValues(row, 1, 1, 1);
var address = sheet.getSheetValues(row, 2, 1, 1);
var receiver = sheet.getSheetValues(row, 4, 1, 1);
var advancedArgs = {name:"Present-lotterymachine",noReply:true};
//Uncomment next line to enable email sending.
//MailApp.sendEmail(address,"This year's Secret Santa","Hi " + present_giver +", this year 2022 you will give present to: " + receiver,advancedArgs);
//Uncomment next line to send test email.
//MailApp.sendEmail(address,"Test mail","Hi " + present_giver +", if everything goes well, you will soon get secret santa mail",advancedArgs);
sheet.getRange(row,5).setValue(MAIL_SENT);
}
}
}
function different_families(families, buyer, receiver) {
return families[buyer] != families[receiver];
}
function iterate() {
var isdone = false;
var iter = 0;
var maxiter = 5;
do {
isdone = shuffle();
iter++
}
while (!isdone && iter < maxiter)
return isdone;
}
function shuffle() {
var done = false;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var maxiter = 20;
var present_givers = sheet.getRange(2,1,10).getValues();
var families = sheet.getRange(2,3,10).getValues();
var lkm = present_givers.length;
var receivers = new Array(lkm);
receivers.length = lkm;
var used = new Array(lkm);
used.length = lkm;
for (i=0;i<used.length;i++) {
used[i] = false;
}
for (i=0;i<lkm;i++) {
var receiver = present_givers[i];
var iter = 0;
var found = false;
do
{
if (iter < maxiter) {
iter++;
var number = Math.random();
var j = Math.round((lkm-1)*number);
var difference = families[i]-families[j];
if (difference != 0) {
if (!used[j]) {
found = true;
receivers[j] = receiver;
used[j] = true;
}
}
}
else {
found=true;
}
}
while (!found)
}
done = true;
for (i=0;i<used.length;i++) {
if (used[i] == false) {
done = false;
break;
}
}
if (done) sheet.getRange(2, 4, lkm, 1).setValues(receivers)
return done;
}