-
Notifications
You must be signed in to change notification settings - Fork 2
/
Code.gs
101 lines (84 loc) · 3.57 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
function main() {
var events = listAllDayEvents();
var dateTime = findHolidayDate(events);
setNewVacationSettings(dateTime);
}
function setNewVacationSettings(dateTime) {
// if you wants to save the existing VacationSettings
// var vacationSetting = Gmail.Users.Settings.getVacation('me');
if (dateTime.start == undefined || dateTime.end == undefined) {
Logger.log("No event needs processing, no vacation response set, disabling AutoReply");
var vacationSetting = Gmail.newVacationSettings();
vacationSetting.enableAutoReply = false;
Gmail.Users.Settings.updateVacation(vacationSetting, 'me');
return;
}
const displayName = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].displayName;
const escalationName = "";
const escalationContact = "";
const endDate = (dateTime.end.getUTCMonth() + 1 /** Jan starts with 0 */) + "/" + dateTime.end.getUTCDate() + "/" + dateTime.end.getUTCFullYear();
const signature = "";
// create a new VacationSettings
var vacationSetting = Gmail.newVacationSettings();
vacationSetting.enableAutoReply = true;
vacationSetting.responseSubject = "Out of Office Notice";
vacationSetting.responseBodyPlainText = "Hi" +
"\n\nThank you for your message. I am currently out of the office and will be returning on " +
endDate + "." +
"\nFor any urgent inquiery please reach out to " +
escalationName + " " + escalationContact +
"\n\n" + displayName +
"\n\n" + signature;
vacationSetting.startTime = dateTime.start.getTime();
vacationSetting.endTime = dateTime.end.getTime();
// good place to place break point to check
// Logger.log((vacationSetting));
Gmail.Users.Settings.updateVacation(vacationSetting, 'me');
}
/**
* Find start and end date of this event corresponding to particular event.summary
* In this example we use "Vacation"
* @param {Object[]}
* @return {Object} start and end, Date object
*/
function findHolidayDate(events) {
var duration = {};
events.forEach(function(event) {
//Logger.log('%s %s (%s - %s)', event.getTitle(), event.getCreators(), event.getStartTime(), event.getEndTime());
duration["start"] = ((event.getStartTime()));
duration["end"] = ((event.getEndTime()));
});
return duration;
}
/**
* Obtain all day events only
* @return {Object[]} array of event from 'primary calendar'
*/
function listAllDayEvents() {
var calendarId = 'primary';
// search for next 24 hours
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
var events = CalendarApp.getDefaultCalendar().getEventsForDay(tomorrow)
var eventsOfInterest = events.filter(function(event){
//Logger.log('%s %s (%s - %s)', event.getTitle(), event.getCreators(), event.getStartTime(), event.getEndTime());
return event.getTitle() === 'OOO' || event.getTitle() === 'PTO';
});
return eventsOfInterest;
}
/**
* LEGACY CODE
* Apps Script does not supports RFC 3339 dates properly.
* Parses RFC 3339 date from calendar event and returns a corresponding Date object.
* See detailed explanataion in https://code.google.com/p/google-apps-script-issues/issues/detail?id=3860
* @param {string} string The RFC 3339 string to parse.
* @return {Date} The parsed date.
*/
/*
function parseRFC3339Date(string) {
var stringParts = string.split('T');
stringParts[0] = stringParts[0].replace(/-/g, '/');
return new Date(stringParts.join(' '));
}
*/