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 avoids 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 performs one remote getValue call for every row. A supported quick fix can read the whole range once with getValues and work from the returned values instead.

Before and after

See the change before you make it.

Problem
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;
}
Safer version
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 the assist changes

  • 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.

What stays with you

  • Review the selected row and column boundaries before applying the fix.
  • Supported quick fixes appear only when myFunction can preserve the loop’s observable behavior.
  • myFunction changes editor text only; it does not run benchmarks or execute the function.