1

Beginner trying to use scripts, please help!

I am needing a way to automatically sort multiple columns on different sheets in the same workbook. The current script I am using is only sorting one sheet.

const SHEET_NAME = "History";
const SORT_DATA_RANGE = "A2:Y";
const SORT_ORDER = [
{column: 25, ascending: true},
{column: 17, ascending: true}, 
{column: 1, ascending: true}
];

function HistorySort(e){
  multiSortColumns();
}

function multiSortColumns(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(SHEET_NAME);
  var range = sheet.getRange(SORT_DATA_RANGE + sheet.getLastRow()); // change here
  range.sort(SORT_ORDER);
  ss.toast('Sort complete.');
}
2
  • Are they all the same parameters for sorting? Commented Aug 28, 2021 at 0:01
  • No, they are not. Commented Aug 28, 2021 at 0:05

2 Answers 2

1
function sort_sheets() {

  var sorts = [
    {sheet: 'Sheet1', range:'a2:d', cols: [1, 2, 3]},
    {sheet: 'Sheet2', range:'b2:f', cols: [2, 3]},
    {sheet: 'Sheet3', range:'a2:e', cols: [4, 5]},
  ];

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  for (var sort of sorts) {
    var sheet = ss.getSheetByName(sort.sheet);
    var range = sheet.getRange(sort.range + sheet.getLastRow());
    var sort_order = [];
    for (var col of sort.cols) {
      sort_order.push({column: col, ascending: true})
    }
    range.sort(sort_order);
  }

  ss.toast('Sort complete.');
}

It can be even simpler if the range is the same for all the sheets.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's a way to loop through all of them and sort. However, if your columns to sort on are different in each sheet, you'll have to specify that on each one.

//SHEET_NAME = "History";
var SORT_DATA_RANGE = "A2:Y";
var SORT_ORDER = [
{column: 25, ascending: true},
{column: 17, ascending: true}, 
{column: 1, ascending: true}
];

function sortAllSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  allSheets.forEach(multiSortColumns);
  ss.toast('Sort complete.');
}


function multiSortColumns(sheet){
  //you could put some exceptions in here for sheets you might want to skip
  var range = sheet.getRange(SORT_DATA_RANGE + sheet.getLastRow()); // change here
  range.sort(SORT_ORDER);
}

2 Comments

Yes, different sheets have different columns to sort. I see how the code you posted would work if all columns on all sheets were the same, however, since this is not the case, how/what would I need to change?
If I wanted to do this I would store all of the settings for all of the Spreadsheet/Sheets in one Spreadsheet and then read that table to to create an object of settings for each sheet in a spreadsheet. Obviously, this will take a lot of time and effort organizing it and maintaining it so I wouldn't want to do it for nothing unless I wanted to do it for myself and I don't want to do it for myself.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.