-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-blocking-automation.gs
88 lines (74 loc) · 2.52 KB
/
time-blocking-automation.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
const HALF_HOUR = 30 * 60 * 1000;
function main() {
var mainCalendar = CalendarApp.getDefaultCalendar();
var timeBlockingCalendar = CalendarApp.getCalendarsByName('Slots')[0];
const now = new Date() //.setHours(0, 0, 0, 0);
const nextWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7);
mainCalendar.getEvents(now, nextWeek).map((event) => {
createCommuteEvents(timeBlockingCalendar, event);
})
}
// var slotEvents = [
// {
// title: 'Slot Event 1',
// duration: (30 * 60 * 1000),
// when: 'before'
// },
// {
// title: 'Slot Event 2',
// duration: (30 * 60 * 1000),
// when: 'after'
// }
// ]
//
function slotExistsForEvent(slotsCalendar, event, slotEvents) {
// TODO: find a way to match commute events when using maps api to calculate time
var eventList = [];
for (const slot of slotEvents) {
var slotStartDate = slot.when === 'before'
? new Date(event.getStartTime().getTime() - slot.duration)
: event.getEndTime();
var slotEndDate = slot.when === 'before'
? event.getStartTime()
: new Date(event.getEndTime().getTime() + slot.duration);
eventList.push(slotsCalendar.getEvents(slotStartDate, slotEndDate, {search: slot.title}).length);
}
return !eventList.some((item) => item !== 1);
}
function createSlotEvents(slotsCalendar, event, slotEvents) {
if (event.getLocation() === null || event.getLocation() === "") {
return false;
}
if (slotExistsForEvent(slotsCalendar, event, slotEvents)) {
console.log('Evento já processado');
return false;
}
for (const slot of slotEvents) {
var slotStartDate = slot.when === 'before'
? new Date(event.getStartTime().getTime() - slot.duration)
: event.getEndTime();
var slotEndDate = slot.when === 'before'
? event.getStartTime()
: new Date(event.getEndTime().getTime() + slot.duration);
slotsCalendar.createEvent(slot.title, slotStartDate, slotEndDate);
}
}
function createCommuteEvents(commuteCalendar, event) {
var slotEvents = [
{
title: 'Commute',
duration: (30 * 60 * 1000),
when: 'before'
},
{
title: 'Commute home',
duration: (30 * 60 * 1000),
when: 'after'
}
];
// TODO: Check if event is recurrent and create commute events as recurrent as well
// TODO: Calculate time using the maps api
// https://www.bpwebs.com/get-google-map-travel-time-and-distance/
// https://gist.github.com/lankanmon/2c50ad3675c0c4a834fda4ce4f910582
createSlotEvents(commuteCalendar, event, slotEvents);
}