根据下拉菜单中的选择更改单元格颜色

Change cell color based on selection from dropdown menu

这是我正在编写的脚本,旨在帮助我了解如何编写更复杂的脚本。

在我的电子表格中,有一列包含值列表 ("testRange"、"testRangeValues")。我还有一个下拉列表 ("testCell","testCellValue")。下拉列表包含一些在 "testRange" 列表中的值,一些不在列表中。

我希望我的脚本以这样的方式运行:当我 select 下拉列表中的值与 testRange 列表中的值相匹配时,testRange 列表中相应值的背景颜色会发生变化变红。

function onEdit3(e) {

var testRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A4:A8");
var testRangeValues = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A4:A8").getValues();
var testCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("C4");
var testCellValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("C4").getValue(); 

for(var i = 0;i<testRange.length;i++){
if(testCellValue==testRangeValues[i]){
testRange[i].setBackground("Red");
}
}



}

目前没有颜色变化。我想我不太确定如何正确编写 for loop/if 语句以达到预期效果。有没有一种方法可以在我的电子表格中使用某种条件格式公式来做到这一点?

  • 您想更改"A4:A8"范围内单元格的背景颜色,使"C4"的下拉列表的值相同。
  • 您想使用 Google Apps 脚本实现此目的。
  • 在您的脚本中,您不想使用事件对象。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

修改点:

  • sheet对象可以被var sheet = SpreadsheetApp.getActiveSheet();写入。这样,您可以使用 sheet 来检索范围。
  • sheet.getRange("A4:A8")sheet.getRange("C4")可以使用一次。
  • 在这种情况下,范围对象不能在循环中使用,因为testRange.length returns null.

当以上几点反映到你的脚本中,就会变成下面这样。

模式 1:

在此模式中,只有所选值的背景颜色更改为红色。 So for example, when "A" is selected, the background color of "A" is changed to the red color. And when "C" is selected, the background color of "C" is changed to the red color.在这种情况下,"A" 的背景颜色被重置。

修改后的脚本:

function onEdit3(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var testRange = sheet.getRange("A4:A8");
  var testRangeValues = testRange.getValues();
  var testCell = sheet.getRange("C4");
  var testCellValue = testCell.getValue();

  var backgroundColors = testRangeValues.map(function([a]) {return [a == testCellValue ? "Red" : ""]});
  testRange.setBackgrounds(backgroundColors);
}

模式二:

在此模式中,保存了所选值的背景色。 So for example, when "A" is selected, the background color of "A" is changed to the red color. And when "C" is selected, the background color of "C" is changed to the red color.在这种情况下,"A" 的背景色保持红色。

修改后的脚本:

function onEdit3(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var testRange = sheet.getRange("A4:A8");
  var testRangeValues = testRange.getValues();
  var testCell = sheet.getRange("C4");
  var testCellValue = testCell.getValue();

  for (var i = 0; i < testRangeValues.length; i++) {
    if (testRangeValues[i] == testCellValue) {
      sheet.getRange("A" + (4 + i)).setBackground("Red");
      break;
    }
  }
}

现代javascript:

您可以使用现代 javascript 语法。为此,您需要 enable V8 runtime。之后你可以

/**
 *
 * @param {GoogleAppsScript.Events.SheetsOnEdit} e
 */
const onEdit = e => {
  const sheet = e.range.getSheet();
  const value = e.range.getValue();
  if (sheet.getName() == 'Sheet1' && e.range.getA1Notation() === 'C2') {
    const range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 1);
    range.setBackgrounds(
      range.getValues().map(row => [row[0] === value ? 'red' : ''])
    );
  }
};

参考文献:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。