-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks.gs
More file actions
233 lines (199 loc) · 5.89 KB
/
tasks.gs
File metadata and controls
233 lines (199 loc) · 5.89 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Script to create tasks from Gmail label
* Based on: https://qmacro.org/2011/10/04/automated-email-to-task-mechanism-with-google-apps-script/
* Author: tedsteinmann
*/
// Setting primary inputs - Configuration constants
const TASKLIST = 'Personal';
const LABEL_PENDING = '@Task';
const LABEL_DONE = 'Tasked';
/**
* Primary function to process the pending task emails
*/
function processTasks() {
try {
processPending_();
} catch (e) {
Logger.log('ERROR: Failed to process tasks: %s', e.message);
}
}
/**
* Testing and debugging functions
*/
function listTasksByName() {
const tasklistId = getTasklistId_(TASKLIST);
if (tasklistId) {
listTasks_(tasklistId);
} else {
Logger.log('ERROR: Tasklist "%s" not found', TASKLIST);
}
}
function addTestTask() {
const tasklistId = getTasklistId_(TASKLIST);
if (tasklistId) {
addTask_('test', 'Test task note', tasklistId);
} else {
Logger.log('ERROR: Tasklist "%s" not found', TASKLIST);
}
}
/**
* Private functions
*/
/**
* Get tasklist ID by name
* @param {string} tasklistName - The name of the tasklist
* @return {string|null} The tasklist ID or null if not found
*/
function getTasklistId_(tasklistName) {
// Input validation
if (!tasklistName || typeof tasklistName !== 'string') {
Logger.log('ERROR: Invalid tasklist name');
return null;
}
try {
const tasklistsList = Tasks.Tasklists.list();
if (!tasklistsList) {
Logger.log('ERROR: Failed to retrieve tasklists');
return null;
}
const taskLists = tasklistsList.getItems();
if (!taskLists || taskLists.length === 0) {
Logger.log('WARNING: No tasklists found');
return null;
}
for (const taskList of taskLists) {
if (taskList && taskList.getTitle() === tasklistName) {
return taskList.getId();
}
}
Logger.log('WARNING: Tasklist "%s" not found', tasklistName);
return null;
} catch (e) {
Logger.log('ERROR: Failed to get tasklist ID: %s', e.message);
return null;
}
}
/**
* List all tasks in a tasklist
* @param {string} taskListId - The tasklist ID
*/
function listTasks_(taskListId) {
// Input validation
if (!taskListId || typeof taskListId !== 'string') {
Logger.log('ERROR: Invalid tasklist ID');
return;
}
try {
const tasks = Tasks.Tasks.list(taskListId);
if (tasks && tasks.items && tasks.items.length > 0) {
for (let i = 0; i < tasks.items.length; i++) {
const task = tasks.items[i];
if (task) {
Logger.log('Task with title "%s" and ID "%s" was found.',
task.title, task.id);
}
}
} else {
Logger.log('No tasks found.');
}
} catch (e) {
Logger.log('ERROR: Failed to list tasks: %s', e.message);
}
}
/**
* Adds a task to a tasklist with due date set to tomorrow
* @param {string} title - The task title
* @param {string} taskNote - The task notes/description
* @param {string} taskListId - The tasklist ID to add to
*/
function addTask_(title, taskNote, taskListId) {
// Input validation
if (!title || typeof title !== 'string') {
Logger.log('ERROR: Invalid task title');
return;
}
if (!taskListId || typeof taskListId !== 'string') {
Logger.log('ERROR: Invalid tasklist ID');
return;
}
try {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const dueDate = Utilities.formatDate(tomorrow, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
const task = {
title: title,
notes: taskNote || '',
due: dueDate
};
const createdTask = Tasks.Tasks.insert(task, taskListId);
Logger.log('Task created: %s', createdTask.title);
} catch (e) {
Logger.log('ERROR: Failed to create task: %s', e.message);
}
}
/**
* Process pending task emails - creates tasks from labeled emails
* Iterates through emails with LABEL_PENDING, creates tasks, and relabels them
*/
function processPending_() {
const label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
const label_done = GmailApp.getUserLabelByName(LABEL_DONE);
// Validate labels exist
if (!label_pending) {
Logger.log('ERROR: Pending label "%s" not found. Please create the label first.', LABEL_PENDING);
return;
}
if (!label_done) {
Logger.log('ERROR: Done label "%s" not found. Please create the label first.', LABEL_DONE);
return;
}
// Get tasklist ID
const taskListId = getTasklistId_(TASKLIST);
if (!taskListId) {
Logger.log('ERROR: Tasklist "%s" not found. Cannot create tasks.', TASKLIST);
return;
}
// The threads currently assigned to the 'pending' label
let threads;
try {
threads = label_pending.getThreads();
} catch (e) {
Logger.log('ERROR: Failed to get threads: %s', e.message);
return;
}
if (!threads || threads.length === 0) {
Logger.log('No pending tasks to process');
return;
}
let successCount = 0;
let failureCount = 0;
// Process each thread, assuming there's only a single message in each thread
for (let i = 0; i < threads.length; i++) {
const thread = threads[i];
if (!thread) {
Logger.log('WARNING: Null thread at index %s', i);
continue;
}
try {
// Grab the task data
const taskTitle = thread.getFirstMessageSubject();
const taskNote = 'Email: ' + thread.getPermalink();
// Validate task title
if (!taskTitle || taskTitle.trim().length === 0) {
Logger.log('WARNING: Skipping thread with empty subject');
continue;
}
// Insert the task
addTask_(taskTitle, taskNote, taskListId);
// Set to 'done' by exchanging labels
thread.removeLabel(label_pending);
thread.addLabel(label_done);
successCount++;
} catch (e) {
Logger.log('ERROR: Failed to process thread: %s', e.message);
failureCount++;
}
}
// Log summary
Logger.log('Processed %s tasks successfully, %s failures', successCount, failureCount);
}