setBackGroundRGB不接受字符串 [英] setBackGroundRGB not accepting string

查看:84
本文介绍了setBackGroundRGB不接受字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

 <$ c $   C> setBackgroundRGB(255,255,255); 

但是如果我将它传递给一个变量,它会失败:

  _Color =255,255,255; 

setBackgroundRGB(_Color);

不起作用并返回错误

`找不到方法setBackgroundRGB(string)`

需要在这里做一些我不知道的转换?

解决方案

的确,API没有方法setBackgroundRGB字符串)提供了一种方法 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(','));
}

更新
$ b

为了得到这个向量可以应用几个改进,稍微扩展一下所示的例子,我们整合了一些在注释中指出的改进,导致如下结果:

 函数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);
}

函数getRGB(RGBString){
//返回一个整数向量
return RGBString.replace(/ / g,'').split(' 。,')映射(returnInt);
}

函数returnInt(value){
return parseInt(value,10);
}


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";

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?

解决方案

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();      
  var range = 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:

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

这篇关于setBackGroundRGB不接受字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆