0

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 problems

I 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")

Community
  • 1
  • 1
  • Do you have an example page that shows the issue? Providing an example/link may make it more likely to get a response. – Liam Dec 07 '15 at 11:20
  • @Liam Here is the link to the spreadsheet [link](https://docs.google.com/spreadsheets/d/192FTycZNWqGqtEc1Y3mlFCq2guiYyDb63M4pWsPRZJo/edit?usp=sharing) You can see that at A3, B3 and C3 there would be numbers, the idea is that i'd enter the formula into A4 so that A4 would take on the colour of the A3, B3, C3 values – FactualOrc Dec 07 '15 at 11:25
  • how would you expect it to work ? why a custom function ? looking at your SS I don't see where you try using your function (which in any case can't work as it is) – Serge insas Dec 07 '15 at 11:50

2 Answers2

2

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.

Karan
  • 1,187
  • 2
  • 9
  • 16
0

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