Hide sheet(s) based on cell value

2019-05-27 02:47发布

I'm literally brand new to google sheets. I've had experience in excel with VB Script to hide sheets based on a cell's value, but I would like to get this same functionality to google sheets.

A simple code example would be wonderful! I'll expand on that from there.

1条回答
Deceive 欺骗
2楼-- · 2019-05-27 02:49

Open a new Google sheet, add a new sheet so that there is a "Sheet1" and a "Sheet2" then go to Tools > Script Editor. In the editor, paste the following.

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Sheet1");
  var sheet2 = ss.getSheetByName("Sheet2");

  var cell1 = sheet1.getRange('B2');

  if (cell1.getValue() == 2) {
    sheet2.hideSheet();
  }

  if (cell1.getValue() == 3) {
    sheet2.showSheet();
  }
}

You will need to set up a trigger now. Click the icon in the Script Editor that looks like a cross between a magnifying lens and a clock. From there, you can tell the script editor that you want to run your function from the spreadsheet on edit. You will probably be asked to give permission for the script to modify your sheet.

Go back to Sheet1, enter 2 in the cell B2, save, and watch Sheet2 disappear. Change the 2 to a 3, save, and watch it reappear.

查看更多
登录 后发表回答