google-apps-script - 未能在 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);
}
解决方案
我相信内置条件格式不适合您尝试做的事情。一种可能的解决方案是跳过内置条件格式并使用 script 和 use 计算每个值的颜色十六进制setBackgrounds()
。
推荐阅读
- django - django rest_framework 相对于 AJAX 和 JsonResponce 的优势
- json - JSON 模式条件语句
- html - 如何在带有边框图像和阴影的html5画布上显示完整图像?
- javascript - 如何首先呈现搜索项目和所有匹配项目 JavaScript?
- android - API> 25 中的前台服务在应用程序运行时是否需要通知(可见)
- html - 在网页的一行中打印两个不同的东西
- python - 如何在不使用python循环的情况下获得循环?
- mysql - 如何在 mysql 中搜索嵌套的 json 查询?
- django - Django 搜索查询功能,如 Django admin 或 Django rest 框架
- python-3.x - 如何设置用户可以使用 Kivy DragBehavior 拖动项目的区域?