Want to auto sort a Google sheets spreadsheet on edit, but want to sort by date

Simon Flavin picture Simon Flavin · Apr 21, 2017 · Viewed 10.9k times · Source

Been looking around for a solution to this for a while and not finding anything that works, unfortunately. I have tried copying and pasting several bits of code but cannot get my sales pipeline sheet to automatically sort on edit of a date field.

The link below is to a test sheet which should give an idea as to how I have formatted my sheet:

https://docs.google.com/a/trinitymirror.com/spreadsheets/d/1huTvgwfmh0RM0kz4BUEuYFZm7DwBG-69D8jTfps3IPg/edit?usp=sharing

I want to sort by Column G with the most recent dates at the bottom of the sheet.

Any suggestions welcome!

Thank you in advance for your help.

Answer

OblongMedulla picture OblongMedulla · Apr 21, 2017

this should help?

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Sheet1")
  var range = sheet.getRange("A2:Z450");

 // Sorts by the values in column 2 (B)
 range.sort({column: 2, ascending: false});
  
}