Google Sheets auto sort script generator
Fill in the form and get an Apps Script that keeps a range of your Google Sheet sorted: on every edit, and if you want, after Google Forms responses and rows added by other tools. The script is built in your browser; nothing you type here is sent anywhere. It sorts the real rows with Google's own Range.sort, so the header stays on top, blank cells go last and formatting moves with its rows. Checked against Google's Apps Script documentation on 1 November 2026.
// Auto-sort script made with keepsorted.tuper.app/google-sheets-auto-sort-script/
// Keeps 'Sheet1'!A1:F sorted by A (A to Z, smallest first).
// Header rows stay on top; blank cells go last; case is ignored (Google Sheets sort rules).
const SHEET_NAME = 'Sheet1';
const FIRST_ROW = 1; // first row of the range, header included
const HEADER_ROWS = 1; // rows at the top of the range that never move
const FIRST_COLUMN = 1; // A
const LAST_COLUMN = 6; // F
const LAST_ROW = 0; // 0 = down to the last row with data
const SORT_KEYS = [ // first key wins, later keys break ties
{column: 1, ascending: true}, // A, A to Z
];
// Runs by itself on every edit a person makes (simple trigger, no setup).
function onEdit(e) {
const sheet = e.range.getSheet();
if (sheet.getName() !== SHEET_NAME) return;
sortRange_(sheet);
}
function sortRange_(sheet) {
if (!sheet) return;
const top = FIRST_ROW + HEADER_ROWS;
const bottom = LAST_ROW ? Math.min(LAST_ROW, sheet.getLastRow()) : sheet.getLastRow();
if (bottom - top + 1 < 2) return;
const last = LAST_COLUMN || sheet.getLastColumn();
sheet.getRange(top, FIRST_COLUMN, bottom - top + 1, last - FIRST_COLUMN + 1)
.sort(SORT_KEYS);
}
How to install it
- Open your sheet and choose Extensions > Apps Script.
- Delete what is in
Code.gs, paste the script, and click Save (the disk icon). - That's it for edits:
onEditis a simple trigger, so it runs for everyone who edits the sheet, with no authorization step.
If you ticked Forms or other tools: choose installTriggers in the function list at the top of the editor, click Run once and allow access when Google asks. Check the result on the Triggers page (the alarm-clock icon). Those triggers run as you, even when the sheet is closed.
Try it on a copy of the sheet first. To undo a sort, use Edit > Undo or File > Version history.
What each part does (Apps Script sort range)
sheet.getRange(row, column, numRows, numColumns)picks the rows below the header.getLastRow()makes the range grow with your data, so new rows are included..sort(SORT_KEYS)is Range.sort. It takes a column number (.sort(3)), one spec (.sort({column: 3, ascending: false})) or a list of specs for several columns. Column numbers count from column A of the sheet, not from the start of the range.onEdit(e)runs after every edit a person makes;e.rangeis the edited cell, which the script uses to check the tab (and, with the first checkbox, the column).onFormSubmit,onChangeand the time-driven trigger are installable triggers. A form response, a row written by another script or the Sheets API is not an edit, soonEditnever sees it; Google's rule is that script and API changes fire no trigger at all, which is why the timer is there.
The catches
- Rows move while people work. Each edit re-sorts the range, so a half-typed row can jump away. The first checkbox limits sorting to edits in the sort columns; fill those in last.
- One range per script. Tables stacked in the same columns get mixed into one. Give each table its own copy of
sortRange_with its own rows. - Text that looks like a number or date sorts as text ("10" before "2"). Check with
=ISNUMBER(B2). - No custom order (High, Medium, Low). Use a helper column such as
=MATCH(C2, {"High","Medium","Low"}, 0)and sort on it. - Merged cells in the range make the sort fail; the error shows under Executions in the editor.
- Simple triggers stop after 30 seconds; the timer uses your daily trigger runtime quota.
No code: KeepSorted
KeepSorted is a Google Sheets add-on that does the same from a sidebar: select the range, pick the sort columns, Save. Same rules as the script (header kept, blanks last, rows moved with their formatting and formulas). Free: one area per tab, up to 2 sort columns, no trial clock. Pro ($5/month or $39/year): up to 5 sort columns, custom orders like High > Medium > Low, stacked tables sorted separately, and sorting after form responses, other editors' changes and an hourly check.
KeepSorted is built and in testing. It is not in the Google Workspace Marketplace yet. Leave your email and we send one message when you can install it.
The generator and the scripts stay free either way.
More KeepSorted guides
- Auto-sort when a new row is added
- Auto-sort alphabetically (A to Z)
- Auto-sort by date, newest first (and Google Forms)
- Auto-sort by multiple columns when data changes
- StaySorted alternative: an honest comparison
Sources
- Range.sort reference · Sheet.getLastRow
- Simple triggers (onEdit, 30-second limit) · installable triggers (onFormSubmit, onChange, time-driven; script and API changes fire no triggers)
- everyMinutes: 1, 5, 10, 15 or 30 · Apps Script quotas