Created
December 12, 2023 00:02
-
-
Save kusold/c92a3fa7d6db448b4c3df9d5560cee72 to your computer and use it in GitHub Desktop.
Sync addresses from Contacts to Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetchContactAddress() { | |
const contacts = getAllContacts(); | |
console.log(`Expected: ${contacts.totalItems} \t Actual: ${contacts.connections.length}`) | |
// Example Entry | |
//console.log(contacts.connections.find(contact => contact.addresses)) | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
var range = sheet.getRange("C2:C"); // Column with the contact name | |
var values = range.getValues(); | |
var addresses = []; | |
for (var i = 0; i < values.length; i++) { | |
var contactName = values[i][0]; | |
let contact = getContact(contacts, contactName); | |
var address = getAddress(contact); | |
addresses.push(address); | |
} | |
// Update the sheet with addresses | |
var outputRange = sheet.getRange(`D2:H${1 + addresses.length}`); // Adjust the output range | |
outputRange.setValues(addresses); | |
} | |
function getAllContacts() { | |
const combinedResponses = { | |
connections: [], | |
totalItems: 0, | |
} | |
let resp = getContacts(); | |
combinedResponses.connections = combinedResponses.connections.concat(resp.connections); | |
combinedResponses.totalItems = resp.totalItems; | |
while(resp.nextPageToken) { | |
resp = getContacts(resp.nextPageToken); | |
combinedResponses.connections = combinedResponses.connections.concat(resp.connections); | |
} | |
return combinedResponses; | |
} | |
function getContacts(nextPageToken) { | |
try { | |
// Get the list of connections/contacts of user's profile | |
let contacts = People.People.Connections.list('people/me', { | |
personFields: 'names,addresses', | |
page_token: nextPageToken, | |
}); | |
// Print the connections/contacts | |
//console.log('Connections: %s', JSON.stringify(contacts, null, 2)); | |
return contacts; | |
} catch (err) { | |
// TODO (developers) - Handle exception here | |
console.log('Failed to get the connection with an error %s', err.message); | |
} | |
} | |
function getContact(contacts, displayName) { | |
return contacts.connections.find(contact => { | |
if (contact.names) { | |
return contact.names.find(name => { | |
return name.displayName == displayName || name.unstructuredName == displayName | |
}); | |
} | |
}); | |
} | |
function getAddress(contact) { | |
// Address, Address 2, City, State, Zipcode | |
const result = [undefined, undefined, undefined, undefined, undefined]; | |
if (!contact || !contact.addresses) { | |
return result; | |
} | |
console.log(contact); | |
let address; | |
if (contact.addresses.length == 1) { | |
address = contact.addresses[0]; | |
} | |
if (contact.addresses.length > 1) { | |
address = contact.addresses.find(addr => addr.type == "home"); | |
} | |
result[0] = address.streetAddress; | |
result[1] = address.extendedAddress; | |
result[2] = address.city; | |
result[3] = address.region; | |
result[4] = address.postalCode; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment