Exceeded maximum execution time Exception in Google Apps Script

Hey, friends today I will teach you Exceeded maximum execution time Exception in Google Apps Script. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Randomize the Order of Rows in Google Sheets. Learn Exceeded maximum execution time Exception in Google Apps Script

Google Apps Script is a serverless environment that makes it simple to work with Gmail, Google Drive, and other Google Workspace platform services.

When you run any code inside the Google Apps Script IDE, it creates a new server with all of the necessary environments to run your application. This server has a hard timeout limit, and the App Script environment will terminate the function’s execution if it exceeds the maximum execution time.

You might also like our trending code snippets

Exceeded maximum execution time

The maximum execution time varies depending on the type of Google Account you have. If you run your Apps Script code within a Gmail account, your functions will be terminated after 6 minutes. Because you pay Google a monthly fee per user for Google Workspace accounts, the timeout limit is more generous at 30 minutes.

If your Apps Script function / trigger exceeds the maximum timeout limit, the script will throw an exception like Exceeded maximum execution time or equivalent based on your script’s locale.

Exceeded maximum execution time
Se ha superado el tiempo máximo de ejecución.
Timpul maxim de executare a fost depășit
تجاوز الحد الأقصى لعدد مرات التنفيذ
Vượt quá thời gian thực thi tối đa
Durée d'exécution autorisée dépassée
Przekroczono maksymalny czas wykonywania
Limite massimo del tempo di esecuzione superato
เวลาประมวลผลเกินขีดจำกัดสูงสุด
Melebihi jumlah eksekusi maksimum
Превышено максимально допустимое время выполнения
Lumagpas sa maximum na oras ng execution

Avoid Maximum Execution Time Limit

You can include a simple time check in your Apps Script function, indicating that it is likely to take more than a few minutes to execute, and gracefully pause the request if it is seen to be exceeding the time limit.

For instance, the Download Gmail add-on saves email messages from Gmail to Google Drive as PDF files. It grabs a bunch of messages from the Inbox, converts them to PDF and runs in a loop. If the execution is taking longer, it breaks from the loop automatically.

const GMAIL_USER = /(gmail|googlemail)/.test(
  Session.getActiveUser().getEmail()
);
const ONE_SECOND = 1000;
const ONE_MINUTE = ONE_SECOND * 60;
const MAX_EXECUTION_TIME = ONE_MINUTE * (GMAIL_USER ? 6 : 30);
const NOW = Date.now();

const isTimeLeft = () => {
  return MAX_EXECUTION_TIME > Date.now() - NOW;
};

const thisFunctionTakesTimeToExecution = () => {
  const threads = GmailApp.getInboxThreads(0, 100);
  for (let t = 0; t < threads.length && isTimeLeft(); t += 1) {
    // Save email to Google Drive
    Logger.log("Saving email...");
  }
};

Leave a Comment