-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloat.gs
184 lines (167 loc) · 4.75 KB
/
Float.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function run() {
advanceTasks();
removeUnsolicitedEvents();
}
function advanceTasks() {
var taskLists = getTaskLists();
for (i = 0; i < taskLists.length; i++) {
taskList = taskLists[i];
console.log('Found task list called %s', taskList.name);
overdueTasks = getOverdueTasks(taskList.id);
console.info("Found %d overdue tasks", overdueTasks.length);
for (j = 0; j < overdueTasks.length; j++) {
advanceTask(taskList.id, overdueTasks[j].id);
}
completedTasks = getCompletedTasks(taskList.id);
console.info("Found %d completed tasks", completedTasks.length);
for (j = 0; j < completedTasks.length; j++) {
console.log("Found completed task called %s", completedTasks[j].title);
if (addCalendarEvent( completedTasks[j].title, completedTasks[j].notes, completedTasks[j].completed, completedTasks[j].links )) {
deleteTask(taskList.id, completedTasks[j].id);
}
}
}
}
function getTaskLists() {
var taskLists = Tasks.Tasklists.list().getItems();
if (!taskLists) {
return [];
}
return taskLists.map(function(taskList) {
return {
id: taskList.getId(),
name: taskList.getTitle()
};
});
}
function getOverdueTasks(listId) {
var params = {
dueMax: formatDate(getToday()),
showCompleted: false,
showDeleted: false
};
var tasks = Tasks.Tasks.list(listId, params).getItems();
if (!tasks) {
return [];
}
return tasks.map(function(task) {
return {
id: task.getId(),
title: task.getTitle(),
due: task.getDue()
};
}).filter(function(task) {
return task.title;
});
}
function advanceTask(taskListId, taskId) {
task = Tasks.Tasks.get(taskListId, taskId);
dueDate = new Date(task.due);
console.log("Advancing task %s due at %s", task.title, dueDate);
while( dueDate < getToday() ) {
dueDate.setDate( dueDate.getDate() + 1 );
}
console.log("Task will now be due at %s", dueDate);
task.due = formatDate(dueDate);
Tasks.Tasks.update(task, taskListId, taskId)
}
function getToday() {
var today = new Date();
today.setDate(today.getDate()-1);
return today;
}
function getCompletedTasks(listId) {
var params = {
showCompleted: true,
showHidden: true,
completedMax: getEndOfDays(),
showDeleted: false
};
var tasks = Tasks.Tasks.list(listId, params).getItems();
if (!tasks) {
return [];
}
return tasks.map(function(task) {
return {
id: task.getId(),
title: task.getTitle(),
notes: task.getNotes(),
completed: task.getCompleted(),
links: task.getLinks()
};
}).filter(function(task) {
return task.title;
});
}
function getEndOfDays() {
date = new Date();
date.setDate( date.getDate() + (1000 * 365) );
return formatDate(date);
}
function addCalendarEvent(title, notes, date, links) {
if (links) {
if (!notes) {
notes = "Links:\n";
} else {
notes = notes + "\n\nLinks:\n"
}
for (i = 0; i < links.length; i++) {
notes = notes + links[i].type + ": " + links[i].link + "\n";
}
}
var event = {
start: {
dateTime: date,
},
end: {
dateTime: date,
},
summary: title,
transparency: "transparent",
description: notes
};
Calendar.Events.insert(event, Session.getActiveUser().getEmail());
return true;
}
function deleteTask(taskListId, taskId) {
Tasks.Tasks.remove(taskListId, taskId);
}
function removeUnsolicitedEvents() {
var events = getUnsolicitedEvents();
console.log("Found %s events that should be declined", events.length);
for (var i = 0; i < events.length; i++) {
var event = events[i];
console.log("Will decline \"%s\" created at %s and updated at %s", event.getTitle(), event.getDateCreated(), event.getLastUpdated());
event.setMyStatus(CalendarApp.GuestStatus.NO);
}
}
function getUnsolicitedEvents() {
var DAYS_MAX_LOOK_AHEAD = new Date(Date.now() + (14 * 24 * 60 * 60 * 1000));
var events = CalendarApp.getEvents(new Date(), DAYS_MAX_LOOK_AHEAD, {max: 200})
if (events.length == 0) {
console.log("Found no events");
return [];
}
return events.filter(function(event) {
return isLargeEvent(event) && hasNotResponded(event) && isNotNewInvite(event);
});
}
function isLargeEvent(event) {
return !event.guestsCanSeeGuests();
}
function hasNotResponded(event) {
return event.getMyStatus() == CalendarApp.GuestStatus.INVITED;
}
function isNotNewInvite(event) {
var DAYS_GRACE_PERIOD = new Date(Date.now() - (8 * 60 * 60 * 1000));
return event.getDateCreated() < DAYS_GRACE_PERIOD;
}
/**
* Return an RFC3339 formated date String corresponding to the given
* Date object.
* @param {Date} date a Date.
* @return {string} a formatted date string.
*/
function formatDate(date) {
return Utilities.formatDate(date, 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ssZ');
}