Jens Damen
Jens Damen

Reputation: 5

Google script - Need assistance on IF function

So I used to work with multiple projects on 1 sheet to use multiple OnEdit events. Now that the OnEdit events got more plenty and I have to many projects I need to group them in to one project.

Since I merged my code no longer works.

In one sheet I have 4 checkboxes.

function onEdit(e) {
  if (e.range.getA1Notation() == "C4"){
    Logger.log('C4 selected');
    FunctionC4();
  }
  if (e.range.getA1Notation() == "C5"){
    Logger.log('C5 selected');
    FunctionC5();
  }
  if (e.range.getA1Notation() == "C6"){
    Logger.log('C6 selected');
    FunctionC6();
  }
  if (e.range.getA1Notation() == "C7"){
    Logger.log('C7 selected');
    FunctionC7();
  }
}

So far this works great but when I then call my FunctionC4 (or any other) things stop working at my IF function

function FunctionC4() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var Range = sheet.getRange(4,3);
  var toggle = Range.getValue();
  Logger.log(toggle);
  if (toggle == "TRUE") {
    Logger.log('C4 is true');
    sheet.hideColumns(6, 15);
  }
  else if (toggle == "FALSE") {
    Logger.log('C4 is false');
    sheet.showColumns(6, 15);
    sheet.hideColumns(11, 1);
  }
}

My logger.log(toggle) still displays true or false. So I know toggle is either true or false and the function is called. I don't understand why the IF function doesn't read it. I never get the logger.log "C4 is true" or false.

I'm guessing it has something to do with my Range.getValue(); but after hours of searching I can't figure it out.

Upvotes: 0

Views: 58

Answers (2)

Marios
Marios

Reputation: 27348

As a side note to Diego's answer. If you are planning to use the onEdit(e) function please modify your logic as follows to get advantage of the event object. Pass the event object as an argument to each of the functions:

function onEdit(e) {
  if (e.range.getA1Notation() == "C4"){
    Logger.log('C4 selected');
    FunctionC4(e);
  }
  if (e.range.getA1Notation() == "C5"){
    Logger.log('C5 selected');
    FunctionC5(e);
  }
  if (e.range.getA1Notation() == "C6"){
    Logger.log('C6 selected');
    FunctionC6(e);
  }
  if (e.range.getA1Notation() == "C7"){
    Logger.log('C7 selected');
    FunctionC7(e);
  }
}

and then FunctionC4 will be:

function FunctionC4(e) {

  var sheet = e.range.getSheet();
  var toggle = e.value;
  
  Logger.log(toggle);
  if (toggle == true) {
    Logger.log('C4 is true');
    sheet.hideColumns(6, 15);
  }
  else if (toggle == false) {
    Logger.log('C4 is false');
    sheet.showColumns(6, 15);
    sheet.hideColumns(11, 1);
  }
}

and follow exactly the same logic for the other functions as well.

Note that e.value is "only available if the edited range is a single cell". Your code already assumes this to be the case with your existing conditions, e.g. e.range.getA1Notation() == "C4".

Upvotes: 2

Diego
Diego

Reputation: 9571

getValue() returns Google's interpreted value of the cell

The value may be of type Number, Boolean, Date, or String depending on the value of the cell.

In this case, it will return a boolean, not a string.

Try

if (toggle == true) {
  // ...
}
else if (toggle == false) {
  // ...
}

Alternatively, you could try if (toggle == "true"), but that only works because it's lowercase. It's equivalent to writing if (toggle.toString() === "true"), which is essentially what happens when you use loose equality.

Upvotes: 3

Related Questions