Reputation: 3
I'm new to Google Scripts and I've found very helpful information from this forum (Thank you!!). I am trying to create the script to export Google Contacts App into a Sheet. I am able to successfully get the Full Name and Email from the below script, but it is only returning "Company Field", "Phone Field" and "Contact Group". I have tried several variations of the code to try to return the actual name of the Company, Phone Number and Groups (if populated), but it can only return those header values. Any help on this would be amazing!
Here's my code:
function getName() {
var contacts = ContactsApp.getContacts(), output = [];
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
//Get Full Name
var fullname = contacts[i].getFullName();
if(fullname) {
output.push([fullname])
}
SpreadsheetApp.getActiveSheet().getRange(2, 1, output.length, 1).setValues(output);
// Gets Company Name Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var company = contacts[i].getCompanies();
if(company) {
output.push([company]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 2, output.length, 1).setValues(output);
// Gets Email Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var email = contacts[i].getEmailAddresses();
if(email) {
output.push([email]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 3, output.length, 1).setValues(output);
// Gets Phone Number
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var phone = contacts[i].getPhones();
if(phone) {
output.push([phone]);
}
}
SpreadsheetApp.getActiveSheet().getRange(2, 4, output.length, 1).setValues(output);
// Gets Tags
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var tags = contacts[i].getContactGroups();
if(tags) {
output.push([tags]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 5, output.length, 1).setValues(output);
}
Upvotes: 0
Views: 188
Reputation: 11184
The methods getCompanies
, getPhones
, getContactGroups
return an array of objects. You need to access their values each per element.
function getName() {
var contacts = ContactsApp.getContacts();
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
sheet.getRange(1, 1, 1, 5).setValues(header);
// Delimiter is set into next line within cell
// Change into comma if you want it delimited by comma
var delimiter = String.fromCharCode(10);
for (var i = 0, iLen = contacts.length; i < iLen; i++) {
// Get last row every loop
var row = sheet.getLastRow();
var fullname = contacts[i].getFullName();
sheet.getRange(row + 1, 1, 1, 1).setValue(fullname);
var company = contacts[i].getCompanies(), companies = [];
for (var j in company)
if (company[j].getCompanyName())
companies.push([company[j].getCompanyName()]);
sheet.getRange(row + 1, 2, 1, 1).setValue(companies.join(delimiter));
var email = contacts[i].getEmails(), emails = [];
for (var j in email)
if (email[j].getAddress())
emails.push([email[j].getAddress()]);
sheet.getRange(row + 1, 3, 1, 1).setValue(emails.join(delimiter));
var phone = contacts[i].getPhones(), phones = [];
for (var j in phone)
if (phone[j].getPhoneNumber())
phones.push([phone[j].getPhoneNumber()]);
sheet.getRange(row + 1, 4, 1, 1).setValue(phones.join(delimiter));
var tags = contacts[i].getContactGroups(), groups = [];
for (var j in tags)
// Default tag in my contacts seems to be "System Group: My Contacts", I did not include them
if (tags[j].getGroupName() && tags[j].getGroupName() != "System Group: My Contacts")
groups.push([tags[j].getGroupName()]);
sheet.getRange(row + 1, 5, 1, 1).setValue(groups.join(delimiter));
}
}
Upvotes: 0