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);
};
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
andjoin
methods of JavaScript arrays to shorten some code.My end result...