Search code examples
google-apps-scriptgoogle-sheets

Google App Script - UI alert message to validate cell color


I am trying to return a ui.alert message when a cell color is white but return true if green. I was able to get it to return the message when the cell color is white but when I change the cell color to green, it still returns the message. I have a feeling it is something simple but I have tried for hours trying to figure out the issue but no luck.

//Validating Cell Color #00ff40 (green)
   var cell = "B6"; // The cell.
   var colors = ["#00ff40", "#ffffff"]; // The two colors to validate.

   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getActiveSheet(); // or ss.getSheets()[0];
   var range = sheet.getRange(cell);
   var currentColor = range.getBackground();
   var color = colors.find(e => e != currentColor);
   
  if (currentColor=="#ffffff"){
      
      ui.alert("Color is not green.");
      shUserForm.getRange("B6").activate();
      
      return false;

  }

  return true;

Solution

  • This works for me:

    function testie() {
      var cell = "B6";
      var colors = ["#00ff40", "#ffffff"];
      const ui = SpreadsheetApp.getUi();
      var ss = SpreadsheetApp.getActive();
      var sheet = ss.getActiveSheet();
      var range = sheet.getRange(cell);
      var currentColor = range.getBackground();
      if (currentColor == "#ffffff") {
        ui.alert("Color is not green.");
        shUserForm.getRange("B6").activate();
        return false;
      }
      return true;
    }