Home > Mobile >  Why aren't my IF conditions being met? (checkbox.isChecked(), and checkbox == true methods not
Why aren't my IF conditions being met? (checkbox.isChecked(), and checkbox == true methods not

Time:06-19

I'm attempting to apply borders to cells conditionally via apps script, but my code is not registering the two (obviously met) conditions. Looking for a logic and syntax check <3

Sheet: stackoverflow noobs can't attach images so here's a link

Code:

function doBorders() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sss = ss.getActiveSheet();
  let doBorder = sss.getRange("A5");
  let checkBox = sss.getRange("B5");
  let checkState = checkBox.isChecked();
    let bRow1 = sss.getRange("A1:B1");
    let bRow2 = sss.getRange("A2:B2");
    let bRow3 = sss.getRange("A3:B3");

  if (doBorder == "Do Borders:" && checkState == true) {
    bRow1.setBorder(false, false, true, false, false, false,'Black', SpreadsheetApp.BorderStyle.SOLID);
    bRow2.setBorder(false, false, true, false, false, false,'Black', SpreadsheetApp.BorderStyle.SOLID);
    bRow3.setBorder(false, false, true, false, false, false,'Red', SpreadsheetApp.BorderStyle.SOLID);
    Logger.log('Conditions met. Borders added.');

    } else
      bRow1.setBorder(false, false, false, false, false, false);
      bRow2.setBorder(false, false, false, false, false, false);
      bRow3.setBorder(false, false, false, false, false, false);
      Logger.log('Conditions not met. No borders.');
}

I've tried both checkBox == true and checkState == true. Same result.

Execution Log: "Conditions not met. No borders."

CodePudding user response:

let checkBox = sss.getRange("B5") gets a Range object, which is different from the value in that cell. To test the value, use Range.getValue(), like this:

  const checkBox = sss.getRange('B5');
  const checkBoxValue = checkBox.getValue();
  if (checkBoxValue === true) {
    // do sfuff...
  } else {
    // do other stuff...
  }

Also observe the { curly braces } after the else.

  • Related