u/DarkArctic88

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},
  ]);
  }
}
reddit.com
u/DarkArctic88 — 7 days ago

Autopopulating third column that combines two text columns

I am trying to get tasks sorted by importance/urgency (the Eisenhower method).

Here's what I'm trying to get done.

Id like to create a formula that will autopopulate column F.

Column D options are highly, somewhat, not important, and E options are highly, somewhat, and not urgent.

That leaves 9 possible options for column F:

highly important, highly urgent,

Somewhat important, highly urgent

Not important, highly urgent

Highly important, somewhat urgent

Somewhat important, somewhat urgent..

You get the idea.

The formula reads column D and E and combines them to populate F automatically.

I figure this might be in the line of a "if then" type thing but I haven't figured how to combine them to output to one cell.

u/DarkArctic88 — 10 days ago