Home > Software design >  How to make this sorting script run in all tabs except certain tabs (Sheets)?
How to make this sorting script run in all tabs except certain tabs (Sheets)?

Time:12-01

I am trying to sort specific columns on a google sheet but exluding a few different tabs. I use google scripts a decent amount in my job but most of the scripts I get from Stack and then edit them because I am not a developer/engineer, I'm in a non-tech role but I know a slight amount about coding.

Can someone that knows coding tell me what I am doing wrong?

I know there is a more efficient way to do the naming conventions of the columns and the ascending and I will change that later but wanted to see if anyone knows why I am getting this error? Thanks!

I found this code from a Stack post that I changed that I feel like should work but I keep getting this error on my trigger which is "onEdit":

"Error: The coordinates of the range are outside the dimensions of the sheet."

 function sortEverySheetTest() {
  var excludeSheetNames = ["Sheet 1","Sheet 2","Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6", "Sheet 7", "Sheet 8"]; // <--- Added

  var sortFirst = 5;
  var sortFirstAsc = true;
  var sortSecond = 6;
  var sortSecondAsc = true;
   var sortThird = 7;
  var sortThirdAsc = true;
  var headerRows = 2;
  var activeSheet = SpreadsheetApp.getActiveSheet();
  var sheetName = activeSheet.getSheetName();

  if (excludeSheetNames.includes(sheetName)) return; // <--- Added

  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  var range = sheet.getRange(headerRows 1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
  range.sort([{ column: sortFirst, ascending: sortFirstAsc }, { column: sortSecond, ascending: sortSecondAsc }, { column: sortThird, ascending: sortThirdAsc }]);
}

CodePudding user response:

Try this:

function onMyEdit(e) {
  e.source.toast("Entry");
  const excl = ["Sheet 1","Sheet 2","Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6", "Sheet 7", "Sheet 8"]; 
  const sh = e.range.getSheet();
  const shts = e.source.getSheets().filter(sh => !~excl.indexOf(sh.getName())).map(sh => sh.getName());
  const idx = shts.indexOf(sh.getName());
  if(~idx) {
    e.source.toast("Gate1");
    let rg = sh.getRange(3,1,sh.getLastRow() - 2,sh.getLastColumn());
    rg.sort([{column:5,ascending:true},{column:6,ascending:true},{column:7,ascending:true}]);
  }
}

Here's the markdown table for my sheet data after sorting:

A B C D E F G H I J
2 COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10
3 10 17 6 7 0 6 14 14 6 0
4 2 4 33 19 0 16 0 0 19 14
5 3 18 16 5 2 7 9 5 16 19
6 16 0 12 3 3 4 13 19 17 12
7 14 2 19 4 6 14 2 16 12 8
8 10 5 5 13 10 3 8 2 15 15
9 14 5 16 9 10 13 0 17 16 7
10 4 9 8 4 10 13 10 17 19 0
11 0 9 8 7 11 0 15 4 14 19
12 17 12 2 9 12 4 13 5 11 17
13 14 4 0 5 14 5 1 5 17 2
14 5 5 18 19 14 9 0 5 9 2
15 15 5 13 15 16 2 7 1 14 19
16 10 15 18 17 16 13 16 13 10 11
17 9 12 1 3 17 3 5 1 2 9
18 12 5 16 10 17 5 1 8 14 9
19 16 2 99 2 17 8 11 2 2 14
20 19 13 16 13 17 16 11 10 14 15
21 2 18 9 10 18 3 16 1 17 19
22 2 7 12 6 18 9 3 6 13 18
  • Related