问题描述
我正在制作一张名为“TEAM SETUP”的表格
我有一个单元格范围内的姓名列表:B29:E39,这是我在“假日”时将姓名作为工作人员放入的范围
然后我将姓名(部署员工)分配到范围内的不同角色:B2:S27
我想突出显示 B2:S27 范围内的任何单元格,其中值也存在于 B29:E39 中,以向我表明我已部署了一名“休假”员工
无论这是否可以通过脚本或条件格式来实现,如果有人能帮我解决这个问题,我将不胜感激。
解决方法
通过脚本执行此操作相当简单。请参阅下面的我的脚本:
代码:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
// Define ranges to only continue the function when edited cell is in there
// This is to prevent unnecessary execution time as we have quotas
var dataRange = { // B2:S27
top : 2,bottom : 27,left : 2,right : 19
};
var holidayRange = { // B29:E39
top : 29,bottom : 39,right : 5
}
// if edited cell is in data or holiday range
if(isInRange(e.range,dataRange) || isInRange(e.range,holidayRange)) {
var data = sheet.getRange("B2:S27").getValues();
// To filter only non blank cells,add filter(Boolean)
var holiday = sheet.getRange("B29:E39").getValues().flat().filter(Boolean);
data.forEach(function (row,i) {
row.forEach(function (cell,j) {
if (holiday.includes(cell))
sheet.getRange(i + 2,j + 2).setBackground("red");
// set to white if it turns out the value is now not found
else
sheet.getRange(i + 2,j + 2).setBackground("white");
});
});
}
}
function isInRange(cell,range) {
var startRow = range.top;
var endRow = range.bottom;
var startColumn = range.left;
var endColumn = range.right;
return cell.getRow() >= startRow && cell.getRow() <= endRow &&
cell.getColumn() >= startColumn && cell.getColumn() <= endColumn;
}