Google-sheets – How to use google apps script to validate a cell

google sheetsgoogle-apps-script

I have been trying to write this code to validate a range of cells. If the cells contain the text "true" then set value as "Incorrect Data" else "DD Data"

/** @OnlyCurrentDoc */
function myFunction() {

  var app = SpreadsheetApp
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet();


  for (var i = 1; i<10; i++) {

   var workingcell = activeSheet.getRange(i,1).getValue();
    if( workingcell == "TRUE" ) { 

       activeSheet.getRange(i,2).setValue("Incorrect Data");
       }

  else {activeSheet.getRange(i,2).setValue("DD Data");}    

}
}

The code is executing without compilation error but it is not able to recognize the cell values because it is mark "DD Data" for all the cells which is incorrect. If the cell value is "TRUE" then — "Incorrect Data" else "DD Data

Best Answer

Choose one of the following options

  1. instead of getValue() use getDisplayValue() the first return a boolean for a Google Sheets TRUE/FALSE values while the second returns a string

  2. instead of "TRUE" use true
    true is the JavaScript equivalent for Google Sheets TRUE