0

I want to create a custom function for a Google Spreadsheet which can set background color of the cell according to the value of given cell as a input. enter image description here

F3 should become RED when I write this function.

I have tried this script so far:

function setFillColors(input) {
  var ss = SpreadsheetApp.getActiveSheet()
  var range = ss.getRange('input');
  const fillColors = range.getValues().map(row => row.map(color => color || null));
  ss.getActiveCell().setBackgrounds(fillColors);
}

but I am getting the following error:

Error ReferenceError: setBackgrounds is not defined (line 5).

Please correct what I am doing wrong.

Codedabbler
  • 143
  • 10
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
  • 1
    Unfortunately, as far as I understand, you can return values to be displayed with custom functions, but you can't perform actions on cells with them (like formatting) – Martín Sep 01 '23 at 11:12
  • Adding to what @Martín stated, you could read this [post](https://stackoverflow.com/questions/24424977/cell-coloring-in-google-spreadsheet-fails-if-it-is-called-from-a-cell-but-works) regarding that. We could make it work if you wrap E3 in double quotes to reference the range to be passed to your function (sample `=setFillColors("E3")`)but it will still log an error due to the restriction to modify the values within the cell itself as mentioned here in this [documentation](https://developers.google.com/apps-script/execution_custom_functions). – Century Tuna Sep 01 '23 at 19:55

0 Answers0