M
M
Marat2021-06-19 17:36:38
Google Apps Script
Marat, 2021-06-19 17:36:38

How to set rgb cell value in google sheets?

Hello. I created 2 sheets. In one sheet I have an rgb color value and an identifier, in another sheet I enter an identifier, and the adjacent cell is painted in a specific color.
I am new to app script. When executing the script, I get an error:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Range.setBackgroundRGB.

If I change the value of rangeB.setBackgroundRGB to some specific value, then it paints over all the cells. Even if the id doesn't match. I would be very grateful if you could help me figure out the code.

My code:

function SetColor() {
  const rowStart = 2;
  const colStart = 1;

  const rowsCount = 5;
  const colsCount = 2;
  
  // Получение объекта страницы
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[1];

  // Извлечение данных из таблицы
  var range = sheet.getRange (rowStart, colStart, rowsCount, colsCount);
  var data = range.getDisplayValues();

  // Константы-имена для индексов столбцов
  const dmcCol = 0;
  const colorCol = 1;

  // sheet 0
  var sss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetO = sss.getSheets()[0];

  var rangeO = sheetO.getRange ("B3:B");
  var rangeB = sheetO.getRange ("A3:A");

  var dataO = rangeO.getDisplayValues();
  // var dataB = rangeB.getDisplayValues();



  for (var i in data)
    {
      let row = data[i];
      let classDmc = row[dmcCol];
      let classColor = row[colorCol];

      for (var o in dataO)
      {
        if (dataO[o] == classDmc) 
        {}
        rangeB.setBackgroundRGB(classColor);
      }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-06-20
@ProgrammerForever

Maybe this is a mistake? There is a check, but no action, the 3rd line is executed anyway.

if (dataO[o] == classDmc) 
        {}
        rangeB.setBackgroundRGB(classColor);

change to
if (dataO[o] == classDmc){
        rangeB.setBackgroundRGB(classColor);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question