使用 Google Apps 脚本更改单元格的颜色(基于 "vLookup")

Using Google Apps Script to Change the Color of a Cell (based on a "vLookup")

我有一个会议安排 sheet,它是一个包含所有演示文稿标题的下拉列表。选择演示文稿后,我希望它自动设置为 "type" 演示文稿的颜色。在另一个 sheet.

上发现演示文稿类型与演示文稿名称相邻

我可以根据单元格的内容设置单元格颜色,但我希望在静态的 2 列 table(在另一个位置)和一个相邻值返回。

function onEdit(event) {

var ss = event.range.getSheet();

if (ss.getName() !== "Sheet2") return;

var changedCell = event.source.getActiveRange();
var c = event.value; 
var color = 'white'; 
var data=getRange('G1:H5').getValues();

for(i=0;i<data.length;i++){
if (data[i][0]==c){
  return data[i][1];
}
}

    switch (data[i][1]) {
        case "Math":
            color = "MediumSeaGreen";
            break;
        case "Science":
            color = "Orange";
            break;
        case "History":
            color = "DarkGray";
            break;
        case "English":
            color = "Gold";
            break;
        case "Spanish":
            color = "LightCoral";
            break;
        default:
            break;
    }

changedCell.setBackground(color);  
}

Image of Spreadsheet

我猜这就是你想要的:

function onEdit(event) {
  var ss = event.range.getSheet();
  if (ss.getName() !== "Sheet2") return;
  var changedCell = event.source.getActiveRange();
  var c = event.value; 
  var color = 'white'; 
  var data=ss.getRange('G1:H5').getValues();
  for(i=0;i<data.length;i++){
    if (data[i][0]==c){
      switch (data[i][1]) {
        case "Math":
          color = "MediumSeaGreen";
          break;
        case "Science":
          color = "Orange";
          break;
        case "History":
          color = "DarkGray";
          break;
        case "English":
          color = "Gold";
          break;
        case "Spanish":
          color = "LightCoral";
          break;
        default:
          break;
      }
      changedCell.setBackground(color);
    }
  } 
}

Note: I haven't tested it.