繁体   English   中英

Google Apps Script - 按标签颜色获取工作表

[英]Google Apps Script - Get sheets by tab color

我很好奇是否有办法通过 Google Script 中标签的颜色来获取工作表?

我目前使用此数组按名称抓取工作表:

function AddRow() {
  var ss = SpreadsheetApp.openById('spreadsheetId');
  var contractorSheets,i,L,sheet;
contractorSheets = [
  "Employee 1's Timesheet",
  "Employee 2's Timesheet",
  "Employee 3's Timesheet",
  "Employee 4's Timesheet",
  "Employee 5's Timesheet",
  "Employee 6's Timesheet",
  "Employee 7's Timesheet"
]
L = contractorSheets.length;
for (i=0;i<L;i++){
  sheet = ss.getSheetByName(contractorSheets[i]);
  sheet.insertRowAfter(sheet.getLastRow());
  }
}

时间表不断被创建和删除,每次发生这种情况时我都必须更新这个数组。 但是,所有时间表标签都是橙色 (#ff9900),所以我想如果我可以按那种颜色拉标签,名称是什么并不重要,从长远来看,无论营业额如何,都可以使电子表格更加动态工作表名称。

感谢您的任何帮助!

尝试这样的事情

function addRow() {
SpreadsheetApp.openById('xxxxxxxxxxxxxxxyPz4').getSheets()
    .filter(function (sh) {
        return sh.getTabColor() == '#ff9900'; //change to match color
    }).forEach(function (sh) {
        sh.insertRowAfter(sh.getLastRow());
    })
}

使用现有的方法很容易做到这一点:

function getSheetsWithTabColor_(colorHex, allSheets) {
  if (!allSheets || !allSheets.length)
    allSheets = SpreadsheetApp.getActive().getSheets();
  return allSheets.filter(function (sheet) { return sheet.getTabColor() === colorHex; });
}

function foo() {
  const sheets = SpreadsheetApp.getActive().getSheets();
  const timesheets = getSheetsWithTabColor_("some color hex", sheets);
  timesheets.forEach(function (sheet) {
    /** do your stuff that should be done on sheets with this color tab */
  });
}

参考

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM