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. Theerepresents 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. Theascending: trueargument orders the dates chronologically from oldest to newest. Change this tofalsefor 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:
| Row | A (Deadline) | B (Task) | C (Owner) |
| 1 | Deadline | Task | Owner |
| 2 | 2026-11-15 | QA Testing | Sarah Jenkins |
| 3 | 2026-09-01 | Wireframe Approval | Mark Torres |
| 4 | 2026-10-10 | Client Pitch | David Chen |
The Application:
- Go to Extensions > Apps Script.
- Paste the script and ensure
targetSheetmatches your tab name exactly (e.g.,"Project_Pipeline"). - Return to your spreadsheet. Change the date in Row 2 to
2026-08-01and 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.
| Row | A (Deadline) | B (Task) | C (Owner) |
| 1 | Deadline | Task | Owner |
| 2 | 2026-08-01 | QA Testing | Sarah Jenkins |
| 3 | 2026-09-01 | Wireframe Approval | Mark Torres |
| 4 | 2026-10-10 | Client Pitch | David Chen |
Common Errors & How to Fix Them
- The script does not run or sort at all: The
targetSheetvariable is case-sensitive and does not perfectly match your actual tab name. Fix: Double-checkconst 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.
