-
Notifications
You must be signed in to change notification settings - Fork 2
/
Code.gs
139 lines (117 loc) · 3.79 KB
/
Code.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
const SHEET_NAME = 'Página1';
const LOG_LINE = 14;
const LOG_COLUMN = 4;
const firstDoseLocations = {
currentCount: {
line: 8,
column: 1
},
totalCount: {
line: 8,
column: 2
},
users: {
line: 8,
column: 3
}
}
const secondDoseLocations = {
currentCount: {
line: 12,
column: 1
},
totalCount: {
line: 12,
column: 2
},
users: {
line: 12,
column: 3
}
}
function doPost(e){
var req = null;
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(SHEET_NAME);
sheet.getRange(LOG_LINE, LOG_COLUMN).setValue(JSON.stringify(e));
var parameter = e.parameter;
var command = parameter.command;
var user = parameter.text;
var text = null;
if (user) {
if (command === '/vacina1dose') {
text = firstDose(sheet, user);
} else if (command === '/vacina2dose') {
text = secondDose(sheet, user);
}
}
var progressBarText = progressBars(sheet);
req = text ? text + '\n\n' + progressBarText : progressBarText;
Logger.log(req);
} catch (error) {
Logger.log(error);
}
var output = JSON.stringify({
"text": req,
"response_type": "in_channel",
})
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}
function firstDose(sheet, user) {
var text;
var alreadyTookFirstDose = alreadyTookDose(sheet, user, firstDoseLocations);
if (alreadyTookFirstDose) {
text = 'O usuário ' + user + ' já foi vacinado com a *primeira* dose.';
} else {
incrementDoseCount(sheet, user, firstDoseLocations)
text = 'O usuário ' + user + ' foi registrado com sucesso para a *primeira* dose!'
}
return text;
}
function secondDose(sheet, user) {
var text;
var alreadyTookFirstDose = alreadyTookDose(sheet, user, firstDoseLocations);
var alreadyTookSecondDose = alreadyTookDose(sheet, user, secondDoseLocations);
if (!alreadyTookFirstDose) {
text = 'O usuário ' + user + ' ainda não foi vacinado com a primeira dose, não foi possível registrar a segunda.';
} else if (alreadyTookSecondDose) {
text = 'O usuário ' + user + ' já foi vacinado com a *segunda* dose.';
} else {
incrementDoseCount(sheet, user, secondDoseLocations)
text = 'O usuário ' + user + ' foi registrado com sucesso para a *segunda* dose!'
}
return text;
}
function incrementDoseCount(sheet, user, locations) {
sheet.getRange(locations.currentCount.line, locations.currentCount.column).setValue(
sheet.getRange(locations.currentCount.line, locations.currentCount.column).getValue() + 1
);
sheet.getRange(locations.users.line, locations.users.column).setValue(
sheet.getRange(locations.users.line, locations.users.column).getValue() + ', ' + user
);
}
function alreadyTookDose(sheet, user, locations) {
var users = sheet.getRange(locations.users.line, locations.users.column).getValue();
return users.includes(user);
}
function progressBars(sheet) {
var firstDoseBar = progressBar(sheet, firstDoseLocations);
var secondDoseBar = progressBar(sheet, secondDoseLocations);
return 'População da Magrathea Vacinada\n' + firstDoseBar + '\nPopulação da Magrathea Totalmente Vacinada\n' + secondDoseBar
}
function progressBar(sheet, locations) {
var total = sheet.getRange(locations.totalCount.line, locations.totalCount.column).getValue();
var current = sheet.getRange(locations.currentCount.line, locations.currentCount.column).getValue();
var current_percent = Math.round((current / total) * 100);
var progress_bar = [];
for (n = 0; n < 20; n++) {
if (current_percent < (n+1)*5) {
progress_bar.push("░");
} else {
progress_bar.push("▓");
}
}
var result = progress_bar.join('') + ' ' + current_percent + '% (' + current + '/' + total + ')';
return result;
}