I am trying to sort data descending based upon column B (2) which has different numbers in each cell. EG: B8 = 99, B14 = 70, etc. I'm intending for it to autosort and do it onEdit. I'm not good at coding, like at all. So I watched a YouTube vid and came up with this, but it still doesn't work.

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 === "Roster" && column === 9 && row >= 8)) return

    const range = currentSheet.getRange(8,5,21,22)

    range.sort({column: 2, ascending: false})


function onEdit(e){

  autoSort (e)
  • I haven't checked the other things but at a quick glance the closing curly bracket of onEdit seems to be missing
    – Daniele
    Jun 5 at 21:44
  • In addition to what Daniele said, what error are you getting when you run this? From reading through it, it should work just fine though.
    – David Tan
    Jun 6 at 0:17
  • Hi, welcome to WebApps. There's either a typo in your question, or a logic error in your code. currentSheet.getRange(8,5,21,22) defines the range "E8:Z28" and range.sort({column: 2, ascending: false}) will sort that range based on the second column (Column F). Column B (which is the column that you describe in your question) NEVER comes into it. You say but it still doesn't work. Would you please describe EXACTLY what you expect to happen, and EXACTLY what happens instead. If there was an error message (which I doubt), please show the full message.
    – Tedinoz
    Jun 6 at 1:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.