r/GoogleAppsScript • u/DarkArctic88 • May 15 '26
Question Where did I go wrong? The two "onEdit" problem.
Ok so im painfully new to javascript and im trying to get two automations going... on the same sheet of course,
The first task sorts the workbook into corresponding sheets and deletes the row as it moves. That one runs fine with the current code.
Im also trying to sort those sheets by multiple columns after the row is moved. Every sheet except one ... The only way ive been able to think to do that is to create a separate task for each of the sheets I need to sort and omit the one I do not.
Of course that ran me smack into the dreaded "multiple onEdit" problem. I was told you can get around it by bundling the tasks into one Onedit "main" and it should run subsequently.
Thing is, it runs the first task, then wont run any of the subsequent. Says "secondtask is undefined" during execution errors.
Ive seen others running with this same work around on youtube so im really not sure why mine doesnt work. I'm sure its a simple fix maybe but I cant seem to find an answer as to why the first task runs but not the second.
I ran it through a javascript validation and it says "parsing error unexpected token const" at line 29 which is
const sheetName = "Colony Maintenance";
Here's the full code, I put some notation in to make it easier to understand what i was trying to do.
Any help would be greatly appreciated!
function onEdit(e) {
firstTask(e);
secondTask(e);
thirdTask(e);
forthTask(e);
fifthTask(e);
sixthTask(e);
}
function firstTask(e) {
let range = e.range;
let col = range.getColumn();
let row = range.getRow();
let val = range.getValue();
let source = e.source.getActiveSheet();
if (col == 1 && val !=''){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName (source.getName());
let targetSheet = ss.getSheetByName(val);
let data = sheet.getRange(row, 1,1, sheet.getLastColumn()).getValues();
targetSheet.appendRow(data [0]);
sheet.deleteRow(row);
}
//this is where the code stops working
function secondTask(e) {
//Change to the sheet you want to sort's name
const sheetName = "Colony Maintenance";
const sheet = e.source.getSheetByName(sheetName);
// Only proceed if the edit happened on the target sheet
if (e.source.getActiveSheet().getName() !== sheetName) return;
// Define the range to sort (starting from row 2 to skip headers)
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
// Sort by multiple columns:
// Priority 1: Column 1 (Ascending= a-z= true)
// Priority 2: Column 3 (Descending= z-a =false)
range.sort([
{column: 11, ascending: true},
{column: 3, ascending: true},
{column: 7, ascending: true},
{column: 6, ascending: true},
{column: 8, ascending: true},
]);
}
function thirdTask(e) {
const sheetName = "Veterinary Care";
const sheet = e.source.getSheetByName(sheetName);
if (e.source.getActiveSheet().getName() !== sheetName) return;
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort([
{column: 11, ascending: true},
{column: 3, ascending: true},
{column: 7, ascending: true},
{column: 6, ascending: true},
{column: 8, ascending: true},
]);
}
function forthTask(e) {
const sheetName = "Sales";
const sheet = e.source.getSheetByName(sheetName);
if (e.source.getActiveSheet().getName() !== sheetName) return;
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort([
{column: 11, ascending: true},
{column: 3, ascending: true},
{column: 7, ascending: true},
{column: 6, ascending: true},
{column: 8, ascending: true},
]);
}
function fifthTask(e) {
const sheetName = "Breeding";
const sheet = e.source.getSheetByName(sheetName);
if (e.source.getActiveSheet().getName() !== sheetName) return;
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort([
{column: 11, ascending: true},
{column: 3, ascending: true},
{column: 7, ascending: true},
{column: 6, ascending: true},
{column: 8, ascending: true},
]);
}
function sixthTask(e) {
const sheetName = "Home Life";
const sheet = e.source.getSheetByName(sheetName);
if (e.source.getActiveSheet().getName() !== sheetName) return;
const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort([
{column: 11, ascending: true},
{column: 3, ascending: true},
{column: 7, ascending: true},
{column: 6, ascending: true},
{column: 8, ascending: true},
]);
}
}
View all comments
Show parent comment
1
u/DarkArctic88 May 15 '26
Yep, the changing of a cell in the first column of the first sheet is what triggers the function. I do understand that its normal for it to bring up that error if, after saving the edits to the code, you were to then try to run it before actually triggering the onedit. No change to the cell= no associated event- because no event happened.
So I do change at least one cell after every code change to test it. I know the trigger works because it preforms the first task. I perform the trigger and it performs the first function, but it only performs the first function, never the second or any further. So that's what I'm trying to figure out.
Is there another way you are suggesting this should be handled? I'm completely amenable to another way. I already feel like I might be making this more complicated than necessary.
All im really trying to do is automatically sort the rows by multiple columns, after the on edit first task sorts them into the right tabs (sheets).
Id like it to perform like this, data get entered into first sheet, the onedit function sorts the data into the proper tab in the workbook, and then the tabs (sheets) themselves have all their rows sorted automatically by multiple columns.
If there's like a formula we could put into a cell that can auto update the sorting and will account for new data coming in so I wouldn't need to constantly update the formula for the change in row number as it updates, I would find that solution quite elegant. I just don't know of one that can do that automatically.