1

他のセルの値に応じて範囲セルに背景色を付ける必要がありますが、この範囲には異なる値があります。次に例を示します。

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 が正しい値を持っているにもかかわらず、行全体が赤くなることを意味します。

事前にThx。

4

1 に答える 1

1

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]);
} 
于 2013-01-13T20:43:06.240 に答える