-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitemNotification.gs
141 lines (128 loc) · 5.22 KB
/
itemNotification.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
function weeklyNotification() {
// GetDate
var today = new Date();
var sevenDaysAfter = new Date();
sevenDaysAfter.setDate(today.getDate() + 7);
//To do processing
var toDoItem = getFormComponentByTitle(title="This Week To Do")[0]
var toDoItemJSON = convertStringToJson(toDoItem.getHelpText())
for (var goal of Object.keys(toDoItemJSON)){
var title = "【" + goal + "】" + toDoItemJSON[goal].map(item => item.replace(/([\s\S]*?) - [\s\S]*/, "$1")).join(" | ")
console.log(title)
var event = CalendarApp.getDefaultCalendar().createAllDayEvent(title, today, sevenDaysAfter);
}
// Calendar Event Analysis and Notification
var hoursTaken = calendarEventAnalysis()
notificationMailer(toDoItem.getHelpText(), hoursTaken)
}
function calendarEventAnalysis() {
// Init
var eventThisWeek = {"Work":[], "Meeting":[], "SideProject":[],"Life":[],"Learn":[]}
// Time Span
var today = new Date();
var sevenDaysAgo = new Date();
sevenDaysAgo.setDate(today.getDate() - 7);
var calendars = CalendarApp.getAllCalendars();
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
var calendarId = calendar.getId()
switch(calendarId){
case WORK_CALENDAR_A:
var msftEvents = calendar.getEvents(sevenDaysAgo, today);
if (msftEvents.length > 0) {
Logger.log('Events from calendar "' + calendar.getName() + '" in the past 7 days between 8am and 7pm:');
for (var j = 0; j < msftEvents.length; j++) {
var event = msftEvents[j];
var startTime = event.getStartTime();
var endTime = event.getEndTime();
// Check if the event falls between 8am and 7pm
if (startTime.getHours() >= 8 && endTime.getHours() <= 20) {
if (!event.getTitle().match(MeetingNotAttending))
eventThisWeek['Meeting'].push(event)
}
}
}
break;
case WORK_CALENDAR_B:
var events = calendar.getEvents(sevenDaysAgo, today);
if (events.length > 0) events.forEach(event => eventThisWeek['Work'].push(event))
break;
case SIDE_PROJECT_CALENDAR:
var events = calendar.getEvents(sevenDaysAgo, today);
if (events.length > 0) events.forEach(event => eventThisWeek['SideProject'].push(event))
break;
case LIFE_CALENDAR:
var events = calendar.getEvents(sevenDaysAgo, today);
if (events.length > 0) events.forEach(event => eventThisWeek['Life'].push(event))
break;
case LEARN_CALENDAR:
var events = calendar.getEvents(sevenDaysAgo, today);
if (events.length > 0) events.forEach(event => eventThisWeek['Learn'].push(event))
break;
}
}
var hoursTaken = countHoursByGroup(eventThisWeek)
return hoursTaken
}
function countHoursByGroup(jsonData) {
var groups = Object.keys(jsonData);
var hoursTaken = {};
// Initialize hoursTaken object
groups.forEach(function (group) {
hoursTaken[group] = 0;
});
// Process events for each group
groups.forEach(function (group, index) {
var events = jsonData[group];
console.log(`=========================${group}=========================`)
events.forEach(function (event) {
if (!event.isAllDayEvent()) {
console.log(`${event.getTitle()} spend ${Math.round((event.getEndTime() - event.getStartTime())/1800000)/2} hours`)
hoursTaken[group] += Math.round((event.getEndTime() - event.getStartTime())/1800000)/2;
}
});
});
// Logger.log("Hours taken in each group:");
// groups.forEach(function (group) {
// Logger.log(group + ": " + hoursTaken[group] + " hours");
// });
console.log(hoursTaken)
return hoursTaken
}
function notificationMailer(toDoItemStr, hoursTaken={ "Work": 18, "Meeting": 10.5, "SideProject": 25, "Life": 16.5, "Learn": 0 }) {
// Data Processing
var groups = Object.keys(hoursTaken);
var pct = {}
var highestHour = 0
Logger.log("Hours taken in each group:");
groups.forEach(function (group) {
Logger.log(group + ": " + hoursTaken[group] + " hours");
if(hoursTaken[group]>highestHour) highestHour = hoursTaken[group]
});
groups.forEach(group => pct[group] = Math.round((hoursTaken[group]/highestHour)*100));
// Recording
var recordSheet = getRecordSpreadsheet().getSheetByName("WeeklyAnalysis")
recordSheet.insertRowAfter(1)
recordSheet.getRange("A2:F2").setValues([[
hoursTaken['Work'], hoursTaken['Meeting'], hoursTaken['SideProject'], hoursTaken['Life'], hoursTaken['Learn'], generateTimestamp()
]])
// Email Generation
var template = HtmlService.createTemplateFromFile('email_template');
template.toDoItemStrArr = toDoItemStr.split(/\n/).map(str => str.replace(/[0-9]*. ([\s\S]*)/, "$1"))
template.hoursTaken = hoursTaken;
template.pct = pct;
var htmlBody = template.evaluate().getContent();
//GetChart
var charts = recordSheet.getCharts();
var chartBlobs = new Array(charts.length);
var emailImages={};
for(var i=0;i<charts.length;i++){
chartBlobs[i]= charts[i].getAs("image/png").setName("chartBlob"+i);
htmlBody += "<img src='cid:chart"+i+"'><br>";
emailImages["chart"+i]= chartBlobs[i];
}
GmailApp.sendEmail(EMAIL_LIST, 'Your Weekly Progress Report', '', {
htmlBody: htmlBody,
inlineImages:emailImages
});
}