i want to get the full list of my google contacts on a JSON file, but im only getting the last value on my contacts.
What should i change? or what is the problem here?
function contactsJSON() {
var contactsjson = {}
var myContacts = ContactsApp.getContactGroup('System Group: My Contacts').getContacts();
for (var i = 0; i < myContacts.length; i++) {
var id = myContacts[i].getId();
var name = myContacts[i].getFullName();
var emailsobj = myContacts[i].getEmails();
var email = {}
for (var j = 0; j < emailsobj.length; j++) {
email[emailsobj[j].getAddress().toLowerCase()] = 1;
};
var phonesobj = myContacts[i].getPhones();
var phone = {}
for (var j = 0; j < phonesobj.length; j++) {
phone[phonesobj[j].getPhoneNumber().replace(/[_)(\s.-]/g, '')] = 1;
};
for (var j = 0; j < id.length; j++) {
contactsjson = { id: id, name: name, phone: phone, email: email };
}
}
contactsjson = JSON.stringify(contactsjson);
contactsjson = JSON.parse(contactsjson);
Logger.log(contactsjson);
return contactsjson;
}
Hope you can help me,
Federico.
I just figured it out.
The problem was in:
for (var j = 0; j < id.length; j++) {
contactsjson = { id: id, name: name, phone: phone, email: email };
I changed it to:
for (var j = 0; j < phonesobj.length; j++) {
contactsjson[phonesobj[j].getPhoneNumber()] = {id: id, name: name, phone: phone, email: email}
and now its properly looping trough all the elements in My contacts.
Regards,
Federico.