google script note row colorGoogle Script row color detector

43 views Asked by At

I need a little assistance writing a google script function. I have a Google Sheet where each individual row may have a different color assigned to it. I need a script that can note the html color code found in the first cell of each row, in that cell.

So for example lets say row 1 is a green row and row 2 is a blue row, the cell A1 should say #00ff00 and A2 should say #0000ff after running the script. Below is what I have so far.

function colorDetector() {
  var startRow = 1;  // First row of data to process
  var numRows = 3;   // Number of rows to process
  var currentsheet = 'Production' // What sheet you would like to process (must be within ' ')
  //This section prepares the document to be read
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName(currentsheet);
  var dataRange = sheet.getRange(startRow, 1, numRows, 15)  // Fetch values for each row in the Range (row, column, numRows, numColumns)
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];

    //this is the section i cant seem to get working correctly
    var color = row[0].getbackground(); //should set the variable "color" to the html color found in the first cell of the current row.

    sheet.getRange(startRow + i, 1).setValue(color);      // notes the variable found
    SpreadsheetApp.flush();

  }
}
1

There are 1 answers

0
Dean Taylor On BEST ANSWER

Here is what I ended up figuring out on my own. It works perfectly.

function colorDetector2() {
  var startRow = 1;  // First row of data to process
  var currentsheet = 'sheet 4' // What sheet you would like to process (must be within ' ')
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName(currentsheet);
  var numRows = 10;   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 20)  // Fetch values for each row in the Range (row, column, numRows, numColumns)
  var colordata = dataRange.getBackgrounds();
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var color = colordata[i]
    var colorconv = String(color[1]);
    if (colorconv == "#00ff00") {
      //do something here;
    } else {
      //do something else here;
    }
  }
}