-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutdown.gs
52 lines (39 loc) · 1.62 KB
/
Shutdown.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
function getDaySchedule(rows, date) {
let weekday = date.getDay() || 7;
let result = [];
rows.forEach(function (value) {
if (value[0] === weekday) {
let /** @type {Date} */ newDate = value[1];
newDate.setFullYear(date.getFullYear());
newDate.setDate(date.getDate());
newDate.setMonth(date.getMonth());
result.push(newDate);
}
});
return result;
}
function getNextShutdown() {
let rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
let today = new Date();
let tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
let schedule = [...getDaySchedule(rows, today), ...getDaySchedule(rows, tomorrow)];
for (let i = 0; i < schedule.length; i++) {
var value = schedule[i];
var diff = Math.ceil((value - today) / 60000);
if (diff > 0) {
break;
}
}
let afterHours = Math.floor(diff / 60);
let afterMinutes = diff % 60;
let hoursLabel = getText(afterHours, "годину", "години", "годин");
let minutesLabel = getText(afterMinutes, "хвилину", "хвилини", "хвилин");
let day = value.getDay() == today.getDay() ? "сьогодні" : "завтра";
let preposition = value.getHours() == 11 ? "об" : "о";
let time = timeString(value, false);
let afterHoursFull = afterHours ? `${afterHours} ${hoursLabel} ` : "";
let afterMinutesFull = afterMinutes || !afterHours ? `${afterMinutes} ${minutesLabel}` : "";
let msg = `Наступне стабілізаційне відключення:\n*${day} ${preposition} ${time}*\n_через ${afterHoursFull}${afterMinutesFull}_`;
return msg;
}