0

When I put only the following Google Apps Script in (by itself), it works properly...

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");             // SHEET NAME
var range = sheet.getRange("A2:C");                  // RANGE TO SORT
 
function onEdit(e)  {
 range.sort([{column: 3, ascending: true}]);        // COLUMN NUMBER TO SORT
}

Similarly, when I put only the following Google Apps Script in (by itself), it works properly...

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Sheet1" ) { //Checks to see if we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 3 ) { //Checks the column
      var nextCell = r.offset(0, 1);
      if( nextCell.getValue() === '' ) //Checks to see if the adjacent cell is empty or not?
        nextCell.setValue(new Date());
    }
  }
}

But when I put them both in, the first Google Apps Script works properly, but the second Google Apps Script fails to work.

What am I doing wrong?

First, I added the following after I posted my initial question based on feedback I received below

Based on best practices, I am guessing, perhaps, the combined apps script should look something like this...

function onEdit(e) {
  if (!e)
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Sheet1");             // SHEET NAME
    var range = sheet.getRange("A2:C");                  // RANGE TO SORT
     
    function onEdit(e)  {
     range.sort([{column: 3, ascending: true}]);        // COLUMN NUMBER TO SORT
  }
    if (var s = SpreadsheetApp.getActiveSheet();
      if( s.getName() == "Sheet1" ) { //Checks to see if we're on Sheet1 or not
        var r = s.getActiveCell();
        if( r.getColumn() == 3 ) { //Checks the column
          var nextCell = r.offset(0, 1);
          if( nextCell.getValue() === '' ) //Checks to see if the adjacent cell is empty or not?
            nextCell.setValue(new Date());
        }
      }
}

Second, I added the following after I posted my initial question based on feedback I received below

Based on best practices, I am guessing, perhaps, the combined apps script should look something like this...

Twilight
  • 1,399
  • 2
  • 11
Ristelan
  • 3
  • 4

1 Answers1

0

The best practice is to only have one global function by each name.

If you have multiple copies of a function like onEdit() in the same script project, only one of them will be active. It does not make a difference whether their argument lists look the same.

See Which function runs and these onEdit(e) best practices.

doubleunary
  • 13,842
  • 3
  • 18
  • 51
  • Every function in a project must have a unique name – Cooper Sep 30 '22 at 15:37
  • Agreed, it is definitely a best practice to not have two global functions by the same name. Unfortunately, repeated function names are valid JavaScript. I do not think you can catch that even with `'use strict';`. – doubleunary Sep 30 '22 at 15:49
  • Please see my feeble attempt at following "best practices" in the code I just appended to my original question. – Ristelan Sep 30 '22 at 16:36
  • Not quite there. See the early part of the [best practices](https://webapps.stackexchange.com/a/155429/269219) post again. Also see [Merging or Combining two onEdit trigger functions](https://stackoverflow.com/a/62603739/13045193). – doubleunary Sep 30 '22 at 16:39
  • I saw function onEdit(e) { doFirstThing_(e); doSecondThing_(e); } but I also read, "You should however consider spreadsheet performance when doing this." therefore I tried the more difficult approach. – Ristelan Sep 30 '22 at 16:49