📅 September 2, 2024
💻 Tutorial
⭐ Beginner Friendly
V1: Data Parsing From Incoming Emails
Ashish Dwivedi
Editorial Team • Tech Writer
About This Tutorial
Automatically extract and organize important data from incoming emails with powerful data parsing. Whether it’s order details, contact information, or attachments, streamline your workflow by converting unstructured email content into structured data you can use in spreadsheets, CRMs, or custom applications—no manual copy-pasting required.
Step 1: Click on the Copy button to copy the code snippet.
Step 2: Paste the copied code into your project’s script editor.
function fetchEmailData() {
var labelName = "Fetch Gmail data"; // replace with the name of your label
var batchSize = 500; // adjust the batch size as needed
var sheet = SpreadsheetApp.getActiveSheet();
var data = [];
// Header row
data.push([
"From Name", "From Email", "Reply-To Name", "Reply-To Email", "To", "Subject", "Date",
"Body Name", "Body Email", "Body Office Phone", "Body Address", "Body Message"
]);
var start = 0;
var threads = GmailApp.search("label:" + labelName, start, batchSize); // get the first batch of threads
while (threads.length > 0) {
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
// Extract FROM details
var from = message.getFrom();
var fromName = "";
var fromEmail = "";
if (from) {
var parts = from.split("<");
if (parts.length > 1) {
fromName = parts[0].trim();
fromEmail = parts[1].replace(">", "").trim();
} else {
fromEmail = from.trim();
}
}
// Extract REPLY-TO details
var replyTo = message.getReplyTo();
var replyToName = "";
var replyToEmail = "";
if (replyTo) {
var replyParts = replyTo.split("<");
if (replyParts.length > 1) {
replyToName = replyParts[0].trim();
replyToEmail = replyParts[1].replace(">", "").trim();
} else {
replyToEmail = replyTo.trim();
}
}
var to = message.getTo();
var subject = message.getSubject();
var date = message.getDate();
// Clean body text
var body = message.getBody()
.replace(/<\/?[^>]+(>|$)/g, "") // remove HTML tags
.replace(/<\/div>/g, "\n"); // replace div tags with newlines
// Extract structured fields from body
var bodyName = "";
var bodyEmail = "";
var bodyOfficePhone = "";
var bodyAddress = "";
var bodyMessage = "";
var bodyMatch = body.match(
/Name:\s*([\w\s]+).*Email:\s*([^\s]+).*Office\s*Phone:\s*(.*)\s*Address:\s*(.*)\s*Message:\s*([\s\S]*)/is
);
if (bodyMatch) {
bodyName = bodyMatch[1].trim();
bodyEmail = bodyMatch[2].trim();
bodyOfficePhone = bodyMatch[3].trim();
bodyAddress = bodyMatch[4].trim();
bodyMessage = bodyMatch[5].trim();
}
data.push([
fromName, fromEmail, replyToName, replyToEmail, to, subject, date,
bodyName, bodyEmail, bodyOfficePhone, bodyAddress, bodyMessage
]);
}
}
start += batchSize;
threads = GmailApp.search("label:" + labelName, start, batchSize); // get next batch
}
// Write to sheet
var numRows = data.length;
var numCols = data[0].length;
sheet.getRange(1, 1, numRows, numCols).setValues(data);
sheet.getRange("B2:B").setNumberFormat("@"); // format email column as plain text
}