Script pour récupérer les contact google

63 views
Skip to first unread message

Damien Go

unread,
Mar 11, 2024, 8:46:52 PM3/11/24
to Google Apps Script Community
Bonjour à tous,

avec l'aide de chatGPT j'ai commencer un script qui me permet de récupérer mes contacts directement dans une feuille google sheet sans a avoir a passer par l'export de contact.

voici les champs que je souhaite récupérer 

"Name",
    "Group Membership",
    "E-mail 1 - Value",
    "Phone 1 - Value",
    "Address 1 - Formatted",
    "Address 2 - Formatted",
    "Organization 1 - Name",
    "Organization 1 - Service",
    "Organization 1 - Title",
    "Website 1 - Value"

et voici le script qui fonctionne mais qui ne remonte pas le champs service de la société.

function getGoogleContacts() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Effacer les données précédentes
  sheet.clear();

  // Écrire les en-têtes des colonnes
  sheet.appendRow([
    "Name",
    "Group Membership",
    "E-mail 1 - Value",
    "Phone 1 - Value",
    "Address 1 - Formatted",
    "Address 2 - Formatted",
    "Organization 1 - Name",
    "Organization 1 - Service",
    "Organization 1 - Title",
    "Website 1 - Value"
  ]);

  // Récupérer les contacts Google
  var contacts = ContactsApp.getContacts();

  // Parcourir les contacts et les écrire dans la feuille
  for (var i = 0; i < contacts.length; i++) {
    var contact = contacts[i];

    // Récupérer les informations sur le contact
    var name = contact.getFullName() || "";
    var groupMembership = "";
    var contactGroups = contact.getContactGroups();
    if (contactGroups !== null) {
      groupMembership = contactGroups.map(group => group.getName()).join(", ") || "";
    }
    var emails = contact.getEmails();
    var phones = contact.getPhones();
    var addresses = contact.getAddresses();
    var organizations = contact.getCompanies();
    var websites = contact.getUrls();

    // Déterminer l'organisation
    var orgName = "";
    var orgService = ""; // Déclaré une seule fois

    if (organizations.length > 0) {
      orgName = organizations[0].getCompanyName() || "";

      if (organizations[0].getService && typeof organizations[0].getService === "function") {
        orgService = organizations[0].getService() || "";
      }

      var orgTitle = organizations[0].getJobTitle() || ""; // Déclaré localement
    }

    // Récupérer les valeurs pour chaque champ
    var email1 = emails.length > 0 ? emails[0].getAddress() : "";
    var phone1 = phones.length > 0 ? phones[0].getPhoneNumber() : "";
    var address1 = addresses.length > 0 ? addresses[0].getAddress() : "";
    var address2 = addresses.length > 1 ? addresses[1].getAddress() : "";
    // Utiliser la propriété 'address' pour obtenir l'URL du site Web
    var website1 = websites.length > 0 ? websites[0].getAddress() : "";


    // Écrire les données du contact dans la feuille
    sheet.appendRow([
      name,
      groupMembership,
      email1,
      phone1,
      address1,
      address2,
      orgName,
      orgService,
      orgTitle,
      website1
    ]);
  }
}
Merci pour votre aide

DME

unread,
Mar 14, 2024, 4:50:54 AM3/14/24
to google-apps-sc...@googlegroups.com
The provided script successfully retrieves most of the desired information from your Google Contacts, but it's missing the getService method due to a deprecation in the Contacts API. Here's the improved version to capture the "Organization 1 - Service" field:

function getGoogleContacts() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Clear previous data
  sheet.clear();

  // Write column headers

  sheet.appendRow([
    "Name",
    "Group Membership",
    "Email 1 - Value",

    "Phone 1 - Value",
    "Address 1 - Formatted",
    "Address 2 - Formatted",
    "Organization 1 - Name",
    "Organization 1 - Service",
    "Organization 1 - Title",
    "Website 1 - Value"
  ]);

  // Recover Google contacts
  var contacts = ContactsApp.getContacts();

  // Browse contacts and write them in the sheet

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

    // Retrieve contact information

    var name = contact.getFullName() || "";
    var groupMembership = "";
    var contactGroups = contact.getContactGroups();
    if (contactGroups !== null) {
      groupMembership = contactGroups.map(group => group.getName()).join(", ") || "";
    }
    var emails = contact.getEmails();
    var phones = contact.getPhones();
    var addresses = contact.getAddresses();
    var organizations = contact.getCompanies();
    var websites = contact.getUrls();

    // Determine the organization

    var orgName = "";
    var orgService = "";

    if (organizations.length > 0) {
      orgName = organizations[0].getCompanyName() || "";

      // **Use People API for service field**
      var peopleService = new PeopleAPI.PeopleService();
      var person = peopleService.people().get(organizations[0].getResourceId());
      orgService = person.organizations && person.organizations[0].department || ""; // Access department property


      var orgTitle = organizations[0].getJobTitle() || "";
    }

    // Get the values for each field

    var email1 = emails.length > 0 ? emails[0].getAddress() : "";
    var phone1 = phones.length > 0 ? phones[0].getPhoneNumber() : "";
    var address1 = addresses.length > 0 ? addresses[0].getAddress() : "";
    var address2 = addresses.length > 1 ? addresses[1].getAddress() : "";
    var website1 = websites.length > 0 ? websites[0].getAddress() : "";

    // Write the contact data in the sheet

    sheet.appendRow([
      name,
      groupMembership,
      email1,
      phone1,
      address1,
      address2,
      orgName,
      orgService,
      orgTitle,
      website1
    ]);
  }
}Key Changes:
  1. People API Integration: The ContactsApp service no longer supports getService. We've replaced it with the PeopleAPI.PeopleService to access contact details using the resource ID obtained from the organizations object.
  2. Department Property: Inside the PeopleAPI call, we access the department property within the first organization (person.organizations[0].department) to retrieve the "Organization 1 - Service" information.

Note:

With these changes, your script should successfully retrieve the "Organization 1 - Service" field along with other contact information.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8f142885-4899-4d3b-adad-37c040c97f96n%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages