[英]How to set background colour of a cell based on another cell in Google Spreadsheet?
[英]Is it possible to set the background color of a google spreadsheet cell with a formula?
我知道條件格式,但我希望能夠使用公式指定顏色。 我正在設計一些褪色算法並計算電子表格中的 RGB 值。 我想將我的 3 個 R、G 和 B 值放在列中,並讓第 4 列顯示顏色。
不使用公式,應用程序腳本上的onEdit
可以做到這一點,但效率不高:
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function onEdit(){
var RGBVals = SpreadsheetApp.getActiveSheet().getDataRange().getValues(),
backSet = [],
currLin;
for( lin in RGBVals ){
currLin = RGBVals[ lin ];
backSet.push([rgbToHex(currLin[0], currLin[1], currLin[2])]);
}
SpreadsheetApp.getActiveSheet().getRange(1, 4, backSet.length).setBackgrounds(backSet);
}
注:A、B、C 列中的 R、G、B,從第 1 行開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.