Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosize Spreadsheet Columns

I didn't see anything in the documentation that would make me think this is a supported function. I'm writing to several sheets and visually it's difficult to see all the data without highlighting all the columns and autosizing with my mouse. I'd rather not have to do that everytime I run my script.

Is there a way to do this programmatically with app scripts? I saw the option to setColumnWidth, but how do I know how wide to set my columns when my text could vary from column to column.

==thanks for any help.

like image 675
jrad Avatar asked Sep 12 '12 05:09

jrad


2 Answers

autoResizeColumn(columnPosition)

See the API for more details

like image 92
Trevor Iampen Avatar answered Oct 14 '22 07:10

Trevor Iampen


There is now a new method called autoResizeColumn(columnPosition)

var s = SpreadsheetApp.getActive().getActiveSheet();
s.autoResizeColumn(6);

Autosizes the 6th column (a.k.a column "F").

See this stackoverflow answer https://stackoverflow.com/a/29930315/8458041


There's also a way to do multiple columns at once: autoResizeColumns(startColumn, numColumns)

var s = SpreadsheetApp.getActive().getActiveSheet();        
s.autoResizeColumns(6, 3);

Autosizes columns F, G, and H.


The corresponding autosizing method for multiple rows is autoResizeRows(startRow, numRows)

s.autoResizeRows(6, 3);

Autosizes rows 6, 7, and 8

There is no corresponding single-row autosizing method (yet?); to autosize a single row, specify 1 for the number of rows

s.autoResizeRows(6, 1);

Autosizes only row 6

like image 5
App Coder Avatar answered Oct 14 '22 05:10

App Coder