我想知道如何根據背景顏色自動反轉單元格值的字體顏色以使其可讀。
我使用以下腳本在將十六進制顏色代碼粘貼到給定單元格時自動設定背景顏色如何將單元格更改為 Google 電子表格中單元格的十六進制值的顏色?,這里有樣品表
function onEdit(e) {
r = e.range;
if(r.getSheet().getSheetName() == "colors"){ //the sheet I want to apply this to is called colors
var rows = r.getNumRows();
var columns = r.getNumColumns();
var colors = [] //this is our 2 dimensional array of colors in case you copy and paste a large amount of colors
for (var i = 1; i <= rows; i ) { //go down each row
var row = [] //create a new row array to clear the old one when we go down a row
for (var j = 1; j <= columns; j ) { //then go across each column
row.push(r.getCell(i,j).getValue()) //put together the row of colors
}
colors.push(row); //insert our row of colors so we can go down the next row
}
r.setBackgrounds(colors) //batch update in case you update many colors in one copy and paste otherwise it will be very slow
}
}
但剩下的問題是字體沒有顯示在深色背景單元格上。
我還發現了這個相關的問題如何根據背景顏色決定白色或黑色的字體顏色?. 那些帶有函式的 Javascript 特定答案,但我無法讓它們在 GAS 中使用上述腳本。
JavaScript 代碼 1
JavaScript 代碼 2
JavaScript 代碼 3
JavaScript 代碼 4
JavaScript 代碼 5
JavaScript 代碼 6
我還查看了setFontColors(colors)的檔案,發現我們可以使用r.setFontColors(colors)上面腳本中的方法。我嘗試呼叫上面的 JavaScript 代碼 1 到 6,但沒有成功。
例如,我已經基于JavaScript 代碼 3嘗試過這種方式:
function onEdit(e) {
r = e.range;
if(r.getSheet().getSheetName() == "colors"){ //the sheet I want to apply this to is called colors
var rows = r.getNumRows();
var columns = r.getNumColumns();
var colors = [] //this is our 2 dimensional array of colors in case you copy and paste a large amount of colors
for (var i = 1; i <= rows; i ) { //go down each row
var row = [] //create a new row array to clear the old one when we go down a row
for (var j = 1; j <= columns; j ) { //then go across each column
row.push(r.getCell(i,j).getValue()) //put together the row of colors
}
colors.push(row); //insert our row of colors so we can go down the next row
}
r.setBackgrounds(colors) //batch update in case you update many colors in one copy and paste otherwise it will be very slow
r.setFontColors(lum([111, 22, 255]));
}
}
function lum(rgb) {
var lrgb = [];
rgb.forEach(function(c) {
c = c / 255.0;
if (c <= 0.03928) {
c = c / 12.92;
} else {
c = Math.pow((c 0.055) / 1.055, 2.4);
}
lrgb.push(c);
});
var lum = 0.2126 * lrgb[0] 0.7152 * lrgb[1] 0.0722 * lrgb[2];
return (lum > 0.179) ? '#000000' : '#ffffff';
}
我錯過了什么?
感謝您的見解!
uj5u.com熱心網友回復:
我相信你的目標如下。
- 將十六進制值放入單元格時,您希望使用輸入的十六進制值設定背景顏色。那時,您希望根據背景顏色為每個單元格設定字體顏色。
改裝要點:
在您的腳本中,我認為 的值
colors可以通過r.getValues(). 在這種情況下,不需要使用 for 回圈。從
r.setFontColors(lum([111, 22, 255]));,當你想設定相同的字體顏色到范圍時,你可以修改如下。從
r.setFontColors(lum([111, 22, 255]));到
r.setFontColor(lum([111, 22, 255]));
但是,根據您的腳本,我認為您可能希望按每種背景顏色設定字體顏色。如果我的理解是正確的,下面的修改如何?
修改后的腳本:
// I modified this function.
function onEdit(e) {
var r = e.range;
if (r.getSheet().getSheetName() == "colors") {
var colors = r.getValues();
r.setBackgrounds(colors);
var fonrColors = r.getBackgroundObjects().map(r => r.map(c => {
var obj = c.asRgbColor();
return lum([obj.getRed(), obj.getGreen(), obj.getBlue()]);
}))
r.setFontColors(fonrColors);
}
}
// This is from your script.
function lum(rgb) {
var lrgb = [];
rgb.forEach(function (c) {
c = c / 255.0;
if (c <= 0.03928) {
c = c / 12.92;
} else {
c = Math.pow((c 0.055) / 1.055, 2.4);
}
lrgb.push(c);
});
var lum = 0.2126 * lrgb[0] 0.7152 * lrgb[1] 0.0722 * lrgb[2];
return (lum > 0.179) ? '#000000' : '#ffffff';
}
在這次修改中,為了將十六進制轉換為RGB,我使用了
getBackgroundObjects(). 當然,您可以使用 Javascript 進行轉換。如果您不想使用getBackgroundObjects(),請檢查此執行緒在此修改中,當您將十六進制值放入單元格時,使用十六進制值設定背景顏色,并使用
lum腳本功能根據背景顏色設定字體顏色。
參考:
- 獲取背景物件()
- 相關執行緒。
- RGB 到十六進制和十六進制到 RGB
uj5u.com熱心網友回復:
的必需引數setFontColors是一個二維陣列
嘗試r.setFontColors(lum([111, 22, 255]));改成如下代碼:
const fontColor = lum([111, 22, 255]);
const fontColorsRow = new Array(columns).fill(fontColor);
const fontColors = new Array(rows).fill(fontColorsRow);
r.setFontColors(fontColors);
參考:
setFontColors(顏色)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395418.html
