Auto Sort Google Sheet

Well, I’m sure if you are using a bunch of Google Sheet to daily stuff like tracking your expenses, you might want it to be sorted by some column.

In my case, I want to be sorted by date.

This is my Google Sheet looks like the end of the month, I will submit my claim based on expenses on a debit or credit card.

Here’s the code for you to do just that.

function autoSortSheet() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getRange("A3:Z" + sheet.getLastRow()); // Adjust the range as needed
  range.sort({column: 1, ascending: true});
}

function createTrigger() {
  ScriptApp.newTrigger('autoSortSheet')
    .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
    .onEdit()
    .create();
}

So whenever you put a new row with an earlier date, it will auto-sort by column A3.

Notice there is “column :1” to sort column A. If you need to sort different column, change the index accordingly.

Do not forget to add the trigger to make it work when you edit the sheet.

Leave a Reply

Your email address will not be published. Required fields are marked *