Trey
Trey

Reputation: 1

Can I use an array of cells as values for a script?

I found a script that works well for my needs, but I would like to make a change so that it works even better. What I'd like to change is the line var hideSheetsContaining = "tw"; to select an array of cells in a tab/sheet because the values change periodically and it would be easier to edit.

function hideTW() {

  // My clumsy edit!
  var hideSheetsContaining = "tw";

  // Original script
  // var hideSheetsContaining = Browser.inputBox("Hide sheets with names containing:");
  if (sheetMatch(hideSheetsContaining)){
    for (var i = 0; i < sheetsCount; i++){
      var sheet = sheets[i]; 
      var sheetName = sheet.getName();
      Logger.log(sheetName); 
      if (sheetName.indexOf(hideSheetsContaining.toString()) !== -1){
        Logger.log("HIDE!");
        sheet.hideSheet();
      }
    }
  } else { 
    noMatchAlert();
  }
}

If I can't use an array of cells, how can I change this from a single string like it is to several different values? They would ideally be exact values, but a list of "Containing..." would work too.

Thanks!

Trey

Upvotes: 0

Views: 32

Answers (1)

ra89fi
ra89fi

Reputation: 1245

I edited your code slightly and added an array to hold strings to search and delete. Populate the array with strings and try.

function hideTW() {
  // My clumsy edit!
  var hideSheetsContaining = ['str1', 'str2', 'str3']; // put strings to search here
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();

  sheets.forEach(function(sheet) {
    var sheetName = sheet.getName();
    hideSheetsContaining.forEach(function(str) {
      if (sheetName.indexOf(str) !== -1) {
        Logger.log('HIDE-' + sheetName);
        sheet.hideSheet();
      }
    });
  });
}

Upvotes: 1

Related Questions