简体   繁体   English

电子表格的Google App脚本

[英]Google App Script for spreadsheet

I need to put a background color to the range cells depending the value of other cell, but this range have a diferents values, for example: 我需要根据其他单元格的值为范围单元格添加背景色,但是此范围具有不同的值,例如:

1 X 2 1 XX | 1 X 2 1 XX | 2 2

The range cells must be independent colors when check the las cell that have value 2, namely, only the cell three must have a green background color because is the same value that the and the others in a red color. 范围单元格在检查具有值2的las单元格时必须是独立的颜色,即,只有单元格3必须具有绿色背景色,因为该值与和单元格中的其他单元格的颜色相同。

I have this code: 我有以下代码:

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')
  }  
} 

But the problem is that not work because the row only is green when all values cells have the same value that the last cell, this means that the entire row become red although the cell three have the correct value. 但是问题在于,这行不通,因为只有当所有值单元格都具有与最后一个单元格相同的值时,行才是绿色,这意味着尽管单元格3具有正确的值,但整行都变成红色。

Thx in advance. 提前谢谢。

range_input.getValues() returns an array of arrays of values for the 4 cells, while target_cell.getValue() returns 1 value. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM