`onEdit` is a special function within [[Google Apps Script]] for [[Google Sheets]] that fires after any edit to the spreadsheet.
Use the `onEdit` function to trigger functionality when the user edits specific ranges.
The `onEdit` function (there can only be one) should quickly and efficiently determine if the edited range should trigger an event by checking the edited range location and/or value. If an event should not be triggered, return.
If an event should be triggered, call a function to handle the desired behavior. This helps keep the `onEdit` function clean when multiple events can be triggered. I prefer to use named ranges as triggers (you can more easily re-design the sheet without editing the code and the named range is a hint that it is important for something).
```javascript
const SWITCH = "Switch"; // "A1"
function onEdit(e) {
let editRange = e.range;
let ss = SpreadsheetApp.getActiveSpreadsheet();
if (editRange.getA1Notation() == ss.getRange(SWITCH).getA1Notation()){
fooBar(e);
}
function fooBar(value) {
// your code here
}
```