1
votes

I have an array of strings where each value represents a Gmail Threadid. This looks something like this:

var threadArray = [threadId1, threadId2, threadId3, threadId4, threadId5];

I want to apply a Gmail Label to each of the elements in my array of threadids, but my current approach is not efficient and takes too long to execute. See below:

for (var i = 0; i < threadArray.length; i++)
{
    thread = getThreadById(threadArray[i]) // this statement takes too long (several seconds) to execute!!
    thread.addLabel(exampleLabel);
}

What's the best approach for applying a Gmail Label to each Gmail Thread in an array of Gmail ThreadIds?

Gmail provides functions like getInboxThreads() which retrieve many threads at once to return GmailThread[] but its unclear to me how I can construct my own GmailThread[] purely based on the threadids, not my entire inbox.

Has anyone else experienced this issue or found a workaround? I found a similar open issue here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2598

1

1 Answers

1
votes

Taking a long time for such task is not really an issue, you can easily manage to get it working on (relatively) small bunches based on a timer trigger.

I have a similar script that runs every night and it works nicely... it works for 4 minutes then wait a bit and continues until all the threads have been treated.

You only need to store the index where to start from (in scriptProperties) and measure elapsed time during script execution.