Automatically resize rows & columns in Google Sheets using Apps Script
In this tutorial, I will show you how easy it is to automatically resize rows and columns in Google Sheets using Apps Script. This can be useful when you automate importing CSV files into Google Sheets using Apps Script since you can automatically resize the rows and columns in the sheet after importing data.
Automatically resize columns in Google Sheets using Apps Script
To automatically resize a single column, use the autoResizeColumn(columnNumber)
method of the Sheet object and specify which column to resize. The width of this column will be automatically adjusted based on the text contained in that column.
The code below resizes the first column (i.e., Column A).
function autoResizeSingleColumn() {
SpreadsheetApp.getActiveSheet().autoResizeColumn(1);
}
To automatically resize multiple columns, use the autoResizeColumns(startColumn, numColumns)
method. The width of these columns will be automatically adjusted. The video below shows an example of the first two columns being automatically resized.
The method autoResizecolumns()
needs two parameters: startColumn
and numColumns
. If these are 2 and 3, then 3 columns starting from the 2nd column will be automatically resized.
function autoResizeMultipleColumns() {
SpreadsheetApp.getActiveSheet().autoResizeColumns(2,3);
}
Note
The methods autoResizeColumn(N)
and autoResizeColumns(N, 1)
will have the exact same effect of auto-resizing the Nth column.
Automatically resize rows in Google Sheets using Apps Script
To automatically resize rows, use the autoResizeRows(startRow, numRows)
method and specify which rows to resize. The height of these rows will be automatically adjusted.
The method autoResizeRows()
needs two parameters: startRow
and numRows
. If these are 3 and 5, then 5 rows starting from the 3rd row will be automatically resized.
Note
There doesn't seem to be a autoResizeRow()
method in Apps Script to resize just a single row. However, you can use auoResizeRows(rowNumber, 1)
to achieve the same thing.
function autoResizeMultipleRows() {
SpreadsheetApp.getActiveSheet().autoResizeRows(3,5);
}
Conclusion
In this tutorial, you learned how to automatically resize rows and/or columns in Google Sheets using Apps Script.
Thanks for reading!
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!