KeepSorted

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.

1 2 3
Your script
// 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

  1. Open your sheet and choose Extensions > Apps Script.
  2. Delete what is in Code.gs, paste the script, and click Save (the disk icon).
  3. That's it for edits: onEdit is 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)

The catches

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.

You're on the list. We'll email you when KeepSorted is ready.

The generator and the scripts stay free either way.

More KeepSorted guides

Sources