Skip to content Skip to sidebar Skip to footer

Setbackgroundrgb Not Accepting String

The class setBackgroundRGB() works if I pass it a literal setBackgroundRGB(255,255,255); but if I pass it a variable instead, it fails: _Color = '255, 255, 255'; setBackgroundR

Solution 1:

You need to pass an array. The following should work:

_Color = [255, 255, 255];
setBackgroundRGB(_Color); 

Note - this is essentially what the post above is doing -- split() converts strings to arrays...

Solution 2:

Indeed the API does not have a method setBackgroundRGB(string), provides a method setBackgroundRGB(Integer, Integer, Integer), however, an option to achieve what you need, having a string as input is:

function setColorToRange() {
  var colorRGB = '0, 255, 0';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();      
  varrange = sheet.getRange('A1:B3');
  range.setBackgroundRGB.apply(range, colorRGB.split(', '));
}

UPDATE

To get the vector can be applied several improvements, widening a little the example presented, we integrate some of the improvements indicated in the comments, resulting in the following:

functionsetColorToRange() {
  var colorRGB = '0, 255, 0';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();      
  var range = sheet.getRange('A1:B3');
  var arrColorRGB = getRGB(RGBString);
  range.setBackgroundRGB.apply(range, arrColorRGB);
}

functiongetRGB(RGBString) {
  // Returns a vector of integersreturnRGBString.replace(/ /g, '').split(',').map(returnInt);
}

functionreturnInt(value) {
  returnparseInt(value, 10);
}

Solution 3:

You are passing a string. It takes integers.

Try This:

_Color = 255,255,255

I doubt that will work however. You may need to do multiple variables for each

red = 255;green = 255;blue = 255;

Solution 4:

Just another variant of the same approach using range and RGBstring as parameters :

functiontest(){ // to define 2 parametersvar range = SpreadsheetApp.getActiveSheet().getRange('A1:B3');
  setColorToRange(range,'0, 255, 0');
}

functionsetColorToRange(range,RGBString) {
  var colorRGB = RGBString.replace(/ /g,'').split(',');// first remove spaces if present then split on comma only to get the array of integers
  range.setBackgroundRGB.apply(range, colorRGB);
}

Solution 5:

const rgb = { red : 224, green : 224, blue : 224 }

....setBackgroundRGB(rgb.red, rgb.green, rgb.blue);

Post a Comment for "Setbackgroundrgb Not Accepting String"