What the error actually means
"Service Spreadsheets failed while accessing document" is the Spreadsheet service reporting that it could not complete an operation on the document — not that your code is syntactically wrong, and not that you lack permission. Google's own infrastructure either hit a transient fault on their end or your script asked for more work than the service is willing to do in one round-trip.
The most common trigger is a single setValues call covering a very large range: 20,000 cells in one go is usually fine; 200,000 in one call is asking for trouble, especially on sheets with conditional formatting, data validation, or named ranges that force recalculation. The second trigger is pure transience — the same call succeeds if you run it again thirty seconds later, which is the tell.