Apps Script: Automatically Sort a Sheet When Data Changes (Google Sheet)

Imagine a Google Sheet that keeps itself neatly sorted every time someone updates it — no clicking, no dragging, no manual sorting. Sounds helpful, right? That’s exactly what Google Apps Script can do for you! With just a bit of scripting, your data can automatically reorder itself every time a new row is added or a cell changes. This is a game-changer for shared spreadsheets and team collaboration.

Whether you’re managing a task list, sales log, or student grades, auto-sorting keeps your sheet organized and easy to read — without the hassle.

Apps Script: Automatically Sort a Sheet When Data Changes (Google Sheet)

What Is Auto-Sorting in Google Sheets?

Auto-sorting is when your spreadsheet automatically rearranges its data based on specific columns, right after a change is made. This is powered by a Google Apps Script, which runs in the background and responds to edits in real time. Think of it as having a tiny helper that keeps things tidy for you.

How Does It Work?

We use a special function called onEdit(e) that triggers automatically whenever someone makes a change to the sheet. The script then sorts the sheet based on your preferred column — such as date, name, or priority.

Real-Life Example: Sorting a Task List by Due Date

Let’s say you have a shared task tracker with your team. You want the tasks to stay sorted by due date so that the most urgent items are always on top. Here’s a sample layout:

Sample Task List

Task Assigned To Due Date
Design homepage Alice 2025-05-01
Write blog post Bob 2025-04-20
Update pricing table Clara 2025-04-18

Right now, the oldest task is at the bottom — but we want it on top!

Step-by-Step: How to Automatically Sort When Data Changes

  1. Open your Google Sheet.
  2. Click Extensions > Apps Script.
  3. Delete any existing code and paste the following:
    
    function onEdit(e) {
      var sheet = e.source.getActiveSheet();
      
      if (sheet.getName() === "Tasks") { // Change to your sheet name
        var range = sheet.getDataRange();
        range.sort({column: 3, ascending: true}); // Sort by column 3 (Due Date)
      }
    }
        
  4. Click the floppy disk icon or press Ctrl + S to save.
  5. Close the Apps Script editor — your sheet is now set to auto-sort!

Key Benefits of Auto-Sorting

  • Stay Organized: Your most important rows stay at the top — always.
  • Save Time: No need to manually sort your sheet after each edit.
  • Great for Collaboration: Perfect for shared lists, logs, or schedules.

Pro Tips

  • Change column: 3 to match the column you want to sort by (e.g., 1 = Column A).
  • Use ascending: false to sort in reverse (e.g., most recent date first).
  • Always double-check your sheet name in the script — it’s case-sensitive!
  • If your sheet has headers, make sure to freeze the top row so it stays in place.

Advanced: Sorting by Multiple Columns

You can also sort by more than one column — for example, sort by due date, then by assigned person:


range.sort([
  {column: 3, ascending: true},
  {column: 2, ascending: true}
]);

Quick Cheat Sheet

Script Purpose
onEdit(e) Runs every time the sheet is edited
range.sort({column: n}) Sorts the data by column number
sheet.getName() Checks if the script is running on the right sheet

Auto-sorting your Google Sheet with Apps Script is one of those small tweaks that makes a big difference — especially when working with dynamic data. It helps your team stay focused, reduces clutter, and keeps everything exactly where it should be. Once you’ve set it up, it runs like magic in the background — keeping your spreadsheet neat, tidy, and stress-free.

So go ahead, give it a try — and say goodbye to manually sorting your sheets ever again!

Leave a Comment

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

Scroll to Top