-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_attachments.gs
90 lines (69 loc) · 3.13 KB
/
fetch_attachments.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
/*
* This script is used to fetch all attachments of new emails
* from inbox of user running it. Saving it to designated folder
* into subfolders with name of the subject of the email.
* Read messages then yeets to Archive.
*/
// Runtime //
function main() {
// get inbox threads and check if there is any new
// if yes, go through them and download attachments if there are any
Logger.log("Attachment fetcher starting...");
var inboxContent = GmailApp.getInboxThreads();
if(inboxContent.length == 0) {
//if no items detected, return
Logger.log("No new messages detected.");
return;
} else if(inboxContent.length > 0) {
//if there are items, go fetch attachments
//even in case of no attachments, archive those items
Logger.log(inboxContent.length + " new items in the inbox.\nFetching...");
var folderToSaveId = "1v2kqn2uNPk5D8aug__qSHFv9EDbHO49k";
// this is Id of folder to save attachments to
getAttachments(inboxContent, folderToSaveId);
moveToArchive(inboxContent);
}
}
// Functions //
function getAttachments(payload, folderId) {
// checks all threads passed as payload for attachments
// saves all attachments in file with date and subject of thread
for (var i = 0; i < payload.length; i++) {
var attachmentCount = 0;
var attachmentLog = "";
var thread = payload[i];
var messages = GmailApp.getMessagesForThread(thread);
//get all messages for current thread
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var attachments = message.getAttachments();
//get all attachments for current message
if (attachments.length > 0) {
//if there are any attachments, continue this branch
var firstDate = messages[0].getDate();
var messageDate = "[" + firstDate.getDay() + "." + (firstDate.getMonth() + 1) + "." + firstDate.getFullYear() + "]";
// string for name of new folder, prefix with date of thread
attachmentCount += attachments.length;
// increase count of attachments for logging purposes
var parentFolder = DriveApp.getFolderById(folderId);
var newFolder = parentFolder.createFolder(messageDate + " " + thread.getFirstMessageSubject());
//create folder with the messageDate and subject of first message
for(var n = 0; n < attachments.length; n++) {
//take each attachment and place it in the newFolder
var attachment = attachments[n];
var file = newFolder.createFile(attachment);
attachmentLog += " + Saved: " + file.getName() + "\n";
//log the operation of saving the file
}
}
}
Logger.log("+ " + attachmentCount + " of " + payload[i].getFirstMessageSubject());
if (attachmentLog) {Logger.log(attachmentLog)};
//log how many attachments did the thread had, eventually list them if they were saved
}
}
function moveToArchive(payload) {
GmailApp.moveThreadsToArchive(payload);
Logger.log("Archived " + payload.length + " items.");
//just to have this separated and logged
}