Search code examples
google-apps-scriptgoogle-docs-apigoogle-contacts-api

Google Apps Script Contacts Services error


First, I know, there is a similar question but it's just similar and not the same :)

This script import Google Contacts -> Contacts Sevices, in a spreadsheet.

function onOpen()
{
  var menuItems = [{name: "Gmail Contact Import", functionName: "getContacts"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu("import", menuItems);
}

function getContacts()
{
  var groupName = "MyGroup";
  var contacts;
  var sheet = SpreadsheetApp.getActiveSheet();

  if (groupName) {
    var contactGroup = ContactsApp.findContactGroup(groupName);

    if (contactGroup) {
      contacts = contactGroup.getContacts();
    }
  } 
  else {
    contacts = ContactsApp.getAllContacts();
  }

  if (!contacts) {
    Browser.msgBox("no contacts found!");
    return;
  }  

  sheet.clear();

  sheet.getRange("A1").setValue("forename");
  sheet.getRange("B1").setValue("lastname");
  sheet.getRange("C1").setValue("private email");
  sheet.getRange("D1").setValue(groupName + " email");
  sheet.getRange("E1").setValue("phone");
  sheet.getRange("F1").setValue("mobile");
  sheet.getRange("G1").setValue("street");
  sheet.getRange("H1").setValue("zip");
  sheet.getRange("I1").setValue("city");
  sheet.getRange("J1").setValue("group");
  sheet.getRange("K1").setValue("date1");
  sheet.getRange("L1").setValue("date2");
  sheet.getRange("M1").setValue("date3");
  sheet.getRange("N1").setValue("date4");

  for ( var i = 0; i < contacts.length; i++) {
    var addressArray = new Array();
    addressArray = contacts[i].getAddresses(ContactsApp.Field.HOME_ADDRESS);
    var addressArrayLength = addressArray.length;    
    for ( var ii = 0; ii < addressArrayLength; ii++ ) {
      var address = addressArray[ii].getAddress();
      var addressSplit = address.split(/\r\n|\r|\n/);
      var street = addressSplit[0];
      var city = addressSplit[1].split(" ")[0];
      var zip = addressSplit[1].split(" ")[1];
    }
    var homePhone = contacts[i].getHomePhone().replace("0049-","0").toString();
    var mobilePhone = contacts[i].getMobilePhone().replace("0049-","0").toString();
    var fireMail = ""
    var privateMail = "";
    var mailArray = new Array();
    mailArray = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL);
    var mailArrayLength = mailArray.length;    
    for ( var ii = 0; ii < mailArrayLength; ii++ ) {
      privateMail = contacts[i].getEmails(ContactsApp.Field.HOME_EMAIL)[ii].getAddress();
    }

    if (!privateMail)
    {
      mailArray = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL);
      mailArrayLength = mailArray.length;

      for ( var ii = 0; ii < mailArrayLength; ii++ ) {
        privateMail = contacts[i].getEmails(ContactsApp.Field.WORK_EMAIL)[ii].getAddress();
      }
    }

    mailArray = contacts[i].getEmails(groupName);
    mailArrayLength = mailArray.length;

    for ( var ii = 0; ii < mailArrayLength; ii++ ) {
      fireMail = contacts[i].getEmails(groupName)[ii].getAddress();
    }

    sheet.getRange(1*i+2, 1, 1, 1).setValue(contacts[i].getGivenName());
    sheet.getRange(1*i+2, 2, 1, 1).setValue(contacts[i].getFamilyName());
    sheet.getRange(1*i+2, 3, 1, 1).setValue(privateMail);
    sheet.getRange(1*i+2, 4, 1, 1).setValue(fireMail);
    sheet.getRange(1*i+2, 5, 1, 1).setValue(homePhone);
    sheet.getRange(1*i+2, 6, 1, 1).setValue(mobilePhone);
    sheet.getRange(1*i+2, 7, 1, 1).setValue(street);
    sheet.getRange(1*i+2, 8, 1, 1).setValue(zip);
    sheet.getRange(1*i+2, 9, 1, 1).setValue(city);

    var groups = contacts[i].getContactGroups();

    for ( var j = 0; j < groups.length; j++) {
      var name = groups[j].getName();
      switch (name) {
        case "group1":
          sheet.getRange(1*i+2, 10, 1, 1).setValue(1);
          break;
        case "group2":
          sheet.getRange(1*i+2, 10, 1, 1).setValue(2);
          break;
        case "group3":
          sheet.getRange(1*i+2, 10, 1, 1).setValue(3);
          break;
        case "group4":
          sheet.getRange(1*i+2, 10, 1, 1).setValue(4);
          break;
        case "group5":
          sheet.getRange(1*i+2, 10, 1, 1).setValue(5);
          break;
        default:
          break;
      }

      if (name.indexOf("datetrigger1") > -1)
      {
        var dateArray = contacts[i].getDates("date1");
        var dateArrayLength = dateArray.length;        
        for ( var ii = 0; ii < dateArrayLength; ii++ ) {
          var date = contacts[i].getDates("date1")[ii];
          var day = date.getDay();
          var month = date.getMonth();
          var year = date.getYear();
          sheet.getRange(1*i+2, 11, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
        }
        sheet.getRange(1*i+2, 11, 1, 1).setBackgroundColor('green');
      }

      if (name.indexOf("datetrigger2") > -1)
      {
        var dateArray = contacts[i].getDates("date2");
        var dateArrayLength = dateArray.length;        
        for ( var ii = 0; ii < dateArrayLength; ii++ ) {
          var date = contacts[i].getDates("date2")[ii];
          var day = date.getDay();
          var month = date.getMonth();
          var year = date.getYear();
          sheet.getRange(1*i+2, 12, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
        }
        sheet.getRange(1*i+2, 12, 1, 1).setBackgroundColor('green');
      }

      if (name.indexOf("datetrigger3") > -1)
      {
        var dateArray = contacts[i].getDates("date3");
        var dateArrayLength = dateArray.length;        
        for ( var ii = 0; ii < dateArrayLength; ii++ ) {
          var date = contacts[i].getDates("date3")[ii];
          var day = date.getDay();
          var month = date.getMonth();
          var year = date.getYear();
          sheet.getRange(1*i+2, 13, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
        }
        sheet.getRange(1*i+2, 13, 1, 1).setBackgroundColor('green');
      }

      if (name.indexOf("datetrigger4") > -1)
      {
        var dateArray = contacts[i].getDates("date4");
        var dateArrayLength = dateArray.length;
        if (dateArrayLength > 0) {
          for ( var ii = 0; ii < dateArrayLength; ii++ ) {
            var date = contacts[i].getDates("date4")[ii];
            var day = date.getDay();
            var month = date.getMonth();
            var year = date.getYear();
            sheet.getRange(1*i+2, 14, 1, 1).setValue(day.toString() + "." + month.toString() + "." + year.toString());
          }
          sheet.getRange(1*i+2, 14, 1, 1).setBackgroundColor('green');
        }
      }
    }
  }

  Browser.msgBox(contacts.length + " contacts imported.");
}

Works fine, but after the message "xy contacts imported" I get this error message:

Unexpected exception upon serializing continuation

I cannot figure out what's wrong.

UPDATE 2012-07-26:

I have an additional error:

Dienst-Fehler: ContactsApp: Temporary problem - please try again later and consider using batch operations. The user is over quota. (Zeile 96)

It's about this row: var groups = contacts[i].getContactGroups();

I already tried it again later ... error still comes. Always I get results in my list. The error comes after various iterations of the loop.

UPDATE 2012-07-27:

I added Utilities.sleep(2000); after var groups = contacts[i].getContactGroups();. This is already sufficient. There are not as many contacts ...


Solution

  • I have got such an error message often. Here's what I do:

    1. It may be a result of msgBox calls. Replace the Browser.msgBox("..."); with Logger.log("..."); You can then view the results in the script editor > View > Logs...

    2. The script could require authorization that is not able to be triggered in the spreadsheet UI. Run the script via script editor > Run > getContacts.

    Hope this helps.

    Update 27 July 2012:

    In response to your later question about a quota error:

    Google has several layers of defence against denial of service and one of them is quotas for users. Some of the GAS service quotas are listed here https://script.google.com/dashboard (go to Quota Limits tab) but not all of them are made public. Your error for example is not for a quota listed there.

    Another layer is rate limiting where you may hit a threshold if you make an API call too many times in a short period (say, 1 second) which is what I think is happening with you. Good practice is to use an exponential backoff. Google says you should perform up to 5 retries with exponential backoff, retrying with delays of approximately 1, 2, 4, 8 then 16 seconds for a total of about 32 seconds before you give up with an unrecoverable error. See: https://developers.google.com/google-apps/documents-list/#implementing_exponential_backoff

    I have a GASRetry library function that I use to do exponential backoff automatically. I maintain it here and it's available as a public library project key: MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE too

    Taking your line of code as an example, you would wrap it in a GASRetry call like this:

    //var groups = contacts[i].getContactGroups();
    var groups = GASRetry.call(function(){contacts[i].getContactGroups()});
    

    This should overcome any short term rate limits. Let us know how you go.