Tuesday, December 8, 2015

Fix missing country codes for all your Google contracts

When I recently moved from US to Sweden I realized that many of my beloved Google Contact phone numbers didn't have a country code specified, which has caused a number of issues.  From the obvious one of not being able to complete a call, to not being able to locate friends on WhatsApp.

Anyway, in google contacts you can set default country code, but that only works well going forward, so I wrote a simple script for Google Apps Script to solve the problem. The script is listed below.

Disclaimer: I have tested the script many ways, but please use at your own risk !

To run simply:
- Go to https://script.google.com,
- Start a blank project, and copy/paste the script below
- Run once with UNCOMMENT line commented out (as is)
- Press Ctrl-Enter to view logs to make sure everything looks fine.
- Uncomment the UNCOMMENT line, and run again to add country codes.

Nifty,
Thomas

// ============================================================
// What:  Find and fix all phones from your Google contacts without country codes
// How:   1) Update list of common area codes you want to fix. See phone_fixes. Otherwise it could add invalid country code to contacts
//        2) Run once to verify that correct phones will be auto fixed.
//           Review log file (press ctrl-enter)
//        2) Uncomment the line after UNCOMMENT to run and update all contact that will be auto-fixed.
//
// Dec 2015, Thomas Gemal
// ============================================================
function FindPhonesWithoutCountryCodes () {

  // Grab all contacts
  var myContacts = ContactsApp.getContacts();
  var num_contact_without_phones = 0;
  var num_phones_to_fix = 0;
  var num_phones_auto = 0;
  
  // Add all the starting areas codes to fix, and country code to pre-pend. Add as many as you like
  var phone_fixes = [
    ["206", "+1"],
    ["302", "+1"],
    ["312", "+1"],
    ["(206)", "+1"],
    ["(302)", "+1"],
    ["(312)", "+1"],
  ];
    
  // For each contact group
  for(var i = 0; i < myContacts.length; i++) { 

    // Get contact info
    var contact = myContacts[i];
    var phones = contact.getPhones();
    var num_phones = phones.length;
    var name = contact.getFullName();
    
    if (num_phones == 0) {
      num_contact_without_phones++;
      continue;
    }

    var printed = false;
    // For each phone per contact
    for(var p = 0; p < num_phones; p++) { 

      var phone = phones[p].getPhoneNumber();

      // Skip if already country code
      if (phone.substring(0,1) == "+" ) {
        continue;
      }
      
      if (!printed) {
        Logger.log(Utilities.formatString("%s: %d phone(s)",name,num_phones));
        printed = true;
      }
      
      num_phones_to_fix++;
      
      var auto = false;
      // Check if phone number matches to the ones we want to fix
      for(var pf = 0; pf < phone_fixes.length; pf++) { 

        if (phone.substring(0,phone_fixes[pf][0].length) == phone_fixes[pf][0]) {

          // matched. Now prepend the country code
          var phone_new = phone_fixes[pf][1] + " " + phone;
          Logger.log(Utilities.formatString(" Fix-auto    %s  -> %s",phone, phone_new));
        
          // UNCOMMENT: Updates the phone number. Uncomment next line when fully tested
          //phones[p].setPhoneNumber(phone_new);
          
          auto = true;
          num_phones_auto++;
          continue;
        }
      }
      if (!auto) {
        Logger.log(Utilities.formatString(" Fix-manual  %s",phone));
      }
    }
  }
  
  Logger.log(Utilities.formatString(" Contacts:         %s",i));
  Logger.log(Utilities.formatString("   No Phone:       %s",num_contact_without_phones));
  Logger.log(Utilities.formatString("   Phone to fix:   %s",num_phones_to_fix));
  Logger.log(Utilities.formatString("    Auto:          %s",num_phones_auto));
  Logger.log(Utilities.formatString("    Manual:        %s",num_phones_to_fix - num_phones_auto));
  
}