Resize Rows and Columns in Google Sheets Using Apps Script

This article will show you how to automatically Resize Rows and Columns in Google Sheets Using Apps Script.

Example 1: Auto-resizing Rows and Columns Based on Content

To automatically resize rows and columns based on content in Google Sheets using Apps Script, you can use the autoResizeRows() and autoResizeColumns() methods. Here’s an example code snippet:

function resizeCells() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.autoResizeRows(1, sheet.getLastRow());
  sheet.autoResizeColumns(1, sheet.getLastColumn());
}

In this code, we first get the active sheet using getActiveSheet(). We then use the autoResizeRows() method to resize all rows in the sheet based on content using 1 as the starting row index and sheet.getLastRow() as the ending row index. Similarly, we use the autoResizeColumns() method to resize all columns in the sheet based on content using 1 as the starting column index and sheet.getLastColumn() as the ending column index.

Example 2: Auto-resizing Specific Rows and Columns

To automatically resize specific rows and columns based on content in Google Sheets using Apps Script, you can use the autoResizeRows() and autoResizeColumns() methods with specific row and column indices. Here’s an example code snippet:

function resizeCells() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.autoResizeRow(1);
  sheet.autoResizeRow(2);
  sheet.autoResizeColumn(1);
  sheet.autoResizeColumn(2);
}

In this code, we first get the active sheet using getActiveSheet(). We then use the autoResizeRow() method to resize the first two rows based on content using row indices 1 and 2. Similarly, we use the autoResizeColumn() method to resize the first two columns based on content using column indices 1 and 2.

By using these examples, you can easily automatically resize rows and columns in Google Sheets using Apps Script.