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},
]);
}
}