"Exceeded maximum execution time": make a slow script fast
Apps Script times out when a loop calls getValue and setValue per row. Read once with getValues, write once with setValues: 20,000 rows in 1.55 s.
On this page
sheet.getRange(row, 5).setValue(qty * price);Before one cell at a time, Timed Out
| A | C | D | E | |
|---|---|---|---|---|
| 1 | Customer | Qty | Price | Total |
| 100 | Lauren | 1 | 7.25 | 7.25 |
| 101 | Brian | 8 | 4.99 | 39.92 |
| 102 | Emily | 6 | 19.99 |
What changed
- One
getValuesread - One
setValueswrite
After one read, one write
| A | C | D | E | |
|---|---|---|---|---|
| 1 | Customer | Qty | Price | Total |
| 100 | Lauren | 1 | 7.25 | 7.25 |
| 101 | Brian | 8 | 4.99 | 39.92 |
| 102 | Emily | 6 | 19.99 | 119.94 |
100. That is how many order totals the first script wrote before it was stopped, out of 20,000 rows. Column E of the Broken tab should hold Qty times Price on every row. The count in G2 says 100, the last total sits in E101, and the Executions page for the script shows the run as Timed Out. Expanded on that page, a timed-out run has one line in its log:
Exceeded maximum execution time
What got this script under the limit was taking the spreadsheet calls out of the loop. Read the whole input range once with getValues, do the math on the array in JavaScript, and write the whole result once with setValues. Here that is one read of C2:D20001 and one write to column E, which filled all 20,000 totals in 1.55 seconds. When your own script shows this message, search the loop body for spreadsheet calls: every getRange, getValue, setValue or setBackground inside a for runs once per row.
The same 20,000 rows went through three versions of the script, each on its own tab and each started by ticking J1. Version 1 makes three spreadsheet calls per row, version 2 makes one read up front and then one write per row, and version 3 makes one read and one write in total:
| Script | Tab | Totals written (G2) | Result |
|---|---|---|---|
| 1. getValue, getValue, setValue for every row | Broken (as you see it) | 100 | Timed Out after 30.286 s |
| 2. One getValues, then setValue for every row | Read once, write per row | 4,586 | Timed Out after 30.234 s |
| 3. One getValues, one setValues | Fixed | 20,000 | Completed, 1.55 s logged in H2 |
The two that timed out did not fail on a bad value. Every total they managed to write is correct, and the rows they finished stayed in column E after the cutoff.
getValue and setValue inside a loop make 60,000 calls, and the run times out
This is the loop on the Broken tab. It follows the job exactly as you would describe it out loud: for each row, get the quantity, get the price, write the total.
function totalsOneCellAtATime(sheet) {
const start = Date.now();
const last = sheet.getLastRow();
for (let row = 2; row <= last; row++) {
const qty = sheet.getRange(row, 3).getValue();
const price = sheet.getRange(row, 4).getValue();
sheet.getRange(row, 5).setValue(qty * price);
}
sheet.getRange('H2').setValue((Date.now() - start) / 1000);
}Each pass makes three calls to the spreadsheet. Over 20,000 rows that comes to 60,000 calls before the script can finish. Google's Apps Script best practices page names the problem directly: "Alternating read and write commands is slow." The same page says Apps Script has look-ahead and write caching built in, and that the way to get the most from them is to make fewer reads and writes. Inside this loop, two getValue reads and one setValue write alternate on every row.

To find the message for your own runs, open the script from Extensions, then Apps Script, and pick Executions, the list icon in the left rail. Each run gets a row with its type, start time, duration and status, and clicking a Timed Out row opens its Cloud logs line. Google's page on bound scripts gives the same route: open the spreadsheet and click Extensions > Apps Script.
The last line of the function writes the elapsed time into H2. On the Broken tab, H2 is empty. That empty cell is a quick way to tell a script that was cut off from one that ended early on purpose: the code after the loop never ran.


The checkbox on the Broken tab still runs version 1. Ticking it in your copy starts the slow loop again, and it stops at about 30 seconds with the same message on the Executions page. Totals it wrote before the cutoff stay in column E.
One getValues read and one setValues write finish 20,000 rows in 1.55 seconds
Version 3, on the Fixed tab, leaves no spreadsheet call inside a loop. It reads everything once, does the multiplication in JavaScript, and hands the whole column back in one call:
function totalsBatch(sheet) {
const start = Date.now();
const last = sheet.getLastRow();
const rows = sheet.getRange(2, 3, last - 1, 2).getValues();
const totals = rows.map(([qty, price]) => [qty * price]);
sheet.getRange(2, 5, totals.length, 1).setValues(totals);
sheet.getRange('H2').setValue((Date.now() - start) / 1000);
}getValues() returns an array of rows, and each row is an array of cells, here [qty, price]. The map line turns that into an array of one-cell rows, [[total], [total], ...], which is the shape setValues needs for a range one column wide. The target range takes its height from totals.length, so it always matches the data that was read.
To point the function at your own columns, change the numbers in the two getRange calls. getRange(2, 3, last - 1, 2) starts at row 2, column 3, which is C. Its third number, last - 1, is how many rows to read, one fewer than the last row because the data starts on row 2. The final 2 covers two columns, Qty and Price, which works because they sit side by side in C and D. getRange(2, 5, totals.length, 1) starts at row 2 in column 5, which is E, and is 1 column wide.
On the Fixed tab, this version wrote all 20,000 totals and logged 1.55 seconds in H2. The Executions page listed the whole run, trigger included, at 2.401 seconds. G2 on that tab counts 20,000 totals, and K2, the sum of column E, is 969,707.34. In two throwaway copies made for testing, the same script logged 1.839 and 1.712 seconds.

| Input | 20,000 rows, Qty in C2:C20001, Price in D2:D20001, Fixed!E2:E6 read back |
|---|---|
| Formula | totalsBatch(sheet): getValues on C2:D20001, setValues on E2:E20001 |
| Expected | 159.92, 75, 15, 14.5, 44.91 |
| Sheets returned | 159.92, 75, 15, 14.5, 44.91 Matches expected |
| Checked | 2026-11-11, free personal account |
In version 3 the multiplication happens in the map line, between the one read and the one write, and that line makes no spreadsheet call at all. Google's best practices page puts it as "Using JavaScript operations within your script is faster than calling other services."
Arithmetic on two columns, like these totals, is also the kind of job one formula can do for a whole column with no script. That route is in ARRAYFORMULA down a whole column.
Moving only the reads out of the loop still timed out
A smaller change is to pull only the reads out of the loop. The fourth tab runs that version, number 2 in the table. It reads Qty and Price for every row in one call, then still writes the totals one at a time:
const rows = sheet.getRange(2, 3, last - 1, 2).getValues();
for (let i = 0; i < rows.length; i++) {
sheet.getRange(i + 2, 5).setValue(rows[i][0] * rows[i][1]);
}That got a lot further than version 1. G2 on the Read once, write per row tab shows 4,586, so the last total landed in E4587. The run still ended Timed Out, at 30.234 seconds. With the reads gone from the loop, 20,000 separate setValue calls were still more than the time allowed.
The two cut-off runs ended less than a tenth of a second apart, so the gap between 100 and 4,586 rows comes from what each pass of the loop asked of the spreadsheet. In version 1 a pass waited on two reads and a write. In version 2 it waited on one write.

Where the limit comes from: 30 seconds for simple triggers, 6 minutes from the Run button
Google's quotas page lists script runtime as 6 min / execution, the same for free and Workspace accounts. That is the limit a slow script started with Run in the editor runs into.
The sheet for this page starts every version from a simple trigger instead. Ticking a checkbox counts as an edit, and this function decides which version to run:
function onEdit(e) {
const range = e.range;
if (range.getA1Notation() !== 'J1' || range.getValue() !== true) return;
const sheet = range.getSheet();
const name = sheet.getName();
if (name === 'Broken (as you see it)') totalsOneCellAtATime(sheet);
if (name === 'Read once, write per row') totalsReadOnce(sheet);
if (name === 'Fixed' || name === 'Try it yourself') totalsBatch(sheet);
}Google's triggers page says simple triggers "can't run for longer than 30 seconds" and "can't access services that require authorization." The second rule is why the sheet was built this way. A copy can run all three versions without a permission screen, and the slow ones reach the message in half a minute instead of six. At the pace version 1 managed here, 100 rows in about 30 seconds, six minutes would still leave most of the 20,000 rows without a total.
Running the batch version on the Try it yourself tab
Make a copy with the button at the end of this page. The bound script, Order totals, comes along with it. The Try it yourself tab starts in the same state as Broken, with totals in E2:E101 and nothing below them.
- Type
=SUM(E2:E20001)inK2. It adds up the 100 totals that are already there. - Tick the box in
J1. That runs version 3 on this tab. In a fresh test copy the new totals appeared between 4 and 8 seconds after the tick, andK2changed with them. - Type
=K2-Fixed!K2inK3. It returns 0 when your totals match the Fixed tab.
For a spot check at the far end, E19997:E20001 on the Fixed tab holds 99.95, 37.5, 3.75, 58 and 29.94, and the same cells on your Try it yourself tab should show those values after the tick.
Take-home copy. Four tabs: Broken (as you see it), Fixed, Try it yourself, Read once, write per row. The bound script (Order totals) is copied with the sheet and opens from Extensions, then Apps Script.