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
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
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. I ended up using
var cell = sheet.getActiveCell();
Although I may have been unsuccessful in this endeavour which may be contributing to my problemsI have been using https://developers.google.com/apps-script/guides/sheets/functions#using_a_custom_function as a guide
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)). (line 7, file "RGB Colour Cell")
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
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.