Skip to content

Instantly share code, notes, and snippets.

@fdiasr
Last active December 29, 2015 00:59
Show Gist options
  • Save fdiasr/7589585 to your computer and use it in GitHub Desktop.
Save fdiasr/7589585 to your computer and use it in GitHub Desktop.
Script to convert custom labels (with brazilian phone operators) to default labels on Google Contact, using Google Script. It can be necessary when you have a mobile or app that dont show numbers with this custom labels.
/**
* you nedd enable some access, and create a spreedsheat called 'contact-data', before it
* the script need to be run on google script - https://script.google.com
*/
function contactLabelChange()
{
backupContacts();
var contacts = getMyContacts();
Logger.log('Starting label chages...');
for (i = 0; i < contacts.length; i++ ) {
var name = contacts[i].getGivenName() + ' ' + contacts[i].getFamilyName();
var fullname = contacts[i].getFullName();
var mail = contacts[i].getPrimaryEmail();
var phones = contacts[i].getPhones();
for (var j in phones) {
changeLabel(phones[j]);
}
}
Logger.log('Label changes done.');
}
function backupContacts()
{
var files = DocsList.find('contact-data');
var spreadsheet = SpreadsheetApp.open(files[0]);
var sheet = spreadsheet.insertSheet(Utilities.formatDate(new Date(), 'GMT-3', 'yyyy-MM-dd HH:mm'));
spreadsheet.setActiveSheet(sheet);
var contacts = getMyContacts();
Logger.log('Running backup');
for (i = 0; i < contacts.length; i++ ) {
var name = contacts[i].getGivenName() + ' ' + contacts[i].getFamilyName();
var fullname = contacts[i].getFullName();
var mail = contacts[i].getPrimaryEmail();
var phones = contacts[i].getPhones();
for (var j in phones) {
sheet.appendRow([name, fullname, mail, phones[j].getLabel(), phones[j].getPhoneNumber()]);
}
}
return true;
}
function getMyContacts()
{
var group = ContactsApp.getContactGroup('System Group: My Contacts');
var contacts = ContactsApp.getContactsByGroup(group);
Logger.log('Find %s contacts', contacts.length);
return contacts;
}
function changeLabel(phone)
{
changeLabelToMobile(phone);
changeLabelToMain(phone);
}
function changeLabelToMobile(phone)
{
if ( phone.getLabel() == 'Oi'
|| phone.getLabel() == 'Oi Móvel'
|| phone.getLabel() == 'Vivo'
|| phone.getLabel() == 'Tim'
|| phone.getLabel() == 'TIM'
|| phone.getLabel() == 'Mobile (TIM)'
|| phone.getLabel() == 'Claro'
|| phone.getLabel() == 'NEXTEL - SPO'
) {
phone.setLabel(ContactsApp.Field.MOBILE_PHONE);
}
}
function changeLabelToMain(phone)
{
if ( phone.getLabel() == 'Oi Fixo'
|| phone.getLabel() == 'Embratel'
|| phone.getLabel() == 'Tim Fixo'
|| phone.getLabel() == 'Vivo Fixo'
|| phone.getLabel() == 'SOS PF'
) {
phone.setLabel(ContactsApp.Field.MAIN_PHONE);
}
}
function contactGroups()
{
groups = ContactsApp.getContactGroups();
for (i in groups)
{
Logger.log(groups[i].getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment