简体   繁体   English

setBackGroundRGB 不接受字符串

[英]setBackGroundRGB not accepting string

The class setBackgroundRGB() works if I pass it a literalsetBackgroundRGB()如果我传递给它一个文字

setBackgroundRGB(255,255,255); 

but if I pass it a variable instead, it fails:但是如果我改为传递一个变量,它就会失败:

_Color = "255, 255, 255";

setBackgroundRGB(_Color); 

Does not work and returns an error不起作用并返回错误

`Cannot find method setBackgroundRGB(string)`

Do i need to do some kind of conversion here that I am not aware of?我是否需要在这里进行某种我不知道的转换?

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... 注意 - 这基本上就是上面的帖子 - split()将字符串转换为数组......

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;

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: 实际上API没有方法setBackgroundRGB(string),提供了一个方法setBackgroundRGB(Integer,Integer,Integer) ,但是,一个选项可以实现你所需要的,输入一个字符串是:

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

UPDATE 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: 为了获得向量可以应用几个改进,扩展一些示例,我们整合了注释中指出的一些改进,产生以下结果:

function setColorToRange() {
  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);
}

function getRGB(RGBString) {
  // Returns a vector of integers
  return RGBString.replace(/ /g, '').split(',').map(returnInt);
}

function returnInt(value) {
  return parseInt(value, 10);
}

Just another variant of the same approach using range and RGBstring as parameters : 使用范围RGBstring作为参数的相同方法的另一种变体:

function test(){ // to define 2 parameters
  var range = SpreadsheetApp.getActiveSheet().getRange('A1:B3');
  setColorToRange(range,'0, 255, 0');
}

function setColorToRange(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);
}

const rgb = { red : 224, green : 224, blue : 224 } const rgb = { 红色:224,绿色:224,蓝色:224}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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