[英]Google Apps Script - Get sheets by tab color
I am curious if there is a way to get sheets by the color of their tabs within Google Script?我很好奇是否有办法通过 Google Script 中标签的颜色来获取工作表?
I currently use this array to grab the sheets by name:我目前使用此数组按名称抓取工作表:
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());
}
}
Timesheets are constantly being created and deleted, and I would have to update this array every time that happens.时间表不断被创建和删除,每次发生这种情况时我都必须更新这个数组。 However, all the timesheet tabs are orange (#ff9900) so I figured if I could pull tabs by that color, it wouldn't matter what the name was and in the long run, make the spreadsheet a lot more dynamic regardless of turnover and sheet names.
但是,所有时间表标签都是橙色 (#ff9900),所以我想如果我可以按那种颜色拉标签,名称是什么并不重要,从长远来看,无论营业额如何,都可以使电子表格更加动态工作表名称。
Thank you for any help!感谢您的任何帮助!
Try something like this尝试这样的事情
function addRow() {
SpreadsheetApp.openById('xxxxxxxxxxxxxxxyPz4').getSheets()
.filter(function (sh) {
return sh.getTabColor() == '#ff9900'; //change to match color
}).forEach(function (sh) {
sh.insertRowAfter(sh.getLastRow());
})
}
This is easily done using already available methods:使用现有的方法很容易做到这一点:
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 */
});
}
References参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.