The edge cases that break simple Google Apps Script automations
说明与用途
简短的 Apps Script 行移动脚本在多次快速编辑时常因竞争导致数据丢失。使用 LockService 并从底部向上扫描表格可可靠地移动所有 "Done" 行。
问题
Simple Google Apps Script row‑moving scripts often lose data or behave incorrectly in real sheets.
结果
The updated script reliably moves all completed rows, even when multiple edits happen in rapid succession, and avoids data loss.
工作流
- 1. Trigger onEdit(e).
- 2. Acquire LockService.getDocumentLock().
- 3. Rescan the entire sheet from bottom to top for rows where status is "Done".
- 4. Move each found row to the destination sheet using range.moveTo().
- 5. Release the lock.
证据
Set two rows to Done within a second and both triggers run at once. The first deletes its row, so the second one's row number now points at the wrong row.
Fix: take LockService.getDocumentLock(), then rescan the whole tab bottom‑up for Done rows instead of trusting e.range.getRow().
- 记录日期: 2026-11-01T06:29:24.179Z