【问题标题】:Change cell color based on selection from dropdown menu根据下拉菜单中的选择更改单元格颜色
【发布时间】:2020-02-15 02:28:27
【问题描述】:

这是我正在编写的脚本,以帮助我弄清楚如何编写更复杂的脚本。

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

我希望我的脚本能够以这样一种方式运行,即当我从下拉列表中选择一个与 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 循环/if 语句以达到预期的效果。有没有办法在我的电子表格中使用某种条件格式公式来做到这一点?

【问题讨论】:

    标签: for-loop google-apps-script google-sheets conditional-formatting


    【解决方案1】:
    • 您想更改“A4:A8”范围内单元格的背景颜色,以使“C4”的下拉列表的值相同。
    • 您希望使用 Google Apps 脚本实现此目的。
    • 在您的脚本中,您不想使用事件对象。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    修改点:

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

    当以上几点反映到你的脚本中时,它变成如下。

    模式一:

    在此模式中,只有选定值的背景颜色更改为红色。 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);
    }
    

    模式 2:

    在此模式中,将保存所选值的背景颜色。 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' : ''])
        );
      }
    };
    

    参考资料:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2021-09-17
    • 2012-08-22
    • 2023-02-13
    相关资源
    最近更新 更多