-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeslots.gs
170 lines (137 loc) · 5.43 KB
/
timeslots.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
function findCommonFreeTime() {
// Configuration
const CALENDARS = [
{
// Calendar IDs obscured
}
];
const DAYS_TO_CHECK = 7;
const SLOT_DURATION = 30;
const WEEKDAY_RANGES = [
{ start: 07, end: 23 }
];
const WEEKEND_RANGES = [
{ start: 07, end: 23 }
];
const COLORS = {
BUSY: '#FF9999',
FREE: '#99FF99',
COMMON_FREE: '#00FF00',
HEADER: '#E6E6E6'
};
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1JejWq0H90CR5CJ26A7Kpf2UsqUyqTQV_bIPP7wkVOzA/edit?gid=2092247677#gid=2092247677');
const sheet = ss.getSheetByName('Availability Grid') || ss.insertSheet('Availability Grid');
try {
const startDate = new Date();
startDate.setHours(0, 0, 0, 0);
const endDate = new Date(startDate.getTime() + (DAYS_TO_CHECK * 24 * 60 * 60 * 1000));
const calendarObjects = [];
const errorMessages = [];
for (const calendarConfig of CALENDARS) {
try {
let calendar;
if (calendarConfig.id === 'primary') {
calendar = CalendarApp.getDefaultCalendar();
} else {
calendar = CalendarApp.getCalendarById(calendarConfig.id);
}
if (!calendar) {
errorMessages.push(`Could not access calendar for ${calendarConfig.name}`);
continue;
}
calendarObjects.push({
calendar: calendar,
name: calendarConfig.name
});
} catch (e) {
errorMessages.push(`Error accessing calendar for ${calendarConfig.name}: ${e.toString()}`);
}
}
if (calendarObjects.length === 0) {
throw new Error('No calendars could be accessed. ' + errorMessages.join('; '));
}
sheet.clear();
const headers = ['Date', 'Time'];
calendarObjects.forEach(({name}) => headers.push(name));
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
sheet.getRange(1, 1, 1, headers.length)
.setBackground(COLORS.HEADER)
.setFontWeight('bold')
.setHorizontalAlignment('center');
const calendarEvents = [];
for (const {calendar} of calendarObjects) {
try {
const events = calendar.getEvents(startDate, endDate);
calendarEvents.push(events);
} catch (e) {
Logger.log(`Error getting events: ${e.toString()}`);
calendarEvents.push([]);
}
}
let currentRow = 2;
for (let d = 0; d < DAYS_TO_CHECK; d++) {
const currentDate = new Date(startDate.getTime() + (d * 24 * 60 * 60 * 1000));
const dayOfWeek = currentDate.getDay();
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
const timeRanges = isWeekend ? WEEKEND_RANGES : WEEKDAY_RANGES;
for (const range of timeRanges) {
for (let hour = range.start; hour < range.end; hour++) {
for (let minute = 0; minute < 60; minute += SLOT_DURATION) {
const slotStart = new Date(currentDate.getTime());
slotStart.setHours(hour, minute, 0, 0);
const slotEnd = new Date(slotStart.getTime() + SLOT_DURATION * 60000);
const rowData = [
Utilities.formatDate(currentDate, Session.getScriptTimeZone(), 'MM/dd/yyyy'),
Utilities.formatDate(slotStart, Session.getScriptTimeZone(), 'HH:mm')
];
const availability = [];
calendarEvents.forEach(events => {
const isBusy = events.some(event => {
const eventStart = event.getStartTime();
const eventEnd = event.getEndTime();
return slotStart < eventEnd && slotEnd > eventStart;
});
availability.push(isBusy);
});
availability.forEach(() => rowData.push(''));
const range = sheet.getRange(currentRow, 1, 1, rowData.length);
range.setValues([rowData]);
for (let i = 0; i < availability.length; i++) {
const cell = sheet.getRange(currentRow, i + 3);
cell.setBackground(availability[i] ? COLORS.BUSY : COLORS.FREE);
}
if (availability.every(isBusy => !isBusy)) {
range.setBackground(COLORS.COMMON_FREE);
}
currentRow++;
}
}
}
}
sheet.autoResizeColumns(1, headers.length);
sheet.setFrozenRows(1);
sheet.setFrozenColumns(2);
const legendRow = currentRow + 1;
sheet.getRange(legendRow, 1).setValue('Legend:');
sheet.getRange(legendRow, 2).setValue('Busy').setBackground(COLORS.BUSY);
sheet.getRange(legendRow, 3).setValue('Free').setBackground(COLORS.FREE);
sheet.getRange(legendRow, 4).setValue('Common Free Time').setBackground(COLORS.COMMON_FREE);
} catch (error) {
Logger.log('Error: ' + error.toString());
sheet.getRange('A1').setValue('Error: ' + error.toString());
}
}
function createTrigger() {
try {
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => ScriptApp.deleteTrigger(trigger));
ScriptApp.newTrigger('findCommonFreeTime')
.timeBased()
.everyDays(1)
.atHour(1)
.create();
SpreadsheetApp.getActiveSpreadsheet().toast('Daily update trigger created successfully', 'Success');
} catch (error) {
SpreadsheetApp.getActiveSpreadsheet().toast('Error creating trigger: ' + error.toString(), 'Error');
}
}