Google Sheets AutomationDifficulty: Intermediate⚡ onEdit(e) trigger

Auto-Move Completed Rows from Active Sheet to Archive Tab

When a task or lead status is marked "Done" or "Closed", this script cuts the entire row from the active working tab and appends it to an "Archive" tab.

Required OAuth Scopes: https://www.googleapis.com/auth/spreadsheets
Code.gs (Apps Script V8 Runtime)
function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  
  if (sheet.getName() === 'Active' && range.getColumn() === 4 && e.value === 'Done') {
    const row = range.getRow();
    const numColumns = sheet.getLastColumn();
    const rowData = sheet.getRange(row, 1, 1, numColumns).getValues();
    
    const archiveSheet = e.source.getSheetByName('Archive');
    archiveSheet.appendRow(rowData[0]);
    sheet.deleteRow(row);
  }
}

Line-by-Line Code Explanation

Line 5: Checks if the edit occurred on "Active" sheet, in Column 4 (Status), and value equals "Done".
Line 6-8: Retrieves the entire row data array across all active columns.
Line 10-12: Appends the data row to the "Archive" tab and deletes the source row from the "Active" tab.

Step-by-Step Setup & Deployment

  1. Set up two tabs: "Active" and "Archive" with identical column headers.
  2. Ensure Column D is your Status column with a dropdown including "Done".
  3. Paste the code into Apps Script and save.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More Google Sheets Automation Snippets