-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
132 lines (107 loc) · 3.83 KB
/
Code.gs
File metadata and controls
132 lines (107 loc) · 3.83 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
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Email Tools')
.addItem('Start Extraction', 'showSidebar')
.addItem('Stop Script', 'stopScript')
.addToUi();
}
function showSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Email Extractor');
SpreadsheetApp.getUi().showSidebar(html);
}
function extractEmailsToSheet(startDate, endDate, sortOrder, dateTimeFormat, scope, label, whitelist, blacklist) {
const start = new Date(startDate);
const end = new Date(endDate);
let baseQuery = `after:${formatDateForQuery(start)} before:${formatDateForQuery(end)}`;
if (scope === "inbox") {
baseQuery += " in:inbox";
} else if (scope === "anywhere") {
baseQuery += " in:anywhere -in:spam -in:trash";
} else if (scope === "custom" && label) {
baseQuery += ` label:${label}`;
}
const whitelistArray = whitelist ? whitelist.split(',').map(e => e.trim().toLowerCase()) : [];
const blacklistArray = blacklist ? blacklist.split(',').map(e => e.trim().toLowerCase()) : [];
const threads = GmailApp.search(baseQuery);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.clear();
// Status bar setup
const labelCell = sheet.getRange("A1");
labelCell.setValue("Status:")
.setFontWeight("bold")
.setBackground("#e0e0e0")
.setFontColor("black");
const statusCell = sheet.getRange("B1");
statusCell.setValue("RUNNING")
.setFontWeight("bold")
.setFontColor("white")
.setBackground("#4285F4");
sheet.appendRow(["From", "Date", "Subject"]);
const headerRange = sheet.getRange(sheet.getLastRow(), 1, 1, 3);
headerRange
.setFontWeight("bold")
.setHorizontalAlignment("center")
.setBackground("#f1f3f4");
const emailData = [];
for (const thread of threads) {
if (sheet.getRange("B1").getValue() === "STOP") {
Logger.log("Process stopped by user.");
sheet.appendRow(["--- Script stopped manually ---", "", ""]);
return;
}
const messages = thread.getMessages().filter(msg => {
const msgDate = msg.getDate();
return msgDate >= start && msgDate <= end;
});
for (const message of messages) {
if (sheet.getRange("B1").getValue() === "STOP") {
Logger.log("Process stopped by user.");
sheet.appendRow(["--- Script stopped manually ---", "", ""]);
return;
}
const fromEmail = extractEmail(message.getFrom()).toLowerCase();
const subject = message.getSubject();
const formattedDate = formatDateForDisplay(message.getDate(), dateTimeFormat);
if (whitelistArray.length > 0 && !whitelistArray.includes(fromEmail)) continue;
if (blacklistArray.includes(fromEmail)) continue;
emailData.push([fromEmail, formattedDate, subject]);
}}
emailData.sort((a, b) => {
const dateA = new Date(a[1]);
const dateB = new Date(b[1]);
return sortOrder === 'asc' ? dateA - dateB : dateB - dateA;
});
for (const row of emailData) {
sheet.appendRow(row);
}
// Mark status as DONE
sheet.getRange("B1")
.setValue("DONE")
.setFontWeight("bold")
.setFontColor("white")
.setBackground("#34A853");
}
function extractEmail(fromString) {
const match = fromString.match(/<([^>]+)>/); // Match content inside angle brackets
return match ? match[1] : fromString;
}
function formatDateForDisplay(date, format) {
if (!format || format === '' || format.length == 0) {
format = 'ddd, YYYY-MM-DD HH:mm A'
}
var now = dayjs(date);
Logger.log(now.format(format));
return now.format(format)
}
function stopScript() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("B1")
.setValue("STOP")
.setFontWeight("bold")
.setFontColor("white")
.setBackground("#EA4335"); // Google red
}
function formatDateForQuery(date) {
return Utilities.formatDate(date, Session.getScriptTimeZone(), "yyyy/MM/dd");
}