-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.ts
More file actions
53 lines (47 loc) · 1.73 KB
/
email.ts
File metadata and controls
53 lines (47 loc) · 1.73 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
import GmailLabel = GoogleAppsScript.Gmail.GmailLabel
import GmailMessage = GoogleAppsScript.Gmail.GmailMessage;
import GmailAttachment = GoogleAppsScript.Gmail.GmailAttachment;
const INVOICES_FOLDER = 'Invoices'
// This currently only works if the label is the same as the invoices folder.
const INVOICES_LABEL_PREFIX = INVOICES_FOLDER + '/'
const ATTACHMENT_OPTIONS = {
includeInlineImages: false,
includeAttachments: true
}
function isInvoiceLabel(label: GmailLabel): boolean {
return label.getName().indexOf(INVOICES_LABEL_PREFIX) === 0
}
function getInvoiceLabels(): GmailLabel[] {
const labels = GmailApp.getUserLabels()
const invoiceLabels = labels.filter(isInvoiceLabel)
invoiceLabels.forEach((label): void => {
// @ts-ignore
console.log('Invoice label: %s', label.getName())
})
return invoiceLabels
}
function getAttachmentsForLabel(label: string, start: string, end: string): GmailAttachment[] {
const messages = getMessagesForLabel(label, start, end);
const attachments = messages.reduce(
(attachments, message): GmailAttachment[] =>
attachments.concat(message.getAttachments(ATTACHMENT_OPTIONS)),
[]
)
attachments.forEach((attachment): void => {
// @ts-ignore
console.log('Attachment: %s', attachment.getName())
})
return attachments;
}
function getMessagesForLabel(label: string, start: string, end: string): GmailMessage[] {
const query = 'label:"' + label + '" after:' + start + ' -after:' + end + '';
// @ts-ignore
console.log('Searching for: %s', query);
const threads = GmailApp.search(query);
// @ts-ignore
console.log('Found %s results.', threads.length);
return threads.reduce(
(messages, thread): GmailMessage[] => messages.concat(thread.getMessages()),
[]
)
}