関数苦手なんでスクリプトでやってみた。
色をつけはずしする処理は重いうえ、それを繰り返し処理するので、データが多いと使いものにならないかな。
(ロジックを考えて繰り返し回数を抑制しようとしたがバグを生みそうだったので、全部色を外して、対象につける操作にしています)
javascript
1function onEdit(e) {
2 const col = e.range.getColumn();
3 if(col !== 2) { return; } //B列以外は何もしない
4 const row = e.range.getRow();
5 adjustColors(e.source, e.range.getSheet().getRange(row, col - 1).getValue());
6}
7function getPairs(activeSheet, needle) {
8 const checkRange = "A:B";
9 const vals = activeSheet.getRange(checkRange).getValues().map(function(e,i) { return [e[0], e[1], i+1];}).filter(function(e) { return e[0] === needle;});
10 return vals;
11}
12function adjustColors(activeSheet, needle) {
13 const colour = "red";
14 const targetCol = 2; //B
15 const sameIdPairs = getPairs(activeSheet, needle);
16 resetColorOfRowsCol(sameIdPairs.map(function(e) { return e[2]; }), targetCol); //全セルの色消す
17 const isAllEmpty = sameIdPairs.filter(function(e) { return e[1] === ""; }).length === sameIdPairs.length;
18 if(isAllEmpty) { return; } //着色しない
19 setColorOfRowsCol(sameIdPairs.filter(function(e){ return e[1] === "";}).map(function(e) { return e[2]; }), targetCol, colour);
20}
21function setColorOfRowsCol(rows, column, colour) {
22 rows.forEach(function(e) {
23 SpreadsheetApp.getActiveSheet().getRange(e, column).setBackground(colour);
24 });
25}
26function resetColorOfRowsCol(rows, column) {
27 rows.forEach(function(e) {
28 SpreadsheetApp.getActiveSheet().getRange(e, column).setBackground(null);
29 });
30}