1

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

Antonio Acevedo
  • 1,480
  • 3
  • 21
  • 39

1 Answers1

2

Finally I have found another SO post.

It shows a nice workaround related to checkbox setValue method implementation and its second parameter (Sets whether the CheckBox should be checked and optionally fires an event if the value changes as a result of this call)

In my case, it works like a charm!

Access the workaround

Community
  • 1
  • 1
Antonio Acevedo
  • 1,480
  • 3
  • 21
  • 39
  • Thanks for using it :-) glad it helped. Wouldn'it but a good idea to show your implementation of this workaround ? Hope you'll take some time to do so! – Serge insas Jun 15 '14 at 14:22
  • @serge-insas I have included a link to our new company github repository and we will write a post as soon as possible. – Antonio Acevedo Jun 20 '14 at 11:37
  • I saw your github, very nice work indeed ;) I love the bi-lingual Ui using 'case'... never think of that myself. – Serge insas Jun 20 '14 at 15:14