-1

I need a program for google script that tells me if I have an email address in my contacts or not.

I get many emails from vendors each day and many of them are completely new contacts. I need a program that makes a list of only the new contacts so I can decide which contact group to add them too.

Basic structure of what I'm looking for:

Will run this program every 5 minutes (I can set that up.)

-Checks emails with yellow or blue star for email address of recipient and sender. filters out my email address.

-Checks the email address against all my contacts. If the email address does not appear in my google contacts I need:

Email address, subject line, message text, additional thread message, additional thread message... in seperate columns in a spreadsheet.

I will then go through this information manually to decide which groups to put them in.

Thank you for your help!!!

UPDATE//////////////////////////////update.3

Here's where I'm at. This is working on and off. One time I run it it works, another time it doesn't. REally annoying. If anyone can see any glairing problems let me know. Especially hard to get the if sndr and if rcpnt to return false to run the rest of the program. I've tried about 20 ways!!!

//http://webapps.stackexchange.com/questions/9813/get-e-mail-addresses-from-gmail-messages-received
/////////////NESTED LOOP METHOD





function newEmailAddressList(){

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("NewEmails");
 var range = sheet.getRange("A2:BA");
 var addrA;

 range.clearContent(); //May need to change this to my delete function to speed things up.

 var contact = ContactsApp.getContacts();

  for(var i = 0;i < contact.length;i++){ 

    var addrA = [];
    var start;
    var addresses = contact[i].getEmails();
    for(var j = 0;j < addresses.length;j++){



      addrA.push(addresses[j].getAddress()); 

    }; 

};
 sheet.getRange('H1').setValue("List Created");



 for (var i=0; i<50; i++){
 var threads = GmailApp.getInboxThreads(0,50)[i];
 var messages = threads.getMessages()[0];

      var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
      var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");


     function contains(addrA, sndr) {
    for (var i = 0; i < addrA.length; i++) {
        if (addrA[i] === sndr) {
            sheet.appendRow("Emails Match");
        }else{



      var dat = messages.getDate();
      //var sndr = messages.getFrom();
      //var rcpnt = messages.getTo();
      var sub = messages.getSubject();
      var msg = messages.getPlainBody();


      var info = [dat,sndr,rcpnt,sub,msg];

 sheet.appendRow(info); //appendRow only works with sheet class not range class

};

};

};

};

};
Clayten
  • 53
  • 1
  • 9
  • The first code I gave you works and it does display emails, from address and contact addresses. They scroll down on an otherwise white background of a modeless dialog. But you have to be looking at the spreadsheet when you run them. That's why I always put them into the My Tools menu. So that I can see what's happening. I prefer it over the Logger.log() display. – Cooper Mar 03 '17 at 05:22
  • The arrays are just javascript arrays. There are plenty of Javascript tutorials out there. [Here's](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) a place I go to a lot. Dealing with range.getValue and range.setValue has some particular problems associated with it and your first few times playing with them will be painful at best. You have to be sure that the arrays and the ranges are the same size and to make matters even worse the ranges start from 1 and arrays start from 0. – Cooper Mar 03 '17 at 05:32

1 Answers1

0

Well, I'll give you a start. And keep in my that I've never really worked with GmailApp in the past so all of this information is pretty much readily available to any who opens the code editor clicks on help and then clicks on API Reference. Everything you need to know about Google Scripts is right there organized about as well as it's ever been. They have really improved their documentation since the first time I've looked at it several years ago.

So I'm giving you some functions that will get all of your contact emails, your from emails from your inbox and and your emails.

function myContacts()
{
  var s = '';
  var br = '<br />';
  var contact = ContactsApp.getContacts();
  for(var i = 0;i < contact.length;i++)
  {
    var addrA = [];
    var addresses = contact[i].getEmails();
    for(var j = 0;j < addresses.length;j++)
    {
      s += addresses[j].getAddress() + br;
      addrA.push(addresses[j].getAddress());
    }
  }
  dispStatus('Contact Emails',s, 800, 400);
}

function MyFroms()
{  
  var threads = GmailApp.getInboxThreads();
  var s = '';
  for(var i = 0; i < threads.length; i++) 
  {
    var msg = threads[i].getMessages();
    for(var j = 0; j < msg.length;j++)
    {
      s += msg[j].getFrom() + '<br />';
    }
  }
  dispStatus('My Messages', s , 800 , 400);  
}

function MyMessages()
{  
  var threads = GmailApp.getInboxThreads();
  var s = '';
  for(var i = 0; i < threads.length; i++) 
  {
    var msg = threads[i].getMessages();
    for(var j = 0; j < msg.length;j++)
    {
      s += 'Message' + j+1 + '<br />';
      s += msg[j].getFrom() + '<br />';
      s += msg[j].getBody() + '<br />';

    }

  }
  dispStatus('My Messages', s , 800 , 400);  
}

function dispStatus(title,html,width,height)
{
  var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
  var width = typeof(width) !== 'undefined' ? width : 800;
  var height = typeof(height) !== 'undefined' ? height : 400;
  var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
  var htmlOutput = HtmlService
     .createHtmlOutput(html)
     .setWidth(width)
     .setHeight(height);
 SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
} 

It's not a complete answer. But hopefully it will encourage you to jump in get your feet wet and exercise your mental muscles and bring us back at least a partially working skeleton of a program that we can help you get running.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • This first code I have no clue how to write it. I get no errors, it just runs and does nothing. – Clayten Mar 03 '17 at 04:52
  • Are you running it in the code editor associated with the spreadsheet? – Cooper Mar 03 '17 at 05:01
  • Yes, I had to edit it to work in my spreadsheet because I will have to manually work with the results when finished. I have troubles with Arrays so I never know what is going on in the background if I don't print it out in sheets. Just did these updates above. – Clayten Mar 03 '17 at 05:04
  • When you ran the code initially were you looking at the spread and running the code from the created My Tools menu. If your running the code in the editor it will run and display the modeless dialog but you won't see it because it comes behind the editor. All you see is the spreadsheet tab icon blinking to tell you that something is happening on the spreadsheet. – Cooper Mar 03 '17 at 05:08
  • I ran each of the 4 functions from the script editor. – Clayten Mar 03 '17 at 05:23
  • Only the first function gave me any information. That part worked well so I adapted it to work with the spreadsheet. – Clayten Mar 03 '17 at 05:24
  • I've updated the code to what is currently partially working. – Clayten Mar 03 '17 at 05:29