google-apps-scriptgoogle-contacts-api

How to get email address from full contact name


Is there a way to get a email address from user full name. I have a list of more than 100 names and I don't want to enter it manually. I wanted to make a quick script on a google sheet that retrieve the email address of a list of names.

Any ideas? I can't find a function that get email address from a name.


Solution

  • Nvm I found the information:

    https://developers.google.com/apps-script/reference/contacts/contacts-app#getContactsByName(String)

    I overlooked it

    BTW, ContactApp class is extremely slow, so I recommend to fetch all contacts (around 2500 can takes up to a minute). See the documentation: https://issuetracker.google.com/issues/36754694


    function contactsToCustomObj(lc){
       //Create custom obj
       var pl= {name:[],email:[]}, len = lc.length,i=0,h;
        while(i < len){
         h = lc[i].getEmails()[0];
         //Check if there is an email linked to this contact
         if(h)
           pl.email[i] = h.getAddress();
         else
           pl.email[i] = "*No email linked*";
    
         pl.name[i] = lc[0].getFullName();
         //Check if there is a name  
         //If not, in my case, all emails are first_name.last_name@hotmail.com
         if(!pl.name[i]){
           var t = pl.email[i].split("@")[0].split(".");
           pl.name[i] = t[0] + " " +t[1];
         }
         //RemoveSpecialCase is my custom regex class
         pl.name[i] = pl.name[i].trim().removeSpecialCase().toUpperCase();
         i++;
      }
      return pl;
    }