routeEdit(e, routes) is a single onEdit dispatcher. Each route is {sheet, column, handler}. The first match runs, and the rest do not. Multi-cell edits (a paste, a fill) are skipped unless that route sets allowMulti: true.
When to use
- One spreadsheet with several
onEditbehaviors, each for a tab or column - Replacing two functions both named
onEdit— only one of those can exist - Stopping a column handler from running on a whole-range paste unless you mean it
How to use this snippet
column is 1-based (3) or a letter ('C'). Omit column to match any column on that sheet, or omit sheet to match that column on any sheet.
/**
* Dispatch an onEdit event to the first matching route.
* Routes are {sheet, column, handler, allowMulti}. column is a 1-based
* number or a letter like "C". Multi-cell edits are skipped unless
* allowMulti is true; a skipped first match does not fall through.
* Missing e.range is a no-op. A matching route with no function handler
* throws.
*
* @param {Object} e
* @param {Array<Object>} routes
*/
function routeEdit(e, routes) {
if (!e || !e.range) return;
var list = routes || [];
var range = e.range;
var sheetName = range.getSheet().getName();
var startCol = range.getColumn();
var endCol = startCol + range.getNumColumns() - 1;
var multi = range.getNumRows() > 1 || range.getNumColumns() > 1;
function colToNumber_(column) {
if (column == null || column === '') return null;
if (typeof column === 'number') {
if (column < 1 || Math.floor(column) !== column) {
throw new Error('routeEdit: column must be an integer >= 1 or a letter like "A"');
}
return column;
}
var s = String(column).trim();
if (/^\d+$/.test(s)) {
var n = Number(s);
if (n < 1) throw new Error('routeEdit: column must be an integer >= 1 or a letter like "A"');
return n;
}
if (!/^[A-Za-z]+$/.test(s)) {
throw new Error('routeEdit: column must be an integer >= 1 or a letter like "A"');
}
var letters = s.toUpperCase();
var col = 0;
for (var i = 0; i < letters.length; i++) {
col = col * 26 + (letters.charCodeAt(i) - 64);
}
return col;
}
for (var i = 0; i < list.length; i++) {
var r = list[i] || {};
if (r.sheet != null && String(r.sheet) !== '' && String(r.sheet) !== sheetName) continue;
var wantCol = colToNumber_(r.column);
if (wantCol != null && (wantCol < startCol || wantCol > endCol)) continue;
// First matching route wins. If this edit is multi-cell and the route
// does not allow that, stop without running a later route.
if (multi && !r.allowMulti) return;
if (typeof r.handler !== 'function') {
throw new Error('routeEdit: first matching route is missing a handler function');
}
r.handler(e);
return;
}
}
Example
function onEdit(e) {
routeEdit(e, [
{ sheet: 'Tasks', column: 'C', handler: onPriorityEdit },
{ sheet: 'Tasks', column: 'D', handler: onDueEdit, allowMulti: true }
]);
}
function onPriorityEdit(e) {
// one-cell edits in Tasks!C
}
function onDueEdit(e) {
// can handle a pasted block in Tasks!D
}
Notes
- Simple triggers must be named
onEditand stay top-level functions. Two files that both declarefunction onEditshare one global scope: the file that loads last wins, and the other handler never runs. This dispatcher is how you keep both behaviors. - Installable
onEdittriggers with different handler names can both fire. That is a different problem; this helper does not merge those. - Stop onEdit from chasing your own writes still applies inside each handler.
Happy Coding!
