Extract data from Email Body - javascript

So i'm writing a script to automatically collect emails to a Google spreadsheet. I would like from this email to extract some details like the email address, the name, the address, the phone number. So far i managed to extract the date, the subject, the sender details and the body contents. Below i'll attach the email body and also the code i've written so far! If you have contact me directly at this emails.
This a code i've tried so far:
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu("KingBoss Menu").addItem("Get Emails", "getGmailEmails").addToUi();
}
function getGmailEmails() {
var label = GmailApp.getUserLabelByName("warranty");
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--) {
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++) {
var message = messages[j];
extractDetails(message);
GmailApp.markMessageRead(message);
}
}
}
function extractDetails(message) {
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var bodyContents = message.getPlainBody();
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
}

Related

Receiving [Ljava.lang.Object in Apps Script (bringing attachment name from Gmail to Sheets)

Have been working on an Apps Script to bring Gmail message information (sender, subject, link, attachment) into a Google Sheet, but not able to bring the title of the attachment over successfully (attachment name field in Google Sheet reads like [Ljava.lang.Object;#127afe9; it appears to be related to the attachments array, but I cannot figure it out. Have tried the solutions offered in What does "[Ljava.lang.Object;#" mean?, but no luck.
function emailToSheet() {
var label = GmailApp.getUserLabelByName('New TODO'); // Find messages with label
var changeLabel = GmailApp.getUserLabelByName('TODO'); // Change message label to
var ss = SpreadsheetApp.openById('xxxx'); // Append to spreadsheet
var sh = ss.getSheetByName("Email"); //On this sheet
var threads = label.getThreads();
for (var i=0; i<threads.length; i++){
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++) {
var sent = messages[j].getDate();
var from = messages[j].getFrom();
var subject = messages[j].getSubject();
// Get attachment name
var attachments = messages[j].getAttachments();
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
var attachmentName = attachments[k].getName();
}
var messageId = messages[j].getId();
var messageUrl = "https://mail.google.com/mail/u/0/#inbox/" + messageId;
ss.appendRow([sent, from, subject, attachmentName, messageUrl])
}
threads[i].removeLabel(label);
threads[i].addLabel(changeLabel);
}
}
I'm thinking that you want this:
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
var attachmentName = attachments[k].getName();
}
to be this:
var attachmentName = [];
for (var k=0; k<attachments.length; k++) {
attachmentName.push(attachments[k].getName());
}
and maybe this:
ss.appendRow([sent, from, subject, attachmentName, messageUrl])
to be this:
ss.appendRow([sent, from, subject, attachmentName.join(" "), messageUrl])

Google Sheet Import Ljava.lang.Object error

I have pieced together a few different scripts to get this to work but I cant seem to get the code to put the data in correctly.
Email Input:
*Status:*
*Date:* 03/31/2020
*WorkOrder:* 123456-1
*DMSShipDate:* 03/31/2020
*PONumber:* 8675309
*Company:* Test
Script
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Import Email").addItem("Import Email", "getGmailEmails").addToUi();
}
function getGmailEmails(){
if (Session.getActiveUser().getEmail() != "email#gmail.com"){
Browser.msgBox("Please log in as email#gmail.com");
return;
}
var label = GmailApp.getUserLabelByName("ImportMe");
var threads = label.getThreads();
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++){
var message = messages[j];
extractDetails(message);
GmailApp.markMessageRead(message);
}
threads[i].removeLabel(label);
}
}
function extractDetails(message){
var bodyContents = message.getPlainBody();
var status = bodyContents.match(/\*Status:\*(.*) /);
var dateEntered = bodyContents.match(/\*Date:\*(.*) /);
var workOrder = bodyContents.match(/\*WorkOrder:\*(.*) /);
var dmsShipDate = bodyContents.match(/\*DMSShipDate:\*(.*) /);
var poNum = bodyContents.match(/\*PONumber:\*(.*) /);
var company = bodyContents.match(/\*Company:\*(.*) /);
var activeSheet = SpreadsheetApp.getActiveSheet();
activeSheet.appendRow([status, dateEntered, workOrder, dmsShipDate, poNum, company]);
}
Result:
I get the following in each column:
[Ljava.lang.Object;#488e1851, [Ljava.lang.Object;#5c588720, etc, etc
I have tried JSON.Stringify and toString() but nothing seems to be able to get the data into the column correctly.
String.match returns a array. To get the captured group, index into that array:
bodyContents.match(/\*Status:\*(.*) /)[1];

How do i retrieve their timestamp from an email

Writing a small script that pulls messages with a label and outputs the subject to a Slack channel.
I've gotten it down ( customized to from someone ) but need further help on where to get the time from.
/* Credit: gist.github.com/andrewmwilson */
function sendEmailsToSlack() {
var label = GmailApp.getUserLabelByName('!urgent');
var messages = [];
var threads = label.getThreads();
for (var i = 0; i < threads.length; i++) {
messages = messages.concat(threads[i].getMessages())
}
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
Logger.log(message);
var output = '*State Change Detected*';
output += '\n*subject:* ' + message.getSubject();
Logger.log(output);
You can use the getDate() method on a message:
var message = messages[0]; // Get first message
Logger.log(message.getDate()); // Log date and time of the message

Automatically send emails from addresses listed on one sheet when their name is selected from a drop down menu on another sheet

Hi I have a phone enquiry log that has two sheets - I want to automatically send emails to email addresses stored in a second sheet when their name is selected from a drop down box on another sheet.
I would also like to fill column H with a notifacation 'mail sent'but have limited skills as you all can probably see.
This is the code I have so far:
function emailMessage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Enquiry Log");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+lastRow)+":J"+(lastRow)).getValues(); //get the range and values in one step
var headers = responses.getRange("A1:J1").getValues();
var recipient = responses.getRange("E"+(lastRow)).getValues();
var emailSheet = ss.getSheetByName("Emails");
var names = emailSheet.getRange("A2:A12").getValues();
var emails = emailSheet.getRange("B2:B12").getValues();
var subject = "New phone message";
var message = composeMessage(headers,values);
var email = '';
for (var row=0; row < names.length; row++) {
if (names[row][0] == recipient) {
email = emails[row][0];
break;
MailApp.sendEmail(recipient,subject,message);
}
}
function composeMessage(headers,values) {
var message = 'Please check the Enquiry Log as you have a new message:'
for(var c=0;c<values[0].length;++c){
message+='\n'+headers[0][c]+' : '+values[0][c]
}
return(message);
}
};

How can i get all objects in a column Google Spreadsheet

I have the column "D" in my spreadsheet. I would like to get all objects in the column and send an email to them. My script gets the firts user in the column and then loops it, it dosen't continue.
This i what i have:
function email(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var email = dataSheet.getRange(2, 5);
var email2 = email.getValue();
for (var i = 0; i < email2.length; ++i) {
// Get a row object
var rowData = email2[i];
MailApp.sendEmail(rowData emailSubject, emailText);
Logger.log(email2);
}
}
you selected only one cell, so you are making a loop in a single element...
try this way :
function email(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var emails = dataSheet.getRange('D2:D').getValues();
for (var i = 0; i < emails.length; ++i) {
// Get a row object
var rowData = emails[i][0];
if(rowData!=''){
MailApp.sendEmail(rowData emailSubject, emailText);// I assume emailSubject and emailText are defined in your real code...
}
Logger.log(emails[i]);
}
}

Categories

Resources