Google apps script 用于电子表格的谷歌应用程序脚本

Google apps script 用于电子表格的谷歌应用程序脚本,google-apps-script,Google Apps Script,我需要根据其他单元格的值为区域单元格添加背景色,但此区域有不同的值,例如: 1 X 2 1 X | 2 当检查值为2的las单元格时,范围单元格必须是独立的颜色,即,只有三个单元格必须具有绿色背景色,因为它们的值与其他单元格的值相同,且为红色 我有以下代码: function test() { var libro = SpreadsheetApp.getActiveSpreadsheet(); var range_input = libro.getRange("B3:E3"); va

我需要根据其他单元格的值为区域单元格添加背景色,但此区域有不同的值,例如:

1 X 2 1 X | 2

当检查值为2的las单元格时,范围单元格必须是独立的颜色,即,只有三个单元格必须具有绿色背景色,因为它们的值与其他单元格的值相同,且为红色

我有以下代码:

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')
  }  
} 
但问题是,这不起作用,因为只有当所有单元格的值与最后一个单元格的值相同时,该行才是绿色的,这意味着尽管第三个单元格的值正确,但整行变为红色


Thx提前。

range\u input.getValues()
返回4个单元格的值数组,而
target\u 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]);
} 
功能测试(){
var libro=SpreadsheetApp.getActiveSpreadsheet();
var range_input=libro.getRange(“B3:E3”);
var target_cell=libro.getRange(“G3”).getValue();//保存值
//获取范围的值([0]仅检索第一行值)
var input_values=范围_input.getValues()[0];
//获取范围的背景([0]仅检索第一行值)
var backgrounds=range_input.getBackgroundColors()[0];
//循环行中的值,对照单元格进行检查。
对于(变量i=0;i
range\u input.getValues()
返回4个单元格的值数组,而
target\u 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]);
} 
功能测试(){
var libro=SpreadsheetApp.getActiveSpreadsheet();
var range_input=libro.getRange(“B3:E3”);
var target_cell=libro.getRange(“G3”).getValue();//保存值
//获取范围的值([0]仅检索第一行值)
var input_values=范围_input.getValues()[0];
//获取范围的背景([0]仅检索第一行值)
var backgrounds=range_input.getBackgroundColors()[0];
//循环行中的值,对照单元格进行检查。
对于(变量i=0;i
Thx太多了,我不得不测试它并开始工作。我正在学习javascript,在这方面我很在行,有时我会陷入思想困境:-PNo问题,这个网站是提问的最佳场所。Thx太多了,我已经测试了它并开始工作。我正在学习javascript,在这方面我非常不在行,有时我会在思想上陷入困境:-PNo问题,这个网站是提问的最佳场所。