Damian Burgos
Damian Burgos

Reputation: 3

Google Apps Script - Hide Rows

I work for a dealership & we use a spreadsheet to manage our inventory, I would like it if any row on column F to be hidden if it says "Sold" in the field, I found this script & changed it a bit but it's not working.

function onOpen() {
  SpreadsheetApp.getUi().createMenu("Scripts")
    .addItem("Hide Sold Units", "soldUnits")
    .addItem("Show all rows", "showAllRows")

    .addToUi();
}

function soldUnits() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Pre-Owned");
  var data = sheet.getDataRange().getValues();
  for(var i = 1; i < data.length; i++) {
    //If column C (6th column) is "sold" then hide the row.
    if(data[i][6] === "Sold") {
      sheet.hideRows(i + 1);
}
  }
}

function showAllRows() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Pre-Owned");
  sheet.showRows(1, sheet.getMaxRows());
} 

Upvotes: 0

Views: 51

Answers (1)

Cooper
Cooper

Reputation: 64082

Index five is the sixth column

function soldUnits() {
  const ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Pre-Owned");
  var data = sheet.getDataRange().getValues();
  for (var i = 1; i < data.length; i++) {
    if (data[i][5] === "Sold") {
      sheet.hideRows(i + 1);
    }
  }
}

Columns start at one. Arrays start at zero.

Upvotes: 2

Related Questions