Add e.mails to contacts from google doc

You can add emails to contacts from a Google Doc using a combination of Google Apps Script and the Contacts API. Here's a step-by-step guide to help you achieve this:

Prerequisites:

  1. You need to have a Google Doc with a list of names and emails.
  2. You need to have the Google Apps Script editor enabled in your Google Doc.
  3. You need to have the Contacts API enabled in your Google Cloud Console project.

Step 1: Enable the Contacts API

  1. Go to the Google Cloud Console and navigate to the API Library page.
  2. Search for "Contacts API" and click on the result.
  3. Click on the "Enable" button.
  4. Create a new project or select an existing one.
  5. Click on the "Create credentials" button and select "OAuth client ID".
  6. Choose "Web application" and enter a authorized JavaScript origins (e.g., https://script.google.com).
  7. Click on the "Create" button and copy the client ID and client secret.

Step 2: Set up the Google Apps Script

  1. Open your Google Doc and click on the "Tools" menu.
  2. Select "Script editor".
  3. Delete any existing code in the editor and paste the following code:
    
    function addEmailsToContacts() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getActiveSheet();
    var contacts = ContactsApp.getContacts();
    var emails = [];

// Loop through the rows in the sheet sheet.getRange(2, 1, sheet.getLastRow() - 1, 2).getValues().forEach(function(row) { var name = row[0]; var email = row[1];

// Check if the email is not empty
if (email!== "") {
  // Add the email to the contacts list
  contacts.addContact(name, email);
  emails.push(email);
}

});

// Log the emails that were added Logger.log("Added the following emails to contacts: %s", emails.join(", ")); }


4. Save the script by clicking on the floppy disk icon or pressing `Ctrl+S` (or `Cmd+S` on a Mac).

**Step 3: Run the script**

1. Go back to your Google Doc and click on the "Run" button (or press `F5`) to execute the script.
2. The script will loop through the rows in the sheet, extract the names and emails, and add them to the contacts list.

**Tips and Variations:**

* You can modify the script to add additional fields to the contacts, such as phone numbers or addresses.
* You can use the `ContactsApp.getContactList()` method to retrieve a list of existing contacts and then update their information.
* You can use the `ContactsApp.deleteContact()` method to delete contacts that are no longer needed.
* You can schedule the script to run automatically using the Google Apps Script trigger feature.