Google Apps脚本 - 按标签颜色获取图纸

问题描述 投票:-1回答:2

我很好奇是否有办法通过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),所以我想如果我能用这种颜色拉标签,那么名称是什么并不重要,从长远来看,无论营业额如何,都可以使电子表格更加动态。工作表名称。

感谢您的任何帮助!

javascript google-apps-script google-sheets
2个回答
0
投票

尝试这样的事情

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

0
投票

这可以使用已有的方法轻松完成:

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 */
  });
}

参考

© www.soinside.com 2019 - 2024. All rights reserved.