I am creating an script responsible of generating a report containing info about a set of groups and its contacts in google app script.
If I select a few groups, everything goes OK but If I select several (20 or more), then the scripts launches the "Exceeded maximum execution time" error.
How could I reduce the script time execution?
Thanks!
My (reduced) script
var allContactsInfo = []
/II. For each group
for(var i = 0 ; i < numberOfGroups ; i++)
{
var selected = eventInfo.parameter["group"+i]
var contactInfo = []
//III. If it has been selected by the user
if(selected)
{
//IV. Get the contact group and contacts
var contactGroup = ContactsApp.getContactGroupById(eventInfo.parameter["group_"+i+"_id"])
var contacts = contactGroup.getContacts()
//IV. Iterate over each group contact
for (var j=0; j < contacts.length; j++)
{
var contact = contacts[j];
contactInfo.push(contact.getGivenName())
contactInfo.push(contact.getFamilyName())
var groups = contact.getContactGroups()
var groupsAsArray = []
for (var k = 0 ; k < groups.length; k++)
{
groupsAsArray.push(groups[k].getName())
}
contactInfo.push(groupsAsArray.sort().join())
//V. Add the contact into the array
allContactsInfo.push(contactInfo)
}
}
...
...
//VI. Fill the spreadsheet with the array built within the loop
sheet.getRange(1, 1, allContactsInfo.length, headers.length).setValues(allContactsInfo);
UPDATE
After some tests, I have found the bottleneck is produced returning user's groups (via contact.getContactGroups()).
Returning name, surnames, emails, addresses and phones works OK but If I include user's groups too, then timeout exception appears...
UPDATE 2
Here it is my working solution, hope it helps :)
https://github.com/antacerod/google-app-script-6-minutes-limitation