Skip to content

Commit a959dc2

Browse files
kar320vinay-google
andauthored
feat: Add examples to People API advanced service (googleworkspace#374)
* Adds examples to People API advanced service * fix: Lint errors and replace for loop with find * fix: missing semi-colon * chore: more lint fixes * chore: more lint fixes Co-authored-by: Vinay Vyas <69166360+vinay-google@users.noreply.github.com>
1 parent eb46792 commit a959dc2

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

advanced/people.gs

+113
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,116 @@ function getAccount(accountId) {
7474
}
7575
}
7676
// [END people_get_account]
77+
78+
// [START people_get_group]
79+
80+
/**
81+
* Gets a contact group with the given name
82+
* @param {string} name The group name.
83+
* @see https://developers.google.com/people/api/rest/v1/contactGroups/list
84+
*/
85+
function getContactGroup(name) {
86+
try {
87+
const people = People.ContactGroups.list();
88+
// Finds the contact group for the person where the name matches.
89+
const group = people['contactGroups'].find((group) => group['name'] === name);
90+
// Prints the contact group
91+
console.log('Group: %s', JSON.stringify(group, null, 2));
92+
} catch (err) {
93+
// TODO (developers) - Handle exception
94+
console.log('Failed to get the contact group with an error %s', err.message);
95+
}
96+
}
97+
98+
// [END people_get_group]
99+
100+
// [START people_get_contact_by_email]
101+
102+
/**
103+
* Gets a contact by the email address.
104+
* @param {string} email The email address.
105+
* @see https://developers.google.com/people/api/rest/v1/people.connections/list
106+
*/
107+
function getContactByEmail(email) {
108+
try {
109+
// Gets the person with that email address by iterating over all contacts.
110+
const people = People.People.Connections.list('people/me', {
111+
personFields: 'names,emailAddresses'
112+
});
113+
const contact = people['connections'].find((connection) => {
114+
return connection['emailAddresses'].some((emailAddress) => emailAddress['value'] === email);
115+
});
116+
// Prints the contact.
117+
console.log('Contact: %s', JSON.stringify(contact, null, 2));
118+
} catch (err) {
119+
// TODO (developers) - Handle exception
120+
console.log('Failed to get the connection with an error %s', err.message);
121+
}
122+
}
123+
124+
// [END people_get_contact_by_email]
125+
126+
// [START people_get_full_name]
127+
/**
128+
* Gets the full name (given name and last name) of the contact as a string.
129+
* @see https://developers.google.com/people/api/rest/v1/people/get
130+
*/
131+
function getFullName() {
132+
try {
133+
// Gets the person by specifying resource name/account ID
134+
// in the first parameter of People.People.get.
135+
// This example gets the person for the user running the script.
136+
const people = People.People.get('people/me', {personFields: 'names'});
137+
// Prints the full name (given name + family name)
138+
console.log(`${people['names'][0]['givenName']} ${people['names'][0]['familyName']}`);
139+
} catch (err) {
140+
// TODO (developers) - Handle exception
141+
console.log('Failed to get the connection with an error %s', err.message);
142+
}
143+
}
144+
145+
// [END people_get_full_name]
146+
147+
// [START people_get_phone_numbers]
148+
/**
149+
* Gets all the phone numbers for this contact.
150+
* @see https://developers.google.com/people/api/rest/v1/people/get
151+
*/
152+
function getPhoneNumbers() {
153+
try {
154+
// Gets the person by specifying resource name/account ID
155+
// in the first parameter of People.People.get.
156+
// This example gets the person for the user running the script.
157+
const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
158+
// Prints the phone numbers.
159+
console.log(people['phoneNumbers']);
160+
} catch (err) {
161+
// TODO (developers) - Handle exception
162+
console.log('Failed to get the connection with an error %s', err.message);
163+
}
164+
}
165+
166+
// [END people_get_phone_numbers]
167+
168+
// [START people_get_single_phone_number]
169+
/**
170+
* Gets a phone number by type, such as work or home.
171+
* @see https://developers.google.com/people/api/rest/v1/people/get
172+
*/
173+
function getPhone() {
174+
try {
175+
// Gets the person by specifying resource name/account ID
176+
// in the first parameter of People.People.get.
177+
// This example gets the person for the user running the script.
178+
const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
179+
// Gets phone number by type, such as home or work.
180+
const phoneNumber = people['phoneNumbers'].find((phone) => phone['type'] === 'home')['value'];
181+
// Prints the phone numbers.
182+
console.log(phoneNumber);
183+
} catch (err) {
184+
// TODO (developers) - Handle exception
185+
console.log('Failed to get the connection with an error %s', err.message);
186+
}
187+
}
188+
189+
// [END people_get_single_phone_number]

0 commit comments

Comments
 (0)