[英]Google App Script for spreadsheet
我需要根据其他单元格的值为范围单元格添加背景色,但是此范围具有不同的值,例如:
1 X 2 1 XX | 2
范围单元格在检查具有值2的las单元格时必须是独立的颜色,即,只有单元格3必须具有绿色背景色,因为该值与和单元格中的其他单元格的颜色相同。
我有以下代码:
function test() {
var libro = SpreadsheetApp.getActiveSpreadsheet();
var range_input = libro.getRange("B3:E3");
var target_cell = libro.getRange("G3");
if (range_input.getValues()==target_cell.getValue())
{
range_input.setBackgroundColor('#58FA58');
}
else
{
range_input.setBackgroundColor('#FA5858')
}
}
但是问题在于,这行不通,因为只有当所有值单元格都具有与最后一个单元格相同的值时,行才是绿色,这意味着尽管单元格3具有正确的值,但整行都变成红色。
提前谢谢。
range_input.getValues()
返回4个单元格的值数组,而target_cell.getValue()
返回1个值。
function test() {
var libro = SpreadsheetApp.getActiveSpreadsheet();
var range_input = libro.getRange("B3:E3");
var target_cell = libro.getRange("G3").getValue();//Save the value
//Gets the values of the range ([0] to retrieve just the first row of values)
var input_values = range_input.getValues()[0];
//Gets the backgrounds of the range ([0] to retrieve just the first row of values)
var backgrounds = range_input.getBackgroundColors()[0];
//Loop through values in the row, checking against the cell.
for(var i = 0; i < input_values.length; i += 1){
if(input_values[i] === target_cell) {
backgrounds[i] = '#58FA58';
} else {
backgrounds[i] = '#FA5858';
}
}
//Put the row of backgrounds back in an array to make it 2d and set the backgrounds
range_input.setBackgroundColors([backgrounds]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.