Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active September 26, 2026 06:04
Show Gist options
  • Select an option

  • Save magnetikonline/a64d52b8f1e0b499dff29b3281f342da to your computer and use it in GitHub Desktop.

Select an option

Save magnetikonline/a64d52b8f1e0b499dff29b3281f342da to your computer and use it in GitHub Desktop.
Google spreadsheet auto row sort after cell change.

Google spreadsheet auto row sort after cell change

Example: Spreadsheet of expenses (sheet name Expenses) starting from the sixth row (allowing for headers/etc.) with each expense date in column A. After update of any expense row date cell, all line items are automatically resorted by date.

How to implement:

  1. From Google spreadsheet goto: Extensions -> Apps Script.

  2. Enter the following JavaScript code:

    function onEdit() {
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      if (sheet.getName() === 'Expenses') {
        // sort range: Row: 6, Column: A (1) -> Row: Last edited, Column: F (6)
        const range = sheet.getRange(6,1,sheet.getLastRow() - 1,6);
        range.sort({ column: 1,ascending: true });
      }
    }
  3. Click the Save floppy disk icon at the top.

  4. Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment