简体   繁体   中英

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

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 aa 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)

}

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 }])

啊,必须是(列=== 5,列=== 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM