Search code examples
google-sheets

Google Apps Script Change nth Row height in Sheets


I'm trying to change the height of every 3rd row within a Google Sheet using Apps Script.

The commented out .setBackground() works as expected, however .SetRowHeight() throws up the following error: TypeError: sheet.getRange(...).autoResizeRows is not a function

function Height() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var sheet = ss.getActiveSheet();
  var totalRows = sheet.getMaxRows();
  var totalColumns = sheet.getMaxColumns()

  for (var i=3; i <= totalRows; i+=3){
      //sheet.getRange(i, 1, 1, totalColumns).setBackground("#F3F3F3");
      sheet.getRange(i, 1, 1, totalColumns).setRowHeight(1, 200);
  }
}

Any assistance, gratefully received.


Solution

  • setRowHeight is a method that works on a sheet, not range.

    You can do it like this:

    function Height() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    
      var sheet = ss.getActiveSheet();
      var totalRows = sheet.getMaxRows();
      var totalColumns = sheet.getMaxColumns()
    
      for (var i=3; i <= totalRows; i+=3){
          //sheet.getRange(i, 1, 1, totalColumns).setBackground("#F3F3F3");
          if (i%3 === 0) {
              sheet.setRowHeight(i, 200);
          }
      }
    }