Auto-Sorting a Specific Google Sheet Range


Sorting your Google Sheet using Apps Script is very simple. Firstly, you'll need to decide how you want to sort your data - either by the entire sheet or by using a specific range.

In this how-to guide, we'll go over sorting your data using a specific range. For sorting the entire sheet, check out the Auto-Sorting Your Google Sheet how-to guide.

In a nutshell, you'll get a Range object and then apply .sort() against it. Unlike the .sort() method for a Sheet object, you can specific multiple columns in your sorting logic which allows you to do more complex sorting.

Sorting by Ascending Order

If you're just looking to sort the data in ascending order, you can simply pass in a column number when calling the method - e.g. sort(1) which would sort Column A in ascending order. For sorting multiple columns in ascending order, just pass your column numbers as an array, in the order you want to sort them in - e.g. sort([1,2]) which will sort Column A in ascending order and then Column B in ascending order.

Sorting by either Ascending or Descending Order

To specify the column and order, you can pass an object as a parameter when you call the sort() method - For example range.sort({column:1, ascending:true}).

To sort more than one column at a time, you just need to pass in your sorting objects as an array - e.g. range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]) will sort your range by Column B in descending order and then Column A in ascending order.

Cheat Sheet

// Sort by Column B ascending range.sort(2) // Sort by Column A descending range.sort({column:1, ascending:false}) // Sort by Column B ascending and then Column A ascending range.sort([2, 1]) // Sort by Column B descending and then Column A ascending range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}])


Automatically Sorting Your Range

The tricky part to auto-sorting is the use of an event trigger. There's two ways to create an event trigger, either using a simple trigger (like shown below) or using an installable trigger (Learn more here)

Here's an example of a simple trigger where cells A1 through E100 in 'Sheet1' will auto-sort whenever someone makes an edit to it:

function onEdit(e) { const triggeredSheet = e.range.getSheet() const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1') if (triggeredSheet.getName() == sheet.getName()) { let range = sheet.getRange(1,1,100,5) // this is the same as saying sheet.getRange("A1:E100") range.sort({column: 1, ascending: false}) } }

To test or run this code, make sure to do what the user would do since this code utilizes the onEdit Simple Trigger. See our Event Triggers Tutorial Video for step-by-step instructions on how to use event triggers.

thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension