Safe appendRow
onFormSubmit waits for lock, appendRow to Log, increments Script Properties ticket counter, then releases lock in finally.
LockService gives your script a mutex so only one execution holds a lock at a time. In busy spreadsheets, two triggers firing together can both read the same last row and write duplicates. LockService.waitLock and releaseLock serialize critical sections like appendRow, counter increments, or quota-sensitive API calls.
Use LockService when parallel runs are possible: dual form submits, overlapping time triggers, onEdit plus manual menu running together, or any shared counter and log append pattern.
Skip locks for read-only work or single-threaded manual runs. Over-locking everything slows the script and can cause wait timeouts if one run hangs.
const lock = LockService.getScriptLock() for project-wide coordination.
lock.waitLock(30000) waits up to 30 seconds to acquire the lock.
Wrap the critical section in try/finally and call lock.releaseLock() in finally.
{:"Keep locked sections short"=>"read, compute, write, then release."}
Document lock and user lock exist for narrower scopes when appropriate.
Log when waitLock times out so you know contention is too high.
onFormSubmit waits for lock, appendRow to Log, increments Script Properties ticket counter, then releases lock in finally.
Two sales triggers read max invoice id, increment, and write back. Lock prevents duplicate invoice numbers.
Only one execution at a time calls a strict external API, avoiding parallel UrlFetchApp bursts.
Build this without starting from a blank cell
Use a Better Sheets tool for LockService, then watch a walkthrough when you want the full pattern.
Watch how LockService works
Triggers tell Apps Script when to run a function: on edit, on open, on a schedule, or when a form is submitted. Simple triggers use reserved names like onEdit. Installable triggers are created in the Apps Script Triggers page and can do more, including running as a specific user or on a timer.
Read guide →Automation means work happens without repeating the same clicks every day: imports update, emails send, rows move, and dashboards refresh. In Sheets, automation usually stacks built-in features, Apps Script, and sometimes the Sheets API or add-ons. Start with the lightest tool that still solves the job.
Read guide →A workflow is the path work takes through your sheet: intake, review, approval, done. Good workflows use consistent columns, statuses everyone understands, and just enough automation to move tasks forward without hiding steps from the team.
Read guide →MailApp is an Apps Script service that sends email from your Google account without opening Gmail. You call MailApp.sendEmail with a recipient, subject, and body, often using cell values as the content. It is the fastest way to notify someone when a row is added or a deadline hits in a tracker.
Read guide →Done reading about LockService?
Membership unlocks 636+ tutorials, unlimited generators, and every template. Practical lessons. Zero fluff.
Need this once
Jump to a free tool or a single tutorial for this topic.
Learning Sheets for real
Unlock the full library, generators, and templates with membership.