Why per-cell loops break on large sheets
The first time I watched this bite someone, they had 800 rows and a script that called setBackground inside a nested loop. It took 40 seconds and eventually hit Apps Script's 6-minute execution limit. The problem is that every Sheets API call, no matter how small, crosses a process boundary — Apps Script runs in Google's V8 sandbox and each call to the spreadsheet service is an HTTP round-trip under the hood. Eight hundred calls is 800 round-trips.
The fix is to make exactly two service calls: one getValues to pull everything into a plain JavaScript array, and one setBackgrounds to push all the colors back. Everything between those two calls is local JavaScript — a frequency map built with a for-of loop, then a map() pass to assign colors. On a 1000-row column, the whole function runs in under 2 seconds.