r/GoogleAppsScript • u/lutzy89 • May 20 '26
Question Can a file iterator be dealt with asynchonously
TL:DR
Is there a faster way to run a function on a file iterator that will time out repeatedly before reaching the end? eg...
let 1000Files = DriveApp.searchFiles("title contains '" + sharedString + "'")
while (1000Files.hasNext()){
someSpreadsheetFunction(1000Files.next())
}I have a heap of spreadsheets that are typically viewed anonymously or by someone with no permissions to run the script. if i had foresight years ago, there would be a weekly "check for updates" trigger built in to each.. but here we are.
the individual spreadsheet function takes anywhere from 20 seconds to 20+ minutes depending on the spreadsheet (luckily, the workspace account still has the old 30-minute timeout restriction), so the iterator is guaranteed to need to use the continuation token and the function will need to restart itself with a self perpetuating trigger, i know how to do that, and have been for a few years but it still takes hours or days to finish.
Is there some way to run this asynchronously?
My next plan is to have 12 seperate triggers all try to read from the same scriptProperties continuation token but only at their assigned "5 second window" in the minute to prevent a race condition. It should work as i've done it before with 3 running at once using a spreadsheet for tracking, but it seems like such a janky solution to a problem.
Alternatively is it possible to add a time based trigger to these spreadsheets as it would prevent the entire need for my question.
2
u/WicketTheQuerent May 21 '26
FileIterador should not be used asynchronously. If you use multiple time-driven triggers, you should use each to handle a different file set, e.g., by using different file search queries.
It's worth noting that, in terms of script performance, the Advanced Drive Service is better than the Drive Service.
1
u/lutzy89 May 23 '26 edited May 24 '26
For anyone coming along in the future, the script lock service prevents issues with multiple threads using the same iterator token stored in the script Properties... otherwise this seems to work great? run as many threads as you want