# 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
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