Hide Row in Google Sheets if Cell Contains “no” -

2020-08-08 08:05发布

问题:

I'm running 1 Master spreadsheet, with 4 product specific spreadsheets pulling info from it. If no relevant info is pulled, the smaller spreadsheets will display "no" in all cells. I want rows with "no" to be hidden.

I've sort of hacked together something, that sort of works. But I can't get it to work on all 4 sheets. I've tried getSheets(), I've tried getSheetByName(name) and naming all 4 sheets...it doesn't work.

Here's what I have so far:

function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var sheets = ss.getSheets(); // array of all sheet objects in ss
  var numSheets = ss.getNumSheets(); // count of sheets

//show all the rows
  sheet.showRows(1, maxRows);

//get data from clumn C
  var data = sheet.getRange('C:C').getValues();

//iterate over all rows
  for(var i=0; i< data.length; i++){
//compare column, if no, then hide row
    if(data[i][0] == 'no'){
      sheet.hideRows(i+1);
  }
 }
}

回答1:

Try: (edited)

function onOpen() {
  var ss = SpreadsheetApp.getActive();
  var sheets = ss.getSheets(); // array of all sheet objects in ss
  var numSheets = ss.getNumSheets(); // count of sheets
for(sheet in sheets) {
  if(sheets[sheet].getSheetName() == "Master") continue;
//show all the rows
  sheets[sheet].showRows(1, sheets[sheet].getMaxRows());

//get data from clumn C
  var data = sheets[sheet].getRange('C:C').getValues();

//iterate over all rows
  for(var i=0; i< data.length; i++){
//compare column, if no, then hide row
    if(data[i][0] == 'no'){
      sheets[sheet].hideRows(i+1);
  }
 }
 }
}