Update Google Contacts with Google Apps Script

U
Published in: Google Apps Script

This Google Apps Script project will allow your existing contacts to update their Google Contacts entries using a simple online form. The HTML form is published using Apps Script’s HtmlService.

function go() {

    var emailNAME = ScriptProperties.getProperty("NAME");
    var googleGROUP = ContactsApp.getContactGroup(
        ScriptProperties.getProperty("GROUP"));

    if (googleGROUP) {

        var emailSUBJECT = "Your contact information";
        var myContacts = googleGROUP.getContacts();

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

            var email = myContacts[i].getPrimaryEmail();
            var ID = myContacts[i].getId();
            ID = ID.substr(ID.lastIndexOf("/") + 1);

            var emailBody = "Hi,<br /><br />" +
                "Would you please take a moment and update your contact information in my address book. <br /><br />" +
                "Please <a href='" + ScriptApp.getService().getUrl() + "?" +
                Utilities.base64Encode(ID + "#" + email) + "'>click here</a> and fill-in the required details." +
                "Your information will be directly added to my Google Contacts." +
                "<br /><br />Thanks,<br />" + emailNAME;

            var emailTEXT = "Hi,\n\n" +
                "Would you please take a moment and update your contact information in my address book. \n\n" +
                ScriptApp.getService().getUrl() + "?" + Utilities.base64Encode(ID + "#" + email) +
                "\n\nJust click the URL, fill-in the form and your details will be directly added to my Google Contacts." +
                "\n\nThanks,\n" + emailNAME;

            GmailApp.sendEmail(email, emailSUBJECT, emailTEXT, {
                htmlBody: emailBody,
                name: emailNAME
            });
        }
    }
}

function doGet() {
    var html = HtmlService.createTemplateFromFile("form").evaluate();
    html.setTitle("Google Contacts - Digital Inspiration")
    return html;
}

function labnolGetContact(q) {

    var contact = {};
    contact.FOUND = 0;

    try {

        var id = Utilities.base64Decode(q);
        var email = "";

        for (var i = 0; i < id.length; i++) {
            email += String.fromCharCode(id[i]);
        }

        var contactID = email.substr(0, email.indexOf("#"));
        email = email.substr(email.indexOf("#") + 1);

        var c = ContactsApp.getContact(email);

        if (c) {

            var originalID = c.getId();
            originalID = originalID.substr(originalID.lastIndexOf("/") + 1);

            if (contactID != originalID)
                return contact;

            contact.FOUND = 1;

            if (c.getFullName().length)
                contact.FULL_NAME = c.getFullName();

            if (c.getEmails(ContactsApp.Field.HOME_EMAIL).length)
                contact.HOME_EMAIL = c.getEmails(ContactsApp.Field.HOME_EMAIL)[0].getAddress();

            if (c.getAddresses(ContactsApp.Field.HOME_ADDRESS).length) {
                contact.HOME_ADDRESS = c.getAddresses(ContactsApp.Field.HOME_ADDRESS)[0].getAddress();
                contact.HOME_ADDRESS = contact.HOME_ADDRESS.replace(/\n/g, ", ");
            }

            if (c.getPhones(ContactsApp.Field.MOBILE_PHONE).length)
                contact.MOBILE_PHONE = c.getPhones(ContactsApp.Field.MOBILE_PHONE)[0].getPhoneNumber();

            if (c.getIMs(ContactsApp.Field.SKYPE).length)
                contact.SKYPE = c.getIMs(ContactsApp.Field.SKYPE)[0].getAddress();

            if (c.getUrls(ContactsApp.Field.BLOG).length)
                contact.BLOG = c.getUrls(ContactsApp.Field.BLOG)[0].getAddress();

            if (c.getDates(ContactsApp.Field.BIRTHDAY).length) {

                var months = [0, ContactsApp.Month.JANUARY, ContactsApp.Month.FEBRUARY, ContactsApp.Month.MARCH,
                    ContactsApp.Month.APRIL, ContactsApp.Month.MAY, ContactsApp.Month.JUNE,
                    ContactsApp.Month.JULY, ContactsApp.Month.AUGUST, ContactsApp.Month.SEPTEMBER,
                    ContactsApp.Month.OCTOBER, ContactsApp.Month.NOVEMBER, ContactsApp.Month.DECEMBER
                ];

                contact.BIRTHDAY = months.indexOf(c.getDates(ContactsApp.Field.BIRTHDAY)[0].getMonth().toString()) +
                    "/" + c.getDates(ContactsApp.Field.BIRTHDAY)[0].getDay() +
                    "/" + c.getDates(ContactsApp.Field.BIRTHDAY)[0].getYear();
            }
        }

        return contact;

    } catch (e) {

        return contact;

    }

}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube