1

I'm still fairly new to Apps Script. I found this answer several months ago and it's been working great, but I've been asked to implement it on several sheets in a workbook instead of just the initial sheet.

My question is this: How can I get the following section to work correctly?

  try {
    var issueOpp = '5 - Issues/Opportunities!A3:F';
    var toDoList = '4 - To-do list!A3:F';
    var actRange = e.source.getRange(issueOpp || toDoList);
    var sortSpecs = [
      {
        range: actRange,
        column: 6, // column F = 6
        ascending: false, // true: ascending, false: descending
      },
    ];
  }

Everything sorts as expected when the edit occurs on the isueOpp sheet, but the toDoList sheet doesn't sort. The sheets are identical except for the name and the data in each column.

1 Answer 1

1
var actRange = e.source.getRange(issueOpp || toDoList);

That will not throw a syntax error, but it is semantically incorrect, because it will always get the range '4 - To-do list'!A3:F and never the range '5 - Issues/Opportunities'!A3:F. To understand why, see Logical OR (||).

To make it work, use autoSortRanges_() with these parameters:

    var sortSpecs = [
      {
        range: e.source.getRange('5 - Issues/Opportunities!A3:F'),
        column: 6,
        ascending: true,
      },
      {
        range: e.source.getRange('4 - To-do list!A3:F'),
        column: 6,
        ascending: true,
      },
    ];
2
  • This worked exactly as I'd hoped! While I understand what's happening here and why it works, could you provide any guidance on how my initial code is semantically wrong? Commented Dec 7, 2023 at 18:33
  • See Logical OR (||). Commented Dec 7, 2023 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.