简体   繁体   English

setBackgroundRGB不接受功能争论

[英]setBackgroundRGB not accepting function arguements

What I am trying to do 我要做什么

Make a function in google sheets that will allow me to select a cell and enter "=rgbcolour", select 3 separate cells containing numbers between 0 and 255 to act as rgb values so that the cell that I entered "=rgbcolour" into will turn the appropriate colour as per the 3 separate rgb inputs 在Google工作表中创建一个函数,允许我选择一个单元格并输入“ = rgbcolour”,选择3个包含0到255之间数字的单独的单元格作为rgb值,这样我输入“ = rgbcolour”的单元格就会变成根据3个独立的rgb输入输入适当的颜色

My Research 我的研究

setBackGroundRGB not accepting string This person ran into the same error put his desired result, his methods used and the solution to his problem would not seem to apply setBackGroundRGB不接受字符串此人遇到了同样的错误,他无法获得期望的结果,所使用的方法以及对该问题的解决方案

This setBackground() or setFontColor not working in GAS helped me try and figure out how to pass the cell, that the function would be entered into, into the setBackgroundRGB method. 这个在GAS中不起作用的setBackground()或setFontColor帮助我尝试找出如何将要输入该函数的单元格传递到setBackgroundRGB方法中。 I ended up using var cell = sheet.getActiveCell(); 我最终使用了var cell = sheet.getActiveCell(); Although I may have been unsuccessful in this endeavour which may be contributing to my problems 尽管我在这项工作中可能没有成功,但可能会导致我的问题

I have been using https://developers.google.com/apps-script/guides/sheets/functions#using_a_custom_function as a guide 我一直在使用https://developers.google.com/apps-script/guides/sheets/functions#using_a_custom_function作为指南

This is my code 这是我的代码

function RGBCOLOUR(r,g,b)
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getActiveCell();

  return cell.setBackgroundRGB(r,g,b);
}

this is my error 这是我的错误

Cannot find method setBackgroundRGB((class),(class),(class)). 找不到方法setBackgroundRGB((class),(class),(class))。 (line 7, file "RGB Colour Cell") (第7行,文件“ RGB彩色单元”)

This will not be possible. 这将是不可能的。 According to the documentation, the custom functions can only return value(s). 根据文档,自定义函数只能返回值。 Not manipulate the formatting. 不操纵格式。

https://developers.google.com/apps-script/guides/sheets/functions#return_values https://developers.google.com/apps-script/guides/sheets/functions#return_values

Someone please correct me if I'm wrong so that I can learn something new. 如果我错了,请有人纠正我,以便我可以学习新的东西。

Ok, seeing as what I want to do is not supported in the framework, this is the code I used instead 好的,看到框架中不支持我要执行的操作,这是我使用的代码

function onEdit()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var range = sheet.getRange("A3:R3");
  var colours = sheet.getRange("A4:R4");

  for (var x=1; x < range.getNumColumns(); x=x+3)
  {
    var cell = colours.getCell(1,x);
    var r = range.getCell(1,x).getValue();
    var g = range.getCell(1,x+1).getValue();
    var b = range.getCell(1,x+2).getValue();

    cell.setBackgroundRGB(r,g,b);
  }
}

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

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