-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtract_Emails.txt
100 lines (77 loc) · 3.42 KB
/
Extract_Emails.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Main function to extract emails with a specific label and process them
function Extract_Emails() {
var labelUnprocessed = 'ADD YOUR LABEL HERE'; // Gmail label to search for
var labelProcessed = 'ADD YOUR LABEL HERE'; // Label to apply after processing
var destinationFolder = 'ADD YOUR FOLDER ID HERE'; // Google Drive Folder ID where the sheet should be saved
const scriptProperties = PropertiesService.getScriptProperties();
const lockKey = 'scriptRunning';
// Check if script is already running
if (scriptProperties.getProperty(lockKey) === 'true') {
console.log('Script is already running. Exiting.');
return; // Exit if already running
}
// Set the lock to prevent re-entry
scriptProperties.setProperty(lockKey, 'true');
console.log('Script started.');
// Get the Gmail label
var label = GmailApp.getUserLabelByName(labelUnprocessed);
if (!label) {
Logger.log('Label not found.');
return;
}
// Get the new label
var newLabel = GmailApp.getUserLabelByName(labelProcessed);
if (!newLabel) {
newLabel = GmailApp.createLabel(labelProcessed);
}
// Get all threads with the label
var threads = label.getThreads();
// If no threads, return
if (threads.length === 0) {
Logger.log('No emails found with label: ' + labelUnprocessed);
scriptProperties.deleteProperty(lockKey);
console.log('Script finished.');
Update_Sheet();
return;
}
// Loop through each thread
threads.forEach(function(thread) {
var messages = thread.getMessages();
// Process each message in the thread
messages.forEach(function(message) {
// Create a new Google Sheet for each email
var sheet = SpreadsheetApp.create('Processed Email - ' + message.getSubject() + ' - ' + new Date().toLocaleString()).getActiveSheet();
// Add headers to the sheet
sheet.appendRow(['Subject', 'From', 'Date', 'Body Line']);
var emailData = {
subject: message.getSubject(),
from: message.getFrom(),
date: message.getDate(),
body: message.getPlainBody()
};
// Append the basic email data (subject, from, date) to the sheet
sheet.appendRow([emailData.subject, emailData.from, emailData.date, '']);
// Clean the body and process it
var cleanBody = emailData.body.replace(/([^\n])\n([^\n])/g, '$1 $2'); // Rejoin lines that Gmail broke mid-paragraph
var paragraphs = cleanBody.split(/\n{2,}/); // Split by paragraph breaks (two or more newlines)
paragraphs.forEach(function(paragraph) {
var lines = paragraph.split(/\n/); // Split into individual lines
lines.forEach(function(line) {
sheet.appendRow(['', '', '', line.trim()]); // Insert each line into its own row
});
// Insert an empty row to indicate paragraph break
sheet.appendRow(['', '', '', '']);
// Move the newly created sheet to the specified Google Drive folder
var newSheetId = sheet.getParent().getId();
moveSheetToFolder(newSheetId, destinationFolder);
// Remove the label from the message and apply the new label
thread.removeLabel(label);
thread.addLabel(newLabel);
});
});
});
Logger.log('Emails processed and added to separate sheets.');
Update_Sheet();
scriptProperties.deleteProperty(lockKey);
console.log('Script finished.');
}