Home > front end >  Google sheets : onEdit all sheets available
Google sheets : onEdit all sheets available

Time:05-25

looking for some help with Google Apps script I've got this script :

SHEET_NAME = "1";
SORT_DATA_RANGE = "A3:G100";
SORT_ORDER = [
{column: 2, ascending: true},
{column: 3, ascending: true},
{column: 1, ascending: true}, 
];

function onEdit(e){
  multiSortColumns();
}
function multiSortColumns(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(SHEET_NAME);
  var range = sheet.getRange(SORT_DATA_RANGE);
  range.sort(SORT_ORDER);
}

And I'm simply trying to make it work in all sheets, can someone help me out ?

CodePudding user response:

try

SORT_DATA_RANGE = "A3:G100";
SORT_ORDER = [
  { column: 2, ascending: true },
  { column: 3, ascending: true },
  { column: 1, ascending: true },
];

function onEdit(e) {
  var sh = e.source.getActiveSheet()
  multiSortColumns(sh);
}
function multiSortColumns(sheet) {
  var range = sheet.getRange(SORT_DATA_RANGE);
  range.sort(SORT_ORDER);
}

CodePudding user response:

As posted in Discord, I think this is what you're looking for:

function onEdit() {

    const SORT_DATA_RANGE = "A3:G100"
    const SORT_ORDER = [
      { column: 2, ascending: true },
      { column: 3, ascending: true },
      { column: 1, ascending: true }, 
    ]

    SpreadsheetApp.getActiveSpreadsheet()
                  .getSheets()
                  .forEach(sheet => {
                     sheet.getRange(SORT_DATA_RANGE)
                          .sort(SORT_ORDER)
                  })

}
  • Related