是否可以使用公式设置Google电子表格单元格的背景颜色? [英] Is it possible to set the background color of a google spreadsheet cell with a formula?
本文介绍了是否可以使用公式设置Google电子表格单元格的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我了解条件格式,但我希望能够使用公式指定颜色.我正在设计一些颜色淡入淡出算法,并在电子表格中计算RGB值.我想将3个R,G和B值放入列中,并让第4列显示颜色.
I know about conditional formatting, but I'd like to be able to specify the color with the formula. I'm designing some color fade algorithms and am calculating the RGB values in a spreadsheet. I'd like to put my 3 R, G, and B values in columns and have the 4th column show the color.
推荐答案
不带公式,Apps脚本上的onEdit
可以做到,但效率不高:
Not with formulas, an onEdit
on Apps Script could do that, but wouldn't be efficient:
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行开始.
Note: R,G,B in A,B,C column, starting in ROW 1.
这篇关于是否可以使用公式设置Google电子表格单元格的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文