-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
106 lines (89 loc) · 3.28 KB
/
index.ts
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
import Folder = GoogleAppsScript.Drive.Folder;
import GmailLabel = GoogleAppsScript.Gmail.GmailLabel;
import GDate = GoogleAppsScript.Base.Date;
// NOTE: As a part of App Script syntax the `_` is appended to hide internal functions.
// Array of file extension which you would like to extract to Drive
const fileTypesToExtract = ['pdf'];
// Name of the parent folder in Google Drive in which files will be put
const parentFolderID = '1k6pAq98m1D4rIILkGnzpa1tk_O0uaXqQ';
// Name of the label which will be applied after processing the mail message
// It will be automatically created if it doesn't exist.
const labelName = '@indrive';
// Sub-folders names based on message month index
const monthNames = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const after = "01/01/2022"
const hasAllLabels = ['firma-ksiegowosc']
const recipientEmail = "[email protected]"
function DumpFilesToDrive() {
let query = `NOT label:@indrive AND after:${after} AND to:${recipientEmail}`;
query += hasAllLabels.map((label) => ` AND label:${label}`)
Logger.log(query)
const threads = GmailApp.search(query);
const label = getGmailLabel_(labelName);
threads.forEach((thread) => {
const msgs = thread.getMessages()
msgs.forEach((msg) => {
const date = msg.getDate()
const attachments = msg.getAttachments();
const subFolderName = monthNames[date.getMonth()];
const subFolder = createFolder_(parentFolderID, subFolderName)
attachments.forEach((attachment) => {
const saveFileName = normalizeNameIfNeeded_(attachment.getName(), date)
Logger.log('Saving file: %s to %s', saveFileName, subFolderName)
if (!hasWantedAttachemed_(attachment)) return;
const att = attachment.copyBlob()
att.setName(saveFileName)
subFolder.createFile(att);
})
});
thread.addLabel(label);
})
}
function normalizeNameIfNeeded_(fileName: string, date: GDate): string {
switch (true) {
case fileName.includes('Orlen'):
return normalizeFuelInvoiceName_(fileName, date)
case fileName.includes('FIR'):
return normalizeInternetInvoiceName_()
default:
return fileName
}
}
// It doesn't apply if file name doesn't contain the `Orlen` name.
// Converts:
// From: Faktura F 63K20/0509/22 Orlen Pay
// To: paliwo20220107.pdf
function normalizeFuelInvoiceName_(fileName: string, date: GDate): string {
const simpleDate = date.getFullYear() * 10000 +
(date.getMonth() + 1) * 100 + date.getDate();
return `paliwo${simpleDate}.pdf`
}
function normalizeInternetInvoiceName_(): string {
return "internet-swiatlowodowy.pdf"
}
function createFolder_(folderID: string, folderName: string): Folder {
const parentFolder = DriveApp.getFolderById(folderID);
const subFolders = parentFolder.getFolders();
while (subFolders.hasNext()) {
const folder = subFolders.next();
if (folder.getName() === folderName) {
return folder;
}
}
return parentFolder.createFolder(folderName);
}
function getGmailLabel_(name): GmailLabel {
let label = GmailApp.getUserLabelByName(name);
if (!label) {
label = GmailApp.createLabel(name);
}
return label;
}
function hasWantedAttachemed_(attachment): boolean {
const fileName = attachment.getName();
const extension = fileName.split(".").pop();
return fileTypesToExtract.indexOf(extension) !== -1;
}