Reputation: 11
Question: I'm trying to work with Google Contacts using Google Apps Script and the Google People API. Here's what I need help with:
Fetching contacts from Google Contacts using the Google People API. Adding new contacts to Google Contacts via Apps Script, ensuring that existing contacts are not duplicated. So far, I have:
Enabled the People API in Google Apps Script. Authorized the necessary permissions for the script to interact with Google Contacts. However, I’m facing some issues.
My questions are:
How can I fetch a list of contacts (names, phone numbers, etc.) from Google Contacts using Google Apps Script and the People API?
How can I add a new contact to Google Contacts via Apps Script? I want to ensure that if the contact already exists, it won’t be duplicated.
Could anyone help me with the basic steps or point out what I'm missing?
Upvotes: 1
Views: 179
Reputation: 64040
Here's an example of how I used it. I'm not saying it's perfect but I'm using it this way currently.
function displayCurrentContacts() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Contacts');
sh.clearContents();
const vs = [['Name', 'Emails']];
const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
//Logger.log(resp);
const data = JSON.parse(resp);
let m = 0;
let n = 0;
data.connections.forEach((ob1, i) => {
if (ob1.emailAddresses && ob1.emailAddresses.length > 0) {
let emails = [... new Set(ob1.emailAddresses.map(ob2 => ob2.value))];//used set to insure I get unique list
//let emails = ob1.emailAddresses.map(ob2 => ob2.value);
let name;
m += emails.length;
if (ob1.names && ob1.organizations) {
name = ob1.names[0].displayName + '\n' + ob1.organizations[0].name;
++n;
} else if (ob1.names) {
name = ob1.names[0].displayName;
++n;
} else if (ob1.organizations) {
name = ob1.organizations[0].name;
++n;
}
vs.push([name, emails.sort().join('\n')])
}
});
vs.push([n, m])
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs)
sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).sort({ column: 1, sortAscending: true });
}
function updateWhiteList() {
const ss = SpreadsheetApp.openById(getGlobal('ssid'));
const sh = ss.getSheetByName(getGlobal('wlid'));;
const sr = 2;
if (sh.getLastRow() > 1) {
sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 1).clearContent();
}
SpreadsheetApp.flush();
const resp = People.People.Connections.list('people/me', { personFields: "emailAddresses,names,organizations" });
//Logger.log(resp);
const data = JSON.parse(resp);
let elist = [];
data.connections.forEach((ob1, i) => { if (ob1.emailAddresses && ob1.emailAddresses.length > 0) { ob1.emailAddresses.forEach(ob2 => elist.push(ob2.value)); } });
elist = [... new Set(elist)];
let elist1 = elist.map(function (e) { return e.split('').reverse().join(""); })
elist1.sort();
let result = elist1.map(function (e) { return [e.split('').reverse().join("")]; });
sh.getRange(sr, 1, result.length, 1).setValues(result);
return result.length;
}
Upvotes: 2
Reputation: 1358
If you are looking to list connections. You can use the end point:
GET https://people.googleapis.com/v1/{resourceName=people/*}/connections
You can refer to this documentation to check and modify the request based on what you need.
To modify the data you want the People API to return you will need to edit the personFields
change it accordingly to your needs.
To create a new contact. You can use this endpoint.
POST https://people.googleapis.com/v1/people:createContact
You can refer to this documentation based on how to create a new contact.
On your last question there are a lot of ways to implement this, what you need is to make use of the said 2 endpoints, along with the people.get method on this documentation.
Upvotes: 1