Google script to extract email information

This code helps to get summary of email such as subject, received date etc. Code is for the unread emails; remove lines with if condition check to read all emails.
Make a new google sheet> tools>script editor and copy and paste it. You will require to authorize the use of code while executing it.

function retriveUnreadMessages() {
// imports subject, from (email), received date of all unread email in inbox of gmail account
// the data is then written in the google spreadsheet
//reference:https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#appendRow(Object)
//

//-----------google sheet part
var ss=SpreadsheetApp.getActiveSheet();
var date1=new Date().toISOString().slice(0, 10);
ss.appendRow(["***** new execution = ",date1,"*****"]);// append a new information line where the new execution is done because data is appended at the end

//-----------------------------------------gmail part-----
// Log the subject lines of your Inbox
var threads = GmailApp.getInboxThreads();
// number of message to retrive
var n= threads.length;
for (var i = 0; i < n; i++) {

// check if there is unread messages in a thread
if(threads[i].isUnread)
{

var subject1=threads[i].getFirstMessageSubject(); // subjet of thread
var email1=threads[i].getMessages(); // messages inside the thread
// check if the individual message is unread
for (var j = 0; j < email1.length; j++) {
if(email1[j].isUnread)
{
var message_sender=email1[j].getFrom();
var message_sendDateTime=email1[j].getDate();
}
ss.appendRow([i,j,subject1,message_sender,message_sendDateTime]); //write data to spreadsheet.. it is appended to new row

}
}
}
}

Advertisement
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: