-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleScript.gs
155 lines (135 loc) · 4.66 KB
/
googleScript.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
/**
* Main function run at spreadsheet opening
*/
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{name: "Initialize", functionName: "init"},
{name: "Archive Gmail Messages", functionName: "ScanGmail"}
];
ss.addMenu("Gmail Archiver", menuEntries);
}
/**
* Initialize the system
*/
function init() {
// Create the needed Gmail label
GmailApp.createLabel("Archive to Drive");
// Create Google Drive folder if doesn't exists
try {
var folder = DriveApp.getFoldersByName("Email Archive");
} catch(e) {
// Folder doesn't exists
DriveApp.createFolder("Email Archive");
}
Browser.msgBox("Created Gmail label: Archive to Drive and Google Drive folder: Email Archive");
}
/**
* Scan Gmail account for message archive requests
*/
function ScanGmail() {
// Default Drive folder where to archive messages
var baseFolder = "Email Archive";
// Get the label
var label = GmailApp.getUserLabelByName("Archive to Drive");
var threadsArr = getThreadsForLabel(label);
for(var j=0; j<threadsArr.length; j++) {
var messagesArr = getMessagesforThread(threadsArr[j]);
for(var k=0; k<messagesArr.length; k++) {
var messageId = messagesArr[k].getId();
var messageDate = Utilities.formatDate(messagesArr[k].getDate(), Session.getTimeZone(), "dd/MM/yyyy - HH:mm:ss");
var messageFrom = messagesArr[k].getFrom();
var messageSubject = messagesArr[k].getSubject();
var messageBody = messagesArr[k].getBody();
var messageAttachments = messagesArr[k].getAttachments();
// Create the new folder to contain the message
var newFolderName = messageId; //messageDate + " - " + messageFrom + " - " + messageSubject + " - " +
if (messageAttachments.length != 0) {
var newFolder = createDriveFolder(baseFolder, newFolderName);
// Save attachments
for(var i = 0; i < messageAttachments.length; i++) {
var attachmentName = messageAttachments[i].getName();
var attachmentContentType = messageAttachments[i].getContentType();
var attachmentBlob = messageAttachments[i].copyBlob();
newFolder.createFile(attachmentBlob);
}
}
// Create the message PDF inside the new folder
//var htmlBodyFile = newFolder.createFile('body.html', messageBody, "text/html");
//var pdfBlob = htmlBodyFile.getAs('application/pdf');
//pdfBlob.setName(newFolderName + ".pdf");
//newFolder.createFile(pdfBlob);
//htmlBodyFile.setTrashed(true);
}
// Remove Gmail label from archived thread
//label.removeFromThread(threadsArr[j]);
}
Browser.msgBox("Gmail messages successfully archived to Google Drive");
}
/**
* Find all user's Gmail labels that represent mail message
* movement requests es: moveto->[email protected]
*
* @return {GmailLabel[]} Array of GmailLabel objects
*/
function scanLabels() {
// logs all of the names of your labels
var labels = GmailApp.getUserLabels();
var results = new Array();
for (var i = 0; i < labels.length; i++) {
if(labels[i].getName() == "Archive to Drive") {
results.push(labels[i]);
}
}
return results;
}
/**
* Get all Gmail threads for the specified label
*
* @param {GmailLabel} label GmailLabel object to get threads for
* @return {GmailThread[]} an array of threads marked with this label
*/
function getThreadsForLabel(label) {
var threads = label.getThreads();
return threads;
}
/**
* Get all Gmail messages for the specified Gmail thread
*
* @param {GmailThread} thread object to get messages for
* @return {GmailMessage[]} an array of messages contained in the specified thread
*/
function getMessagesforThread(thread) {
var messages = thread.getMessages();
return messages;
}
/**
* Get methods of an object
* @param {Object} object to scan
* @return {Array} object's methods
*/
function getMethods(obj) {
var result = [];
for (var id in obj) {
try {
if (typeof(obj[id]) == "function") {
result.push(id + ": " + obj[id].toString());
}
} catch (err) {
result.push(id + ": inaccessible");
}
}
return result;
}
/**
* Create a Google Drive Folder
*
* @param {String} baseFolder name of the base folder
* @param {String} folderName name of the folder
* @return {Folder} the folder object created representing the new folder
*/
function createDriveFolder(baseFolder, folderName) {
var baseFolderObject = DriveApp.getFoldersByName(baseFolder);
var tempFolder = baseFolderObject.next();
return tempFolder.createFolder(folderName);
}