Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Follow publication

You're unable to read via this Friend Link since it's expired. Learn more

Beat the Clock! Dodging the Maximum Script Runtime in Google Apps Script ⏲️

Dmitry Kostyuk
Geek Culture
Published in
12 min readAug 16, 2021

What’s the Threshold ?

Google Apps Script is an amazing language that can automate a lot of your work. However, working with GAS also means that you have to learn to live with its built-in limitations and quotas.

One such quota is the total script runtime. It’s limited to six minutes on free accounts and thirty minutes on corporate accounts. After the above threshold is reached, the execution is interrupted with the following error message:

Sometimes this is not enough. I have experienced that the time required to complete tasks like copying or even simply listing files on a drive or in a directory can be quite long. Merging hundreds or thousands of documents can also take longer than both thresholds.

Now let’s look into how we can build a solution.

The full source code is available on GitHub.

The Underlying Principle

The first thing you must do is stop script execution before the threshold runs out. Therefore, you need to have a means of timing your execution time and exiting gracefully.

You then need to create a trigger to restart the execution. This way, you automatically get another six or thirty minutes of runtime depending on your account type.

You also need to save your progress. If your script is, say, copying files, you don’t want to start copying the same files all over again. Rather, you want to pick up from where you left off. The three different ways to save our progress that I have found useful are in a spreadsheet, JSON file, or Firestore. JSON files are probably the easiest way to go.

After you trigger restarts execution, the app must be able to read the saved data and skip all steps that have been accomplished previously. Once the job is done, it needs to remove the trigger and the file.

The App We’ll Be Building

For the purpose of this article, we will build an app that will list all files in a given folder in Google Drive and create a spreadsheet with the result. We will be saving our progress in a JSON file. Without further ado, let’s get into it!

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Dmitry Kostyuk
Dmitry Kostyuk

Written by Dmitry Kostyuk

Google Developer Expert, founder at Wurkspaces.dev | Hire me: https://cutt.ly/p8BgQYE | Get Medium membership: https://cutt.ly/N8BggJs

Responses (5)

Write a response