-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLeanUpAppscript.txt
86 lines (74 loc) · 3.16 KB
/
CLeanUpAppscript.txt
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
function logAndFlagEmails() {
// Define the search criteria
const daysAgo = 10; // Customize this value as needed
const pastDate = new Date();
pastDate.setDate(pastDate.getDate() - daysAgo);
const formattedDate = Utilities.formatDate(pastDate, Session.getScriptTimeZone(), "yyyy/MM/dd");
const searchQueries = [
`(from:noreply@ OR from:*@test.com OR from:[email protected]) before:${formattedDate}`,
`from:[email protected] subject:TESTEMAIL before:${formattedDate}`
];
// Spreadsheet setup
const spreadsheetId = 'YOUR_SPREADSHEET_ID'; // Replace with your actual Spreadsheet ID
const logSheetName = 'Sheet1'; // Logging sheet
const summarySheetName = 'Sheet2'; // Summary sheet
const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
const logSheet = spreadsheet.getSheetByName(logSheetName);
const summarySheet = spreadsheet.getSheetByName(summarySheetName);
if (!logSheet || !summarySheet) {
console.log('One or more specified sheets not found.');
return;
}
// Check for the "DELETE-ME" label, create it if it doesn't exist
// let label = GmailApp.getUserLabels().find(l => l.getName() === "DELETE-ME");
// if (!label) {
// label = GmailApp.createLabel("DELETE-ME");
// }
...
// Process each search query
searchQueries.forEach((query) => {
const threads = GmailApp.search(query);
threads.forEach((thread) => {
const messages = GmailApp.getMessagesForThread(thread);
messages.forEach((message) => {
if (message.getDate() < pastDate) { // Additional check
// Log the email details
console.log(`Flagged for deletion: ${message.getSubject()} from ${message.getFrom()}`);
emailLogs.push([new Date(), message.getSubject(), message.getFrom(), 'Flagged for Deletion']);
totalFlagged += 1;
// Add "DELETE-ME" label to the thread
// label.addToThread(thread); // This line is now commented out
}
});
});
});
// Initialize the counter and log array
let totalFlagged = 0;
let emailLogs = [];
// Process each search query
searchQueries.forEach((query) => {
const threads = GmailApp.search(query);
threads.forEach((thread) => {
const messages = GmailApp.getMessagesForThread(thread);
messages.forEach((message) => {
if (message.getDate() < pastDate) { // Additional check
// Log the email details
console.log(`Flagged for deletion: ${message.getSubject()} from ${message.getFrom()}`);
emailLogs.push([new Date(), message.getSubject(), message.getFrom(), 'Flagged for Deletion']);
totalFlagged += 1;
// Add "DELETE-ME" label to the thread
label.addToThread(thread);
}
});
});
});
// Log to Spreadsheet
if (emailLogs.length > 0) {
// Append the log array to the log sheet
emailLogs.forEach((log) => logSheet.appendRow(log));
// Update the summary sheet
summarySheet.appendRow([new Date(), 'Total Flagged for Deletion', totalFlagged]);
}
// Log the total count
console.log(`Total emails flagged for deletion: ${totalFlagged}`);
}