Google Apps Script to auto sort rows by date on edit

Navigate to Extensions > Apps Script. Clear the editor and paste the code below. Save the script. It triggers automatically when you edit a date in Column A (assuming your data has 1 header row).

JavaScript

function onEdit(e) {
  const sheet = e.source.getActiveSheet();
  const targetSheet = "Sheet1"; // Change to your exact sheet tab name
  const sortColumn = 1; // 1 = Column A, 2 = Column B, etc.
  const headerRows = 1; // Number of rows to exclude from the sort

  if (sheet.getName() === targetSheet && e.range.getColumn() === sortColumn && e.range.getRow() > headerRows) {
    const range = sheet.getRange(headerRows + 1, 1, sheet.getLastRow() - headerRows, sheet.getLastColumn());
    range.sort({column: sortColumn, ascending: true});
  }
}

The Syntax Breakdown

  • onEdit(e): A simple trigger built natively into Google Sheets. It automatically executes the script whenever a user changes a value in the spreadsheet. The e represents the event object holding contextual data about the edit.
  • targetSheet: Defines the specific tab where the script should run. This prevents the script from inadvertently sorting data on your other worksheets.
  • sortColumn: The numerical index of the column containing your dates (e.g., 1 = A, 2 = B, 3 = C).
  • headerRows: Specifies how many frozen header rows to ignore so your titles are not sorted into the dataset.
  • e.range.getColumn() === sortColumn: A conditional check that ensures the heavy sorting action only triggers if the cell you just edited is located within your designated date column.
  • range.sort(...): The core function that executes the physical sort. The ascending: true argument orders the dates chronologically from oldest to newest. Change this to false for newest to oldest.

Real-World Example: Tracking Project Milestone Deadlines

You manage a dynamic project pipeline where team members constantly update milestone deadlines. Instead of manually applying data filters every time a date changes, you want the sheet to chronologically self-organize.

Here is your raw data structure:

RowA (Deadline)B (Task)C (Owner)
1DeadlineTaskOwner
22026-11-15QA TestingSarah Jenkins
32026-09-01Wireframe ApprovalMark Torres
42026-10-10Client PitchDavid Chen

The Application:

  1. Go to Extensions > Apps Script.
  2. Paste the script and ensure targetSheet matches your tab name exactly (e.g., "Project_Pipeline").
  3. Return to your spreadsheet. Change the date in Row 2 to 2026-08-01 and press Enter.

The Output:

The moment you press Enter, the script captures the edit and instantly reorganizes the table. The 2026-08-01 row shifts to the top of the dataset (Row 2), followed by September, and then October.

RowA (Deadline)B (Task)C (Owner)
1DeadlineTaskOwner
22026-08-01QA TestingSarah Jenkins
32026-09-01Wireframe ApprovalMark Torres
42026-10-10Client PitchDavid Chen

Common Errors & How to Fix Them

  • The script does not run or sort at all: The targetSheet variable is case-sensitive and does not perfectly match your actual tab name. Fix: Double-check const targetSheet = "Sheet1"; and ensure it matches the text on your tab at the bottom of the screen exactly.
  • Headers get sorted into the middle of the dataset: The script is treating your column headers as alphabetical data to be sorted because the offset is incorrect. Fix: Update const headerRows = 1; to match the exact number of frozen header rows at the top of your dataset.
  • The script requires authorization every time it runs: You attempted to use an installable trigger or complex custom function instead of the native simple trigger. Fix: Ensure the function is strictly named onEdit(e)—this specific reserved name grants permission to run silently in the background without constant user authorization.