Google Sheets AutomationDifficulty: Advanced⚡ onEdit(e) or Scheduled

Lock / Protect Rows in Google Sheets After Approval

When a row is marked "Approved" in Column E, this script automatically creates a protected range on that row so editors cannot accidentally tamper with approved numbers.

Required OAuth Scopes: https://www.googleapis.com/auth/spreadsheets
Code.gs (Apps Script V8 Runtime)
function protectApprovedRows(e) {
  const range = e.range;
  const sheet = range.getSheet();
  
  if (sheet.getName() === 'Expenses' && range.getColumn() === 5 && e.value === 'Approved') {
    const row = range.getRow();
    const rowRange = sheet.getRange(row, 1, 1, sheet.getLastColumn());
    const protection = rowRange.protect().setDescription('Locked Row ' + row);
    
    // Remove all editors except sheet owner
    const me = Session.getEffectiveUser();
    protection.removeEditors(protection.getEditors());
    protection.addEditor(me);
  }
}

Line-by-Line Code Explanation

Line 5: Evaluates whether edit was on "Expenses" sheet, Column 5, value "Approved".
Line 7-8: Defines range spanning the entire row and initializes protection.
Line 11-13: Strips write access from team members, leaving only the admin authorized.

Step-by-Step Setup & Deployment

  1. Ensure Column E has an "Approved" dropdown option.
  2. Install an installable onEdit trigger via Apps Script Triggers dashboard.

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