簡體   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