google-apps-scriptgoogle-sheetsgoogle-contacts-api

Why ContactsApp.getContacts ByEmailAdress('someemail@email.com') returns [Contact] using Google Apps Script?


Although everything now runs, I only see logged [Contact], as it's returning an array. No documentation of how to get the email besides ContactsApp.getContacts ByEmailAdress('someemail@email.com'), though.

Thanks for any clarification.

Antonio


Solution

  • When the official document of getContactsByEmailAddress(query) is seen, the returned value is Contact[]. Unfortunately, although I cannot see your actual script for retrieving [Contact], I guessed that you might see the returned value with Logger.log. If my understanding is correct, Logger.log(ContactsApp.getContactsByEmailAddress("### email address ###")) show [Contact] in the log.

    I thought that this might be the answer for Why ContactsApp.getContacts ByEmailAdress('someemail@email.com') returns [Contact] using Google Apps Script?.

    If you want to retrieve the other data from the contact, how about the following sample script?

    Sample script:

    const res = ContactsApp.getContactsByEmailAddress("### email address ###");
    // Logger.log(res) // In this case, `[Contact]` and `[Contact, Contact,,,]` can be seen.
    const name = res.map(e => e.getFullName());
    Logger.log(name) // In this case, as a sample, the full name is retrieved from the retrieved contact object.
    

    Reference: