To automatically sort data in Google Sheets you can use this Apps Script. The video above takes you through the steps or you can follow the notes below.
function onEdit(event){ var w = event.source.getSheetByName("Scores");//Replace Scores with the name of the worksheet you want to sort in var c = w.getActiveCell(); var srtby = 2; //Change this to the position of the col you want to sort by var t = "A2:B"; // Change this to range of cells that you want to sort (don't include the column headings). if(c.getColumn() == srtby){ var range = w.getRange(t); range.sort( { column : srtby, ascending: false } );//change false to true if you want to sort in ascending order } }
Installing the Script
- Copy the script above
- Open the Google Sheets file you want to use the script in
- Click on the Extensions menu and select Apps Script
- Select any existing script in the script window and paste my script over it to replace it
- Give the Project a name (top of screen)
- Save the Project (look for save button near the top)
- You can then close down that tab in your browser
Amending the Script
You will need to make some changes to the script so that it works for your data:
Line 2 change the word Scores to the sheet name that you want to sort in
Line 5 change the number 2 to the position of the column within your data that you want to sort on
Line 6 change the range A2:B to reflect the range of cells that your data resides in
Line 10 change the word false to true if you want to sort in ascending order
I’m using this, but want to apply the sorting to 5 sheets (tabs) in the same project. Do i somehow apply the script 5 times, or is there a way to list multiple sheets on line 2 of the script? I am new at using Apps Scripts. Thanks.
THANK YOU!
i have been scouring the web looking for this solution. After trying all the others, yours ACTUALLY worked.
Huge THANKS
it works!!! finally after scouring the many other site! this one actually worked! tq so much!