根据单元格值隐藏工作表 [英] Hide sheet(s) based on cell value

查看:95
本文介绍了根据单元格值隐藏工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是全新的谷歌床单。我曾经有过使用VB脚本的经验,可以根据单元格的值来隐藏表单,但我希望在Google表单中获得相同的功能。



一个简单的代码例子会很棒!

解决方案

打开一个新的Google工作表,添加一个新工作表,以便有一个 Sheet1和Sheet2,然后进入工具>脚本编辑器。在编辑器中,粘贴以下内容。

  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();
}
}

现在需要设置一个触发器。点击脚本编辑器中的图标,看起来像放大镜和时钟之间的交叉。从那里,您可以告诉脚本编辑器,您想在编辑时从电子表格中运行您的功能。回到Sheet1,在B2单元格中输入2,保存并观看Sheet2消失。您可能会被要求授予脚本修改您的工作表的权限。 将2更改为3,保存并重新观看。


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.

解决方案

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.

这篇关于根据单元格值隐藏工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆