首页 > 解决方案 > 未能在 Google 表格条件格式中创建 3 个单独的色阶范围

问题描述

所以我试图创建3个单独的色标,这样0-1范围内的数字从红色到白色变为(比如说)绿色,2-5从红色到白色变为青色,6-90从红色变为白色到紫色。目的是表明接近 0、2 和 6 的值在它们各自的范围内都是“坏的”,而接近每个各自范围顶部的值是“好”的。

我开始只是去:

条件格式 -> 色标 -> 选择 Minpoint、Midpoint 和 Maxpoint Number 以及颜色。-> 向下滚动 -> 添加另一个规则

...直到我有了我的 3 条规则。这一切看起来都可以工作,因为范围不重叠。

不幸的是,输入到定义范围内的所有数字仅根据其中一个规则进行格式化。所以例如。我第一次尝试,0-70之间的数字都是红色的,数字70-90逐渐变成了预期的紫色。

我尝试根据此处给出的示例在脚本(我完全不熟悉)中再次构建它:https://developers.google.com/apps-script/reference/spreadsheet/condit ...它似乎与我第一次尝试的结果完全相同,所以现在所有数字都根据值 0-1 的规则进行格式化。所以即使例如。50,在其范围内应该是白色的,是完全绿色的。

这是我的脚本尝试,如果有人可以帮助我使其正常工作,我将不胜感激。

function myFunction() {

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("E3:BR500");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberBetween (6,90)
    .setGradientMaxpointWithValue("#d600ff", SpreadsheetApp.InterpolationType.NUMBER, "90")
    .setGradientMidpointWithValue("#ffffff", SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue("#ff0000", SpreadsheetApp.InterpolationType.NUMBER, "6")
    .setRanges([range])
    .build();

var rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberBetween(2,5)
    .setGradientMaxpointWithValue("#00b8ff", SpreadsheetApp.InterpolationType.NUMBER, "5")
    .setGradientMidpointWithValue("#ffffff", SpreadsheetApp.InterpolationType.NUMBER, "3.5")
    .setGradientMinpointWithValue("#ff0000", SpreadsheetApp.InterpolationType.NUMBER, "2")
    .setRanges([range])
    .build();

var rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberBetween(0,1)
    .setGradientMaxpointWithValue("#00ff9f", SpreadsheetApp.InterpolationType.NUMBER, "1")
    .setGradientMidpointWithValue("#ffffff", SpreadsheetApp.InterpolationType.NUMBER, "0.7")
    .setGradientMinpointWithValue("#ff0000", SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
  
}

标签: google-apps-scriptgoogle-sheetsconditional-formatting

解决方案


我相信内置条件格式不适合您尝试做的事情。一种可能的解决方案是跳过内置条件格式并使用 script 和 use 计算每个值的颜色十六进制setBackgrounds()


推荐阅读