Skip to content
// Sheets · Apps Script

Auto-resize columns to fit text in Google Sheets.

Use Apps Script to auto-resize one or all columns to fit their content — avoiding the off-by-one trap in autoResizeColumns() and the wrap-mode gotcha that makes cells shrink to a single word.

I want to auto-fit column widths in Google Sheets from a script so I stop manually dragging columns after every data refresh.

The script

copy · paste · trigger
autoResize.gs
Apps Script
// Auto-resize all data columns on the active sheet.
// Disables wrap first so cells measure to their longest line.
function autoResizeAllColumns() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastCol = sheet.getLastColumn();
  if (lastCol < 1) return;

  // Turn off wrap so autoResize measures actual text width
  sheet.getRange(1, 1, sheet.getLastRow(), lastCol)
    .setWrap(false);

  // startColumn is 1-based; second arg is a COUNT, not an end index
  sheet.autoResizeColumns(1, lastCol);
}

// Resize a specific range of columns (e.g. columns 2 through 5)
function autoResizeRange() {
  var sheet = SpreadsheetApp.getActiveSheet();
  // Columns B-E: startColumn=2, howMany=4
  sheet.autoResizeColumns(2, 4);
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

The signature that trips everyone up

autoResizeColumns(startColumn, numColumns) takes a 1-based starting column and a count of how many columns to resize — not a start and end. If you want columns B through E (columns 2 through 5), the call is sheet.autoResizeColumns(2, 4). Writing autoResizeColumns(2, 5) resizes B through F, one column too many. I hit this the first time I wired a post-import trigger and spent ten minutes wondering why column F kept collapsing.

To resize everything, grab sheet.getLastColumn() for the count and always start at 1. If the sheet is empty, getLastColumn() returns 0, so guard against that before calling autoResize or you get a clean no-op instead of a runtime error.

Why wrap mode breaks the measurement

autoResizeColumns sizes a column to the widest rendered cell in that column. When text wrap is on, Sheets wraps long strings onto multiple lines and renders each line narrow — so the column auto-sizes to the width of one wrapped word, not the full string. The fix is to call setWrap(false) on the full data range before resizing, then re-enable it if your layout requires it.

One practical note: if you intentionally want some columns wrapped and others not, resize all columns first with wrap off, then re-apply wrap only to the columns that need it. Doing it column-by-column in a loop works but costs one API call per column; batching is faster inside Apps Script's quota model.

Hooking it to a trigger or menu

The most useful pattern is attaching autoResizeAllColumns to an onEdit or time-driven trigger so it runs automatically after a data paste or import. In the Apps Script editor go to Triggers, add a new trigger, pick your function, and choose the event type. For imports that run via another script, call autoResizeAllColumns() as the last line of that script instead.

If you want a manual button, add a custom menu in onOpen: ui.createMenu('Format').addItem('Auto-resize columns', 'autoResizeAllColumns').addToUi(). That puts a one-click option in the menu bar without requiring anyone to open the script editor.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Why does autoResizeColumns make my columns narrower instead of wider?
Text wrap is almost certainly on. When cells wrap, Sheets measures the wrapped line width, not the full string. Call range.setWrap(false) before autoResizeColumns and the columns will size to the actual content.
How do I auto-resize a single column, like just column C?
sheet.autoResizeColumns(3, 1) — start at column 3 (C is the third column, 1-based), count of 1. The second argument is always a count, never an end index.
Can I auto-resize rows to fit wrapped text height the same way?
Yes: sheet.autoResizeRows(startRow, numRows) follows the same 1-based-start, count signature. For all rows: sheet.autoResizeRows(1, sheet.getLastRow()).
Does autoResizeColumns work on hidden columns?
No. Hidden columns are skipped silently — they stay hidden and their width is not changed. Unhide them first if you need them resized, or set their width explicitly with sheet.setColumnWidth(colIndex, pixels).
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.