1
votes

I have multiple tabs in a single sheet and when the word HIDE is in column A, I want the row to be hidden when the sheet is edited. The below works great for a single named sheet (SHEET1) but I want it to check all sheets.

I can add the below function multiple times naming each sheet in each function and it works but I will continue to add additional sheets later and don't want to have to add another function for each tab I add.

function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("SHEET1");      // This only allows me to check 1 sheet by name
  var row=s.getRange('A:A').getValues();          // This looks for the value in column A that hides that row if true

s.showRows(1, s.getMaxRows());
for(var i=0; i< row.length; i++){ if(row[i] == 'HIDE') { s.hideRows(i+1, 1); }   // This is the value to hide

}}
2

2 Answers

2
votes

Works on any edited sheet in the spreadsheet. Only attempts to hide rows not already hidden by user or by filter

function onEdit(e) {
  e.source.toast('Entry');
  const sh=e.range.getSheet();
  let v=sh.getRange(1,1,sh.getLastRow(),1).getValues();
  v.forEach(function(r,i){if(r[0]=="HIDE" && !sh.isRowHiddenByUser(i+1) && !sh.isRowHiddenByFilter(i+1)){sh.hideRows(i+1);}});
}

animation:

enter image description here

1
votes
var mySheetsArr = ss.getSheets();

will return an array of all the sheets in your spreadsheet so you can then loop through them as required.

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheets()

From the docs

// The code below logs the name of the second sheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
if (sheets.length > 1) {
  Logger.log(sheets[1].getName());
}