Skip to content

Diagnostic reference

Spreadsheet service call inside a loop

Why calling getValue() for every row makes an Apps Script loop slow, and how one getValues() read before the loop reduces the risk of execution timeouts.

Updated August 30, 2026

What myFunction reports

Read the range once before the loop instead of calling getValue() for every row.

What it means

Calls from Apps Script to Google Workspace services are much slower than reading an in-memory array. The original loop makes one remote getValue call per row. A rule-based quick fix can read the whole range once with getValues and work from the returned values.

Before and after

The code before and after the fix.

Problem 6 changed lines
function readStatuses() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const statuses = [];

  for (let row = 2; row <= sheet.getLastRow(); row++) {
    statuses.push(sheet.getRange(row, 3).getValue());
  }

  return statuses;
}
Fixed 6 changed lines
function readStatuses() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const lastRow = sheet.getLastRow();
  if (lastRow < 2) return [];

  return sheet
    .getRange(2, 3, lastRow - 1, 1)
    .getValues()
    .flat();
}

What myFunction does

  • Moves the Spreadsheet range read out of the per-row loop.
  • Uses one getValues call for the complete status column.
  • Preserves the empty-sheet case instead of requesting a zero-height range.

Check before you apply

  • Review the row and column bounds of the range before applying the fix.
  • myFunction offers a quick fix only for loops it can batch without changing what the loop does.
  • The fix changes editor text only. myFunction does not run the function.