Google Apps Script Iterating over Spreadsheets very slow

I agree with Serge's comment that this code is already well optimized, opening that many spreadsheets is going to take some time.

I see one opportunity to improve but it will probably have a very minimal impact on speed if any. You could move the ss.getName() call out of the inner loop, instead assign it to a variable right after you open the spreadsheet, then reference that variable in the inner most loop.

Note that in my experience the speed of the Google service calls tends to vary pretty widely, so sometimes this may run faster or slower. You can see how long each call is taking by looking in the Execution Transcript of the script editor, under the View menu.


First of all, review the information on this page.

Three things I've experienced that can really slow you down are:

  1. Calling external service multiple times
  2. Calling SpreadsheetApp.flush() in a loop to force updating the sheet.
  3. Calling Range.getValue() a lot.

For the first part, try batching your calls. For instance if you are requesting exchange rates for your local currency and foreign ones, try sending a bunch of queries in one request.

For the second part, just don't call this method unless its really necessary.

for the third part, try modifying your code so you can call this method once and move around with its result instead of calling it in multiple methods for the same result.

Hint: To avoid modifying many methods parameters, just pass a single object at the beginning of your method-tree call and fill it with your parameters. This way you don't have to modify each method you pass by to add\remove a parameter. That's a concept that's applied to all programming languages with functions.