Hide Columns and Rows using Google Apps Script

Hides one or more consecutive columns starting at the given index. Use 1-index for this method.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three columns
sheet.hideColumns(1, 3);

Here’s an example of how to use sheet.hideColumns in Apps Script to hide specific columns in a Google Sheets spreadsheet:

function hideColumnsExample() {
  // Get the active spreadsheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // Get the active sheet
  var sheet = spreadsheet.getActiveSheet();
  
  // Hide columns B and D
  sheet.hideColumns(2, 2); // Starts at column 2 (B) and hides 2 columns (B and C)
  
  // Hide column F
  sheet.hideColumns(6, 1); // Starts at column 6 (F) and hides 1 column (F)
}

In the above example, the function hideColumnsExample demonstrates the usage of sheet.hideColumns method. Here’s how it works:

  1. It begins by retrieving the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet().
  2. The active sheet is obtained using spreadsheet.getActiveSheet().
  3. The hideColumns method is then used to hide specific columns. In this case, it hides columns B and C (starting at column 2) using sheet.hideColumns(2, 2). Additionally, it hides column F using sheet.hideColumns(6, 1).
    • The first parameter in hideColumns specifies the starting column index.
    • The second parameter specifies the number of columns to hide.

Note that the column indices are 1-based, where column A corresponds to index 1, column B corresponds to index 2, and so on.

You can run the hideColumnsExample function from the Apps Script editor, and it will hide the specified columns in the active sheet of your Google Sheets spreadsheet.

hideRows(rowIndex, numRows)
Hides one or more consecutive rows starting at the given index.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);

Here’s an example of how to use sheet.hideRows in Apps Script to hide specific rows in a Google Sheets spreadsheet:

function hideRowsExample() {
  // Get the active spreadsheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  
  // Get the active sheet
  var sheet = spreadsheet.getActiveSheet();
  
  // Hide rows 2 to 5
  sheet.hideRows(2, 4); // Starts at row 2 and hides 4 rows
  
  // Hide row 7
  sheet.hideRows(7, 1); // Hides row 7
}

In the above example, the function hideRowsExample demonstrates the usage of sheet.hideRows method. Here’s how it works:

  1. It begins by retrieving the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet().
  2. The active sheet is obtained using spreadsheet.getActiveSheet().
  3. The hideRows method is then used to hide specific rows. In this case, it hides rows 2 to 5 using sheet.hideRows(2, 4). Additionally, it hides row 7 using sheet.hideRows(7, 1).
    • The first parameter in hideRows specifies the starting row index.
    • The second parameter specifies the number of rows to hide.

Note that the row indices are 1-based, where row 1 corresponds to index 1, row 2 corresponds to index 2, and so on.

You can run the hideRowsExample function from the Apps Script editor, and it will hide the specified rows in the active sheet of your Google Sheets spreadsheet.