简体   繁体   English

Google表格根据当前标签的单元格值按名称调用工作表

[英]Google Sheets call sheet by name based on cell value of current tab

I am trying to hide or unhide specific tabs by creating functions.我试图通过创建函数来隐藏或取消隐藏特定的选项卡。 The problem is the sheet name will change depending on who's using it.问题是工作表名称会根据使用它的人而改变。 Is it possible to get a sheet name based on the value of a cell on the current tab?是否可以根据当前选项卡上单元格的值获取工作表名称?

Example:例子:

function HideIncOne() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('G18').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('INCENTIVE #1'), true);
  spreadsheet.getActiveSheet().hideSheet();
};

I tried .getSheetByRange but obviously that didn't work.我试过 .getSheetByRange 但显然没有用。 So I'm trying to call the text from cell B4 of the current tab and make google sheets hide a sheet with the same name as the text in cell B4.所以我试图从当前选项卡的单元格 B4 中调用文本,并使谷歌工作表隐藏与单元格 B4 中文本同名的工作表。

  • Get the active sheet's B4 value获取活动工作表的B4
  • Get Sheet with that name using getSheetByName and hide it使用getSheetByName获取具有该名称的getSheetByName并隐藏它
function HideSheetInB4() {
  const spreadsheet = SpreadsheetApp.getActive();
  spreadsheet
    .getSheetByName(
      spreadsheet
        .getActiveSheet()
        .getRange('B4')
        .getValue()
    )
    .hideSheet();
}

Here's a simple way to put developer metadata into selected sheets这是将开发人员元数据放入选定工作表的简单方法

In this case I only wanted to mess with my sheets that are named with the string "Sheet" followed by 1 or more digits.在这种情况下,我只想弄乱以字符串“Sheet”命名的工作表,后跟 1 个或多个数字。

Hence the regex /Sheet\\d{1,}/ or /Sheet\\d+/因此正则表达式/Sheet\\d{1,}//Sheet\\d+/

function addingmetadatatosomesheet() {
  const ss = SpreadsheetApp.getActive();
  ss.getSheets().forEach((sh, i) => {
    if (sh.getName().match(/^Sheet\d{1,}/)) {
      sh.addDeveloperMetadata('shidx', i + 1);
    }
  });
}

This is how I read the meta data.这就是我读取元数据的方式。

function findSheetMetaData() {
  const ss = SpreadsheetApp.getActive();
  ss.getSheets().forEach(sh => {
    let v = sh.createDeveloperMetadataFinder().withKey('shidx').find();
    if (v && v.length > 0) {
      v.forEach(e => {
        Logger.log(`SheetName: ${sh.getName()} Metadata: ${e.getValue()}`);
      });
    }
  });
}

Execution Log Output:执行日志输出:

1:37:31 PM  Info    SheetName: Sheet01 Metadata: 7.0
1:37:31 PM  Info    SheetName: Sheet1 Metadata: 8.0
1:37:31 PM  Info    SheetName: Sheet2 Metadata: 9.0
1:37:32 PM  Info    SheetName: Sheet3 Metadata: 10.0
1:37:32 PM  Info    SheetName: Sheet4 Metadata: 11.0
1:37:32 PM  Info    SheetName: Sheet5 Metadata: 12.0
  • And this represent a solution that your casual user is not going to find easy to mess with.这代表了一种解决方案,您的临时用户不会轻易弄乱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google 表格根据不同选项卡的单元格值更改选项卡名称 - Google Sheets change tab name based on cell value of a DIFFERENT tab Google Sheets + Apps Script:如何根据单元格的值按名称删除工作表 - Google Sheets + Apps Script: How to delete a sheet by name based on the value of a cell 根据条件和单元格值将行复制到 Google 表格上的另一张表格 - Copy row to another sheet on Google Sheets based on criteria and cell value 如何根据google工作表中的单元格值切换到可变工作表 - how to switch to a variable sheet based on a cell value in google sheets 将基于单元格值的行数据复制到 Google 表格中的新工作表 - Copy row data based on cell value to new sheet in Google Sheets 如何根据Google表格中的单元格值命名Google云端硬盘文件夹 - How to name a Google Drive folder based on cell value in Google Sheets Google 表格 - 基于从范围中选择的值的名称表 - Google Sheets - Name sheet based on value selected from range 我可以使用脚本以当前数据作为选项卡名称复制和重命名 Google 表格中的工作表吗? - Can I use a script to copy and rename a sheet in Google Sheets with the current data as the tab name? 根据单元格重命名 Google 工作表标签 - Rename a Google sheet tab based on cell Google 表格:如何将电子表格文件中的单个表格另存为 PDF,其中文件名基于特定单元格内容? - Google sheets: How to save a single sheet in a spreadsheet file as PDF, where the file name is based on a certain cell content?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM