From bfdf2ba86013fd1fdaeeb4d1a06923579b86649d Mon Sep 17 00:00:00 2001 From: Kara <62033369+kar320@users.noreply.github.com> Date: Tue, 13 Dec 2022 12:12:01 -0700 Subject: [PATCH 1/5] Adds examples to People API advanced service --- advanced/people.gs | 138 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 132 insertions(+), 6 deletions(-) diff --git a/advanced/people.gs b/advanced/people.gs index aa8d996c1..7a59590d9 100644 --- a/advanced/people.gs +++ b/advanced/people.gs @@ -25,10 +25,10 @@ function getConnections() { personFields: 'names,emailAddresses' }); // Print the connections/contacts - Logger.log('Connections: %s', JSON.stringify(people, null, 2)); + console.log('Connections: %s', JSON.stringify(people, null, 2)); } catch (err) { // TODO (developers) - Handle exception here - Logger.log('Failed to get the connection with an error %s', err.message); + console.log('Failed to get the connection with an error %s', err.message); } } // [END people_get_connections] @@ -46,10 +46,10 @@ function getSelf() { personFields: 'names,emailAddresses' // Use other query parameter here if needed }); - Logger.log('Myself: %s', JSON.stringify(people, null, 2)); + console.log('Myself: %s', JSON.stringify(people, null, 2)); } catch (err) { // TODO (developer) -Handle exception - Logger.log('Failed to get own profile with an error %s', err.message); + console.log('Failed to get own profile with an error %s', err.message); } } // [END people_get_self_profile] @@ -67,10 +67,136 @@ function getAccount(accountId) { personFields: 'names,emailAddresses' }); // Print the profile details of Account. - Logger.log('Public Profile: %s', JSON.stringify(people, null, 2)); + console.log('Public Profile: %s', JSON.stringify(people, null, 2)); } catch (err) { // TODO (developer) - Handle exception - Logger.log('Failed to get account with an error %s', err.message); + console.log('Failed to get account with an error %s', err.message); } } // [END people_get_account] + +// [START people_get_group] + +/** + * Gets a contact group with the given name + * @param {string} name The group name. + * @see https://developers.google.com/people/api/rest/v1/contactGroups/list + */ +function getContactGroup(name) { + try { + var group = null; + const people = People.ContactGroups.list(); + // Iterates over all contact groups for the person to find where the name matches. + for (var i = 0; i < people["contactGroups"].length; i++) { + if (people["contactGroups"][i]["name"] == name) { + group = people["contactGroups"][i]; + break; + } + } + // Prints the contact group + console.log('Group: %s', JSON.stringify(group, null, 2)); + } catch (err) { + // TODO (developers) - Handle exception + console.log('Failed to get the contact group with an error %s', err.message); + } +} + +// [END people_get_group] + +// [START people_get_contact_by_email] + +/** + * Gets a contact by the email address. + * @param {string} email The email address. + * @see https://developers.google.com/people/api/rest/v1/people.connections/list + */ +function getContactByEmail(email) { + try { + // Gets the person with that email address by iterating over all contacts. + const people = People.People.Connections.list('people/me', { personFields: 'names,emailAddresses' }); + var contact = null; + for (var i = 0; i < people["connections"].length; i++) { + for (var j = 0; j < people["connections"][i]["emailAddresses"].length; j++) { + if (people["connections"][i]["emailAddresses"][j]["value"] == email) { + contact = people["connections"][i]; + break; + } + } + } + // Prints the contact. + console.log('Contact: %s', JSON.stringify(contact, null, 2)); + } catch (err) { + // TODO (developers) - Handle exception + console.log('Failed to get the connection with an error %s', err.message); + } +} + +// [END people_get_contact_by_email] + +// [START people_get_full_name] +/** + * Gets the full name (given name and last name) of the contact as a string. + * @see https://developers.google.com/people/api/rest/v1/people/get + */ +function getFullName() { + try { + // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // This example gets the person for the user running the script. + const people = People.People.get('people/me', { personFields: 'names' }); + // Prints the full name (given name + family name) + console.log(people["names"][0]["givenName"] + ' ' + people["names"][0]["familyName"]); + } catch (err) { + // TODO (developers) - Handle exception + console.log('Failed to get the connection with an error %s', err.message); + } +} + +// [END people_get_full_name] + +// [START people_get_phone_numbers] +/** + * Gets all the phone numbers for this contact. + * @see https://developers.google.com/people/api/rest/v1/people/get + */ +function getPhoneNumbers() { + try { + // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // This example gets the person for the user running the script. + const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); + // Prints the phone numbers. + console.log(people["phoneNumbers"]); + } catch (err) { + // TODO (developers) - Handle exception + console.log('Failed to get the connection with an error %s', err.message); + } +} + +// [END people_get_phone_numbers] + +// [START people_get_single_phone_number] +/** + * Gets a phone number by type, such as work or home. + * @see https://developers.google.com/people/api/rest/v1/people/get + */ +function getPhone() { + var phoneNumber; + try { + // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // This example gets the person for the user running the script. + const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); + for (var i = 0; i < people["phoneNumbers"].length; i++) { + // Gets phone number by type, such as home or work. + if (people["phoneNumbers"][i]["type"] == "home") { + phoneNumber = people["phoneNumbers"][i]["value"]; + break; + } + } + // Prints the phone numbers. + console.log(phoneNumber); + } catch (err) { + // TODO (developers) - Handle exception + console.log('Failed to get the connection with an error %s', err.message); + } +} + +// [END people_get_single_phone_number] From 76330c95a1c810d211c48c0f6dd6dc28324ea552 Mon Sep 17 00:00:00 2001 From: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> Date: Tue, 13 Dec 2022 16:50:21 -0800 Subject: [PATCH 2/5] fix: Lint errors and replace for loop with find --- advanced/people.gs | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/advanced/people.gs b/advanced/people.gs index 7a59590d9..55a0cee36 100644 --- a/advanced/people.gs +++ b/advanced/people.gs @@ -84,15 +84,9 @@ function getAccount(accountId) { */ function getContactGroup(name) { try { - var group = null; const people = People.ContactGroups.list(); - // Iterates over all contact groups for the person to find where the name matches. - for (var i = 0; i < people["contactGroups"].length; i++) { - if (people["contactGroups"][i]["name"] == name) { - group = people["contactGroups"][i]; - break; - } - } + // Finds the contact group for the person where the name matches. + const group = people['contactGroups'].find(group => group['name'] === name) // Prints the contact group console.log('Group: %s', JSON.stringify(group, null, 2)); } catch (err) { @@ -114,15 +108,9 @@ function getContactByEmail(email) { try { // Gets the person with that email address by iterating over all contacts. const people = People.People.Connections.list('people/me', { personFields: 'names,emailAddresses' }); - var contact = null; - for (var i = 0; i < people["connections"].length; i++) { - for (var j = 0; j < people["connections"][i]["emailAddresses"].length; j++) { - if (people["connections"][i]["emailAddresses"][j]["value"] == email) { - contact = people["connections"][i]; - break; - } - } - } + const contact = people['connections'].find(connection => { + return connection['emailAddresses'].some(emailAddress => emailAddress['value'] === email); + }); // Prints the contact. console.log('Contact: %s', JSON.stringify(contact, null, 2)); } catch (err) { @@ -144,7 +132,7 @@ function getFullName() { // This example gets the person for the user running the script. const people = People.People.get('people/me', { personFields: 'names' }); // Prints the full name (given name + family name) - console.log(people["names"][0]["givenName"] + ' ' + people["names"][0]["familyName"]); + console.log(`${people['names'][0]['givenName']} ${people['names'][0]['familyName']}`); } catch (err) { // TODO (developers) - Handle exception console.log('Failed to get the connection with an error %s', err.message); @@ -164,7 +152,7 @@ function getPhoneNumbers() { // This example gets the person for the user running the script. const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); // Prints the phone numbers. - console.log(people["phoneNumbers"]); + console.log(people['phoneNumbers']); } catch (err) { // TODO (developers) - Handle exception console.log('Failed to get the connection with an error %s', err.message); @@ -179,18 +167,12 @@ function getPhoneNumbers() { * @see https://developers.google.com/people/api/rest/v1/people/get */ function getPhone() { - var phoneNumber; try { // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. // This example gets the person for the user running the script. const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); - for (var i = 0; i < people["phoneNumbers"].length; i++) { - // Gets phone number by type, such as home or work. - if (people["phoneNumbers"][i]["type"] == "home") { - phoneNumber = people["phoneNumbers"][i]["value"]; - break; - } - } + // Gets phone number by type, such as home or work. + const phoneNumber = people['phoneNumbers'].find(phone => phone['type'] === 'home')['value'] // Prints the phone numbers. console.log(phoneNumber); } catch (err) { From 91385ec247d4e9f7c7e7dfa45371b4621f545c35 Mon Sep 17 00:00:00 2001 From: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> Date: Tue, 13 Dec 2022 16:51:52 -0800 Subject: [PATCH 3/5] fix: missing semi-colon --- advanced/people.gs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/people.gs b/advanced/people.gs index 55a0cee36..159050e93 100644 --- a/advanced/people.gs +++ b/advanced/people.gs @@ -86,7 +86,7 @@ function getContactGroup(name) { try { const people = People.ContactGroups.list(); // Finds the contact group for the person where the name matches. - const group = people['contactGroups'].find(group => group['name'] === name) + const group = people['contactGroups'].find(group => group['name'] === name); // Prints the contact group console.log('Group: %s', JSON.stringify(group, null, 2)); } catch (err) { From 32628e7fc92d2110959dfb833cb63815545d0c76 Mon Sep 17 00:00:00 2001 From: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> Date: Tue, 13 Dec 2022 17:01:13 -0800 Subject: [PATCH 4/5] chore: more lint fixes --- advanced/people.gs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/advanced/people.gs b/advanced/people.gs index 159050e93..56435f149 100644 --- a/advanced/people.gs +++ b/advanced/people.gs @@ -86,7 +86,7 @@ function getContactGroup(name) { try { const people = People.ContactGroups.list(); // Finds the contact group for the person where the name matches. - const group = people['contactGroups'].find(group => group['name'] === name); + const group = people['contactGroups'].find((group) => group['name'] === name); // Prints the contact group console.log('Group: %s', JSON.stringify(group, null, 2)); } catch (err) { @@ -107,9 +107,11 @@ function getContactGroup(name) { function getContactByEmail(email) { try { // Gets the person with that email address by iterating over all contacts. - const people = People.People.Connections.list('people/me', { personFields: 'names,emailAddresses' }); - const contact = people['connections'].find(connection => { - return connection['emailAddresses'].some(emailAddress => emailAddress['value'] === email); + const people = People.People.Connections.list('people/me', { + personFields: 'names,emailAddresses' + }); + const contact = people['connections'].find((connection) => { + return connection['emailAddresses'].some((emailAddress) => emailAddress['value'] === email); }); // Prints the contact. console.log('Contact: %s', JSON.stringify(contact, null, 2)); @@ -128,9 +130,10 @@ function getContactByEmail(email) { */ function getFullName() { try { - // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // Gets the person by specifying resource name/account ID + // in the first parameter of People.People.get. // This example gets the person for the user running the script. - const people = People.People.get('people/me', { personFields: 'names' }); + const people = People.People.get('people/me', {personFields: 'names'}); // Prints the full name (given name + family name) console.log(`${people['names'][0]['givenName']} ${people['names'][0]['familyName']}`); } catch (err) { From 8bd4805c8f61c678d7b11d73975ff2b282088628 Mon Sep 17 00:00:00 2001 From: Vinay Vyas <69166360+vinay-google@users.noreply.github.com> Date: Tue, 13 Dec 2022 17:08:53 -0800 Subject: [PATCH 5/5] chore: more lint fixes --- advanced/people.gs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/advanced/people.gs b/advanced/people.gs index 56435f149..10cf68d09 100644 --- a/advanced/people.gs +++ b/advanced/people.gs @@ -151,9 +151,10 @@ function getFullName() { */ function getPhoneNumbers() { try { - // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // Gets the person by specifying resource name/account ID + // in the first parameter of People.People.get. // This example gets the person for the user running the script. - const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); + const people = People.People.get('people/me', {personFields: 'phoneNumbers'}); // Prints the phone numbers. console.log(people['phoneNumbers']); } catch (err) { @@ -171,11 +172,12 @@ function getPhoneNumbers() { */ function getPhone() { try { - // Gets the person by specifying resource name/account ID in the first parameter of People.People.get. + // Gets the person by specifying resource name/account ID + // in the first parameter of People.People.get. // This example gets the person for the user running the script. - const people = People.People.get('people/me', { personFields: 'phoneNumbers' }); + const people = People.People.get('people/me', {personFields: 'phoneNumbers'}); // Gets phone number by type, such as home or work. - const phoneNumber = people['phoneNumbers'].find(phone => phone['type'] === 'home')['value'] + const phoneNumber = people['phoneNumbers'].find((phone) => phone['type'] === 'home')['value']; // Prints the phone numbers. console.log(phoneNumber); } catch (err) {