Home > Net >  Formulas make SpreadsheetApp work slower in Google Sheets Script
Formulas make SpreadsheetApp work slower in Google Sheets Script

Time:08-31

This problem appeared recently ~August 25, 2022.

My onEdit trigger relies on a sheet name and the script runs too long to get it. This is the smallest script to reproduce an error in my enviroment:

function onEdit(e) {
  var t = new Date();
  console.log('we are in onEdit!');
  var sheetName = e.range.getSheet().getName();
  console.log(sheetName);
  console.log('We got sheet name. Time past = '   (new Date() - t));
}

The function range.getName() works too slow, and the script is timed out.

I did not see this behavior any time before. Other sample: SpreadsheetApp. In my other file this line takes 30 sec.:

SpreadsheetApp.openById(id);

Reproduce the error

Here's the file with minimal code needed to reproduce the error:

onEdit Crash Test ➡️ e.range.getSheet().getName()

Please make a copy.

To Reproduce the onEdit Error

  1. Print 1 in the first cell
  2. Immediately press enter and Print 2
  3. Repeat it a few times
  4. Go to the script editor and see execution time

Original

It has a few formulas and the script works fast.

My goal is to understand why formulas cause sheet.getName() method to slow down.

Error

Exceeded maximum execution time

The max. time for onEdit is 30 sec. Tests showed that sheet.getName() worked too long. I cannot see the edited sheet name without the getName() function as it is inside a trigger.

My Fix

Even Lock and Cache did not resolve this:

Injection

  /**            
  • Related