简体   繁体   中英

Change cell value based on cell color in google spreadsheet

I've been searching for a way to change a cell value (eg "Text" if cell color is red) based on another cell color?

be it a script or formula

There is something like this in Google appscript, not sure if any direct formula is also available in spreadsheet, but here it is:

I colored A1 cell of Sheet1 as Red ie #ff0000 and then got the color using the following code:

function test()
{
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var color = sheet.getRange(1, 1).getBackground();
  Logger.log(color);
}

Output

#ff0000

So, you just have to check if(color == "#ff0000") (or code of any color you want) and then set values.

EDIT

Here is the code that will fulfill your requirements. I have also added the comments so that you can develope it further.

function myFunction() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");

  var data1 = sheet1.getDataRange().getValues();  //2D array of sheet1 cell values
  var bg;

  for(var i=1; i<data1.length; i++)
  {
    bg = sheet1.getRange(i+1, 2).getBackground();
    if(bg == "#ff0000")  //Add more colors in else if and else if you have 5-6 different colors, this one is for red
    {
      sheet2.getRange(i+1, 3).setValue("For Verification");  //Set value in corresponding row of sheet2
    }    
  }
}


/**
* For to-be filled records in future, you can 
* set a trigger of onEdit if you are manually 
* filling sheet 1
**/

I did this script to update some colors, but it may give you an Idea on how to do it.

Keep in mind that in Google Sheets, the x,y matrix is always x=row and then y=column. So when you update values you must think first value of the pair is the row and then column, in order to avoid mistakes.

Also, getRange is 1 based and arrays are 0 based.

Finally, getBackground operation is quite expensive because it needs to fetch data from the sheet for each time you call it, if you will run a script over several cells, then it is better to use getBackgrounds() in order to get the matrix of all backgrounds in the range.

/** @OnlyCurrentDoc */
function calculateValues() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("origin")
  var data = sheet1.getDataRange();
  var values = data.getValues();
  var bg = data.getBackgrounds()

  Logger.log("Rows: " + values.length)
  Logger.log("Columns: " + values[0].length)

  for (var y = 0; y < values.length; y++) {

  Logger.log("Row: " + (y+1) + " with length " + values[y].length)

    for (var x = 0; x  < values[y].length; x++) {
      var i = x+1
      var j = y+1

      Logger.log("row, column: " + j + "," + i + " bg: " + bg[y][x])

      if(bg[y][x] == "#34a853") {  // green
        sheet1.getRange(j,i).setValue(2)

      } else if (bg[y][x] == "#fbbc04") {  //yellow
        sheet1.getRange(j,i).setValue(1)

      } else if (bg[y][x] == "#d9d9d9") { //gray
        sheet1.getRange(j,i).setValue(0)

      } else if (bg[y][x] == "#6aa84f" ) { // green 2 
        sheet1.getRange(j,i).setBackground("#34a853")
        sheet1.getRange(j,i).setValue(2)

      } else if (bg[y][x] == "#f1c232"  ) { // yellow 2 
        sheet1.getRange(j,i).setBackground("#fbbc04")
        sheet1.getRange(j,i).setValue(1)
      
      } else if (bg[y][x] == "#b7b7b7") { // gray 2 
        sheet1.getRange(j,i).setBackground("#d9d9d9")
        sheet1.getRange(j,i).setValue(0)
      }
 
    }

  }

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM