-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
273 lines (191 loc) · 11.5 KB
/
Code.js
File metadata and controls
273 lines (191 loc) · 11.5 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Adapted from https://www.gmass.co/blog/add-needs-reply-label/
INSTRUCTIONS FOR PERSONALIZATION:
In the main() function, replace YOUREMAILHERE with your email address. Importantly, write it inside the quotation marks -- the script won't work if you delete them.
LABELS TO HAVE IN GMAIL (or re-write this code for the labels you have):
..Received/Received today
..Received/Received yesterday
..Received/Received past few days
Note there should be an outer label named "..Received" and three inner labels named "Received today", etc.
CODE SECTIONS:
RECEIVED TODAY:
received_today() un-labels all already-labelled messages then does labelling for inbox's messages received or snoozed until today
received_today_me() is tweaked version of today to catch emails I sent myself
RECEIVED YESTERDAY:
received_yest() un-labels all already-labelled messages then does labelling for inbox's messages received or snoozed until yesterday
received_yest_me() is tweaked version of yest to catch emails I sent myself
RECEIVED PAST FEW DAYS:
received_few_days_ago() un-labels all already-labelled messages then does labelling for inbox's messages received or snoozed until between four days ago and three days ago
received_few_days_ago_me() is tweaked version to catch emails I sent myself
Note running main() calls all six functions, so you can set up a time trigger for this script (e.g., mine runs every hour) and call only this function, and you'll run each of the others. For information about time triggers, see here: https://developers.google.com/apps-script/guides/triggers/installable#manage_triggers_manually.
I think the time zones for the script are UTC, but I'm not quite sure. So, the whole today/tomorrow/past few days distinction will be a bit off, but I've never had an issue with it and haven't looked too much into making the script operate in my time zone. If you want to fix the time zone thing for yourself, this exchange might be a good starting point: https://stackoverflow.com/a/18597149.
If you want to change how the labels are named -- rather than using my names, you can do so by renaming them in the functions, or you could create variables of strings of their names in the main() function and pass that into the received...() functions the way I pass in the my_email variable (though note you'll need to replace the parts of the strings where I wrote the names of the label variables by concatenating the truncated strings -- once you remove what I have written -- with your variable names). It's not too a hard fix... though I'll leave it to you.
Also, if you want to further improve the code, you could make a function that does the substance of the unlabelling and relabeling then a function that calls that main function on the six received...() functions. Or maybe that would be the main() function. Up to you. And if you want to really improve it, you might look into whether you can parallelize any of it. I'm not sure whether Google Apps Script has that functionality, but if it does, that might be cool and would help with efficiency.
All that said, I might implement these efficienty boosts and re-commit later... but to be determined.
*/
function main() {
var my_email = "YOUREMAILHERE"
received_today()
received_today_me(my_email)
received_yest()
received_yest_me(my_email)
received_few_days_ago()
received_few_days_ago_me(my_email)
}
function received_today() {
//set label
var received_today_label = GmailApp.getUserLabelByName("..Received/Received today");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('label:..Received/Received today');
received_today_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('in:trash AND label:..Received/Received today');
received_today_label.removeFromThreads(threads_already_labelled_trash);
// get today's date for labelling
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
const today_formatted = today.toLocaleDateString();
// pull threads to label and email info
var threads = GmailApp.search('in:inbox AND after:' + today_formatted);
var currentUserEmail = Session.getActiveUser().getEmail();
// do labelling
for (var i = 0; i < threads.length; i++) {
var lastMessage = threads[i].getMessages()[threads[i].getMessageCount() - 1];
var lastMessageFrom = lastMessage.getFrom();
//console.log(lastMessageFrom.indexOf(currentUserEmail));
if (lastMessageFrom.indexOf(currentUserEmail) == -1) {
//console.log("inside if statement and adding to thread...")
received_today_label.addToThread(threads[i]);
//console.log("added to thread")
}
else {
received_today_label.removeFromThread(threads[i]);
}
}
}
function received_today_me(my_email) {
//set label
var received_today_label = GmailApp.getUserLabelByName("..Received/Received today");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('from:' + my_email + ' AND label:..Received/Received today');
received_today_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('from:' + my_email + ' AND in:trash AND label:..Received/Received today');
received_today_label.removeFromThreads(threads_already_labelled_trash);
// get today's date for labelling
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
const today_formatted = today.toLocaleDateString();
// pull threads to label
var threads = GmailApp.search('in:inbox AND from:' + my_email + ' AND after:' + today_formatted);
// do labelling
received_today_label.addToThreads(threads);
}
function received_yest() {
//set label
var received_yest_label = GmailApp.getUserLabelByName("..Received/Received yesterday");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('label:..Received/Received yesterday');
received_yest_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('in:trash AND label:..Received/Received yesterday');
received_yest_label.removeFromThreads(threads_already_labelled_trash);
// get today's date
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
const today_formatted = today.toLocaleDateString();
// get yesterday's date
var yest = new Date();
yest.setDate(yest.getDate() - 1);
const yest_formatted = String(yest.toLocaleDateString());
// pul threads to label and email info
var threads = GmailApp.search('in:inbox AND after:' + yest_formatted + " AND before:" + today_formatted);
var currentUserEmail = Session.getActiveUser().getEmail();
// do labelling
for (var i = 0; i < threads.length; i++) {
var lastMessage = threads[i].getMessages()[threads[i].getMessageCount() - 1];
var lastMessageFrom = lastMessage.getFrom();
if (lastMessageFrom.indexOf(currentUserEmail) == -1) {
received_yest_label.addToThread(threads[i]);
}
else {
received_yest_label.removeFromThread(threads[i]);
}
}
}
function received_yest_me(my_email) {
//set label
var received_yest_label = GmailApp.getUserLabelByName("..Received/Received yesterday");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('from:' + my_email + ' AND label:..Received/Received yesterday');
received_yest_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('from:' + my_email + ' AND in:trash AND label:..Received/Received yesterday');
received_yest_label.removeFromThreads(threads_already_labelled_trash);
// get today's date
const timeElapsed = Date.now();
const today = new Date(timeElapsed);
const today_formatted = today.toLocaleDateString();
// get yesterday's date
var yest = new Date();
yest.setDate(yest.getDate() - 1);
const yest_formatted = String(yest.toLocaleDateString());
// pul threads to label and email info
var threads = GmailApp.search('in:inbox AND from:' + my_email + ' AND after:' + yest_formatted + " AND before:" + today_formatted);
// do labelling
received_yest_label.addToThreads(threads);
}
function received_few_days_ago() {
//set label
var received_few_days_ago_label = GmailApp.getUserLabelByName("..Received/Received past few days");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('label:..Received/Received past few days');
received_few_days_ago_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('in:trash AND label:..Received/Received past few days');
received_few_days_ago_label.removeFromThreads(threads_already_labelled_trash);
// get yesterday's date
var yest = new Date();
yest.setDate(yest.getDate() - 1);
const yest_formatted = String(yest.toLocaleDateString());
// get date from five days ago
var five_days_ago = new Date();
five_days_ago.setDate(yest.getDate() - 3);
const five_days_ago_formatted = String(five_days_ago.toLocaleDateString());
// pul threads to label and email info
var threads = GmailApp.search('in:inbox AND after:' + five_days_ago_formatted + " AND before:" + yest_formatted);
var currentUserEmail = Session.getActiveUser().getEmail();
// do labelling
for (var i = 0; i < threads.length; i++) {
var lastMessage = threads[i].getMessages()[threads[i].getMessageCount() - 1];
var lastMessageFrom = lastMessage.getFrom();
if (lastMessageFrom.indexOf(currentUserEmail) == -1) {
received_few_days_ago_label.addToThread(threads[i]);
}
else {
received_few_days_ago_label.removeFromThread(threads[i]);
}
}
}
function received_few_days_ago_me(my_email) {
//set label
var received_few_days_ago_label = GmailApp.getUserLabelByName("..Received/Received past few days");
//remove label already on any messages NOT in trash
var threads_already_labelled = GmailApp.search('from:' + my_email + ' AND label:..Received/Received past few days');
received_few_days_ago_label.removeFromThreads(threads_already_labelled);
//remove label already on any messages in trash (not re-adding the label given already deleted)
var threads_already_labelled_trash = GmailApp.search('from:' + my_email + ' AND in:trash AND label:..Received/Received past few days');
received_few_days_ago_label.removeFromThreads(threads_already_labelled_trash);
// get yesterday's date
var yest = new Date();
yest.setDate(yest.getDate() - 1);
const yest_formatted = String(yest.toLocaleDateString());
// get date from five days ago
var five_days_ago = new Date();
five_days_ago.setDate(yest.getDate() - 3);
const five_days_ago_formatted = String(five_days_ago.toLocaleDateString());
// pul threads to label and email info
var threads = GmailApp.search('in:inbox AND from:' + my_email + ' AND after:' + five_days_ago_formatted + " AND before:" + yest_formatted);
// do labelling
received_few_days_ago_label.addToThreads(threads);
}