Hitting User Quota Limits

342 views Asked by At

I'm currently using this Script to grab all my contacts into a Google Sheet. Ideally I'd like to have it run as often as possible. With a trigger set to every 1hr, I receive the following quota limit.

Temporary problem - please try again later and consider using batch operations. The user is over quota.

Is there a more efficient way to batch the following script so that it can run more often? Or maybe only when a contact has been updated/created?

function onOpen()
{
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [];
  menuEntries.push( {name: "Read Contacts", functionName: "readContacts"} );
  spreadsheet.addMenu("Contacts", menuEntries);
};


function readContacts() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Contacts");

  sheet.clear();
  var group  = ContactsApp.getContactGroup('Apptivo Contacts');
  var contacts = ContactsApp.getContactsByGroup(group);
//var contacts = ContactsApp.getContacts();
  var ContactArray  = new Array();
  var ContactArrays = [];

  ContactArray = [];
  ContactArray.push("");
  ContactArray.push("FullName");
  ContactArray.push("Emails");
  ContactArray.push("PhoneNumbers");
//ContactArray.push("HomePhone");
//ContactArray.push("WorkPhone");
  ContactArray.push("Company");
  ContactArray.push("Job Title");
  ContactArray.push("Notes");  
  ContactArray.push("HomeAddress");
  ContactArray.push("WorkAddress");
  ContactArray.push("URL");
  ContactArray.push("Groups");
//ContactArray.push("Group1");
//ContactArray.push("Group2");

  ContactArrays.push(ContactArray);

  for (var i=0;i<contacts.length;i++)
  { 
    ContactArray = [];
    ContactArray.push("");
    ContactArray.push(contacts[i].getFullName());

  //Emails
    var Emails = "";
    for ( var g=0;g<contacts[i].getEmails().length;g++)
    {
      Emails += contacts[i].getEmails()[g].getAddress();
      if (g + 1 == contacts[i].getEmails().length) break
      Emails += "\n";
    }
    try{ContactArray.push(Emails);}
      catch(e){ContactArray.push("N/A")}


  //Phone Numbers
    var Phones = "";    
    for ( var g=0;g<contacts[i].getPhones().length;g++)
    {
      if (contacts[i].getPhones()[g].getLabel() == "MOBILE_PHONE") {
        Phones += "C: "
      } else if (contacts[i].getPhones()[g].getLabel() == "WORK_PHONE") {
        Phones += "W: "
      } else if (contacts[i].getPhones()[g].getLabel() == "HOME_PHONE") {
        Phones += "H: "
      } else if (contacts[i].getPhones()[g].getLabel() == "HOME_FAX") {
        Phones += "F: "
      } else if (contacts[i].getPhones()[g].getLabel() == "WORK_FAX") {
        Phones += "F: "
      } else {
        Phones += "O: "
      }
      Phones += contacts[i].getPhones()[g].getPhoneNumber();
      if (g + 1 == contacts[i].getPhones().length) break
      Phones += "\n" ;
    }
    try{ContactArray.push(Phones);}
    catch(e){ContactArray.push("N/A")}

    try{ContactArray.push(contacts[i].getCompanies()[0].getCompanyName());}
    catch(e){ContactArray.push("N/A")}
    try{ContactArray.push(contacts[i].getCompanies()[0].getJobTitle());}
    catch(e){ContactArray.push("N/A")}

    ContactArray.push(contacts[i].getNotes());

  //Addresses
    var homeAddress = "" , workAddress = "";     
    for ( var g=0;g<contacts[i].getAddresses().length;g++)
    {
      if (contacts[i].getAddresses()[g].getLabel() == "HOME_ADDRESS") {
        homeAddress += contacts[i].getAddresses()[g].getAddress();
      } else if (contacts[i].getAddresses()[g].getLabel() == "WORK_ADDRESS") {
        workAddress += contacts[i].getAddresses()[g].getAddress();
      }
    }

    try{ContactArray.push(homeAddress);}
      catch(e){ContactArray.push("N/A")}
    try{ContactArray.push(workAddress);}
      catch(e){ContactArray.push("N/A")}  

    //ContactArray.push(contacts[i].getAddresses().getAddress()); 

    try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
    catch(e){ContactArray.push("N/A")}

    var ListofGroups = "";    
    for ( var g=0;g<contacts[i].getContactGroups().length;g++)
    {
      ListofGroups += contacts[i].getContactGroups()[g].getName();
      ListofGroups += " | ";
    }
    try{ContactArray.push(ListofGroups);}
      catch(e){ContactArray.push("N/A")}

  //try{ContactArray.push(contacts[i].getContactGroups()[1].getName());}
  //catch(e){ContactArray.push("N/A")}

  //try{ContactArray.push(contacts[i].getContactGroups()[2].getName());}
  //catch(e){ContactArray.push("N/A")}


    ContactArrays.push(ContactArray);
  }

  sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
};
1

There are 1 answers

0
David Meffe On BEST ANSWER

Thanks to Zaq, I reduced the number of get* calls in my script -- these calls are what drained the Google Services quota.

The script now takes a fraction of the time to run. (from 2mins to under 20sec)

Although they don't affect the quotas, I used map and join methods of JavaScript arrays to shorten some code.

My end result...

function readContacts() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Contacts");

  var group  = ContactsApp.getContactGroup('Apptivo Contacts');
  var contacts = ContactsApp.getContactsByGroup(group);
  var ContactArray  = new Array();
  var ContactArrays = [];

  ContactArray = [];
  ContactArray.push("");
  ContactArray.push("FullName");
  ContactArray.push("Emails");
  ContactArray.push("PhoneNumbers");
  ContactArray.push("Company");
  ContactArray.push("Job Title");
  ContactArray.push("Notes");  
  ContactArray.push("HomeAddress");
  ContactArray.push("WorkAddress");
  ContactArray.push("URL");
  ContactArray.push("Groups");

  ContactArrays.push(ContactArray);

  for (var i=0;i<contacts.length;i++)
  { 
    ContactArray = [];
    ContactArray.push("");

  //FullName
    ContactArray.push(contacts[i].getFullName());

  //Emails
    var Emails = contacts[i].getEmails().map(function(email) {
      return email.getAddress();
      }).join("\n");

    try{ContactArray.push(Emails);}
      catch(e){ContactArray.push("N/A")}

  //Phone Numbers
    var Phones = "";
    var contactPhones = contacts[i].getPhones();    
    for ( var g=0;g<contactPhones.length;g++)
    {
      if (contactPhones[g].getLabel() == "MOBILE_PHONE") {
        Phones += "C: "
      } else if (contactPhones[g].getLabel() == "WORK_PHONE") {
        Phones += "W: "
      } else if (contactPhones[g].getLabel() == "HOME_PHONE") {
        Phones += "H: "
      } else if (contactPhones[g].getLabel() == "HOME_FAX") {
        Phones += "F: "
      } else if (contactPhones[g].getLabel() == "WORK_FAX") {
        Phones += "F: "
      } else {
        Phones += "O: "
      }
      Phones += contactPhones[g].getPhoneNumber();
      if (g + 1 == contactPhones.length) break
      Phones += "\n" ;
    }
    try{ContactArray.push(Phones);}
    catch(e){ContactArray.push("N/A")}

  //Company
    var contactCompany = contacts[i].getCompanies();
    try{ContactArray.push(contactCompany[0].getCompanyName());}
    catch(e){ContactArray.push("N/A")}

  //JobTitle
    try{ContactArray.push(contactCompany[0].getJobTitle());}
    catch(e){ContactArray.push("N/A")}

  //Notes  
    ContactArray.push(contacts[i].getNotes());

  //Addresses
    var homeAddress = "" , workAddress = "";
    var contactAddresses = contacts[i].getAddresses();
    for ( var g=0;g<contactAddresses.length;g++)
    {
      if (contactAddresses[g].getLabel() == "HOME_ADDRESS") {
        homeAddress += contactAddresses[g].getAddress();
      } else if (contactAddresses[g].getLabel() == "WORK_ADDRESS") {
        workAddress += contactAddresses[g].getAddress();
      }
    }
    //Home
    try{ContactArray.push(homeAddress);}
      catch(e){ContactArray.push("N/A")}
    //Work
    try{ContactArray.push(workAddress);}
      catch(e){ContactArray.push("N/A")}  

  //URLs  
    try{ContactArray.push(contacts[i].getUrls()[0].getAddress());}
    catch(e){ContactArray.push("N/A")}

  //Groups
    var Groups = contacts[i].getContactGroups().map(function(group) {
      return group.getName();
      }).join(" | ");
    try{ContactArray.push(Groups);}
      catch(e){ContactArray.push("N/A")}


    ContactArrays.push(ContactArray);
  }

  //If Array is not blank(to avoid quota issues)
 if (12 < ContactArrays.length) {
  // Re-populate sheet
   sheet.clear();
   sheet.getRange(1,1,ContactArrays.length,ContactArrays[0].length).setValues(ContactArrays);
   }

};