Home > Blockchain >  Google Sheets Apps Script - I have auto sorted based on column, now I want to auto sort a range of c
Google Sheets Apps Script - I have auto sorted based on column, now I want to auto sort a range of c

Time:06-11

I have a google sheet where the first column can either be "01_High","02_Med","03_Low" or "04_N/A". I have the script below so that when someone changes the value of a a cell in that first column, it auto sorts the entire sheet (see below).

What I'd like to happen next is to sort the range of rows where the value is "01_High" or "02_Med", etc. based on a date value in column E. Basically I'm trying to automate a two step advanced sort. As you'll see below, the script sorts the sheet itself as opposed to other solutions I've seen where it creates a new sheet or a sorted area of columns on the same sheet.

Thank you!

function autoSort(e){
    const row = e.range.getRow()
    const column = e.range.getColumn()
    const ss = e.source
    const currentSheet = ss.getActiveSheet()
    const currentSheetName = currentSheet.getSheetName()
  
    if(!(currentSheetName === "Phone and Email Sheet" && column === 1 && row>=3)) return
  
    const range = currentSheet.getRange(3,1,currentSheet.getLastRow()-1,7)

    range.sort({column: 1, ascending: true})

}


function onEdit(e){

  autoSort(e)

}

CodePudding user response:

Ah, it needs to be (column === 5, column === 1)

CodePudding user response:

Replace column === 1 by

(column === 1 || column === 5)

and sort at the same time by (true or false as you wish for column 5)

range.sort([{ column: 1, ascending: true }, { column: 5, ascending: false }])
  • Related