Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically resize columns in Google sheets

I would like to have columns on google sheets automatically resize to fit my text, without me having to manually resize them after every entry. How do I do this?

like image 809
Isaac King Avatar asked Apr 28 '15 18:04

Isaac King


2 Answers

To make the column re-size fully automatic you an create this function, which will trigger every time you edit the contents of a cell:

function onEdit(e) {
  e.range.getSheet ().autoResizeColumn (e.range.getColumn ());
}

Note that this trigger will only run in response to typing. Pasting and importing will not cause this trigger to run.

If you want a function that will resize all columns in your active sheet, this one will do it:

function resizeAllColumns () {
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getDataRange();
  var firstColumn = dataRange.getColumn();
  var lastColumn = dataRange.getLastColumn();
  sheet.autoResizeColumns(firstColumn, lastColumn);
}

For convenience you can attach a keyboard shortcut to the function if you need to execute the manual function often.

like image 73
Terrible Tadpole Avatar answered Sep 29 '22 15:09

Terrible Tadpole


FYI -- For those who don't want to delve into scripting, but want a way to do it faster, you can auto-resize all the columns at once, manually: Hit Cmd+A (or Ctrl+A in Windows) to select everything, then move the mouse to the border between any two column headers -- as if you wanted to resize that one manually-- the cursor changes to an arrow... and double click. Found this trick here.

like image 25
Tom Hundt Avatar answered Sep 29 '22 13:09

Tom Hundt