Google Sheets multiple scripts

# Google Sheets Multiple Scripts: Resolving Conflicting `onEdit` Triggers

## Summary
- Three separate functions named `onEdit` were implemented to handle spreadsheet edits
- Conflicting trigger definitions caused only the final `onEdit` implementation to execute
- Attempts to rename functions broke trigger registration while maintaining name collisions
- Resolution required merging all logic into a single `onEdit` function handler

## Root Cause
- Google Apps Script only allows **one function per trigger type**
- All implementations used the reserved `onEdit` name for simple edit triggers
- Script executions were **mutually exclusive** due to namespace conflicts
- Subsequent `onEdit` declarations overwrote previous implementations

## Why This Happens in Real Systems
- Copy-pasting solutions without analyzing existing trigger handlers
- Modularization attempts that violate platform constraints
- Lack of awareness about reserved function names in trigger-based systems
- Working with legacy code where multiple contributors added separate triggers
- Assumption that renamed handlers automatically register as triggers

## Real-World Impact
- Partial functionality where only last-defined trigger executes
- Critical features like timestamps or row deletions failing silently
- Requires manual intervention to restore functionality
- Degrades user trust due to inconsistent behavior
- Potential data loss (e.g., canceled rows not deleted)

## Example or Code
Merged implementation:
```javascript
function onEdit(e) {
  const sheet = e.range.getSheet();
  const sheetName = 'CUSTOM CUTS';

  // Exit if not target sheet
  if (sheet.getName() !== sheetName) return;

  // Feature 1: Timestamp on dropdown selection
  if (e.range.columnStart === 5) {
    // ... (timestamp logic from first function)
  }

  // Feature 2: Delete rows on specific values
  if (e.range.columnStart === 5 && ['COMPLETE','CANCEL'].includes(e.value)) {
    // ... (row deletion logic from second function)
  }

  // Feature 3: Sort table on edits
  if (true) {  // Remove condition to always sort
    // ... (sorting logic from third function)
  }
}

How Senior Engineers Fix It

  • Consolidate all logic into a single trigger handler function
  • Implement feature gates using conditional blocks
  • Use comments and section headers within the unified handler
  • Validate trigger registeration via View > Current project triggers
  • Add guard clauses to prevent early-exit pitfalls:
    if (sheet.getName() !== 'CUSTOM CUTS') return;
  • Implement logging to monitor execution flow:
    console.log(`Triggered by edit at ${e.range.getA1Notation()}`);

Why Juniors Miss It

  • Mistaken belief that function renaming resolves trigger conflicts
  • Unfamiliarity with Google Apps Script’s single-handler constraint
  • Testing functions individually masks coexistence issues
  • Assuming script containment rules follow standard JavaScript
  • Overlooking execution order dependencies
  • Difficulty tracing implicit platform behaviors