Omid
Omid

Reputation: 177

This "IF" won't work for me. Why?

I have written the following code:

function test() {
  var spreadsheet = SpreadsheetApp.getActive();
  var ui = SpreadsheetApp.getUi();
  var testModeStatus = spreadsheet.getSheetByName("Students").getRange("P2:P2").getValues();  
  if (testModeStatus == 'FALSE') {
     ui.alert("This is inside IF and Test Mode Status is "+testModeStatus);
    };
    ui.alert("This is outside IF and Test Mode Status is "+testModeStatus);
};

In the above code the variable testModeStatus refers to a sheet cell that is a checkbox and can have the value of either TRUE(checked) or FALSE(unchecked).

I want the code inside if to run only when the checkbox is unchecked. But no matter if the checkbox is checked or unchecked, the code inside if never runs and in both cases just the code outside if runs.
Moreover, in both cases the variable testModeStatus shows the correct value in the popping alert message. I mean if the checkbox is checked the variable shows TRUE and if it is unchecked the variable shows FALSE in the popping alert message.

Upvotes: 0

Views: 41

Answers (1)

Adelin
Adelin

Reputation: 8199

you're comparing apples with oranges

.getValues() returns a bi-dimensional array and "FALSE" is a string.

Since you get a cell, you can use .getValue instead, but do note that GAS parses the boolean into a JS boolean, so, ultimately, the code should be:

function test() {
  var spreadsheet = SpreadsheetApp.getActive();
  var ui = SpreadsheetApp.getUi();
  var testModeStatus = spreadsheet.getSheetByName("Students").getRange("P2:P2").getValue();  // getValue, not values
  if (testModeStatus == false) { //false, not "FALSE"
     ui.alert("This is inside IF and Test Mode Status is "+testModeStatus);
    };
    ui.alert("This is outside IF and Test Mode Status is "+testModeStatus);
};

Upvotes: 4

Related Questions