Home > Enterprise >  Google Appscript IF Or statement not working
Google Appscript IF Or statement not working

Time:09-26

Good day everyone; I am running into an error I can't explain. The scenario is as follows, I have two input boxes that collect information. If no value is entered, I want the if statement to handle it and cause a break. The Input box also has an "x" to close the box, which returns a value of "Cancel". What I am trying to do is capture a condition where if no value is entered OR cancel is passed through, a break will occur. Right now, the problem is Google completely ignores the Or statement. I know individually, my IF logic works, but when coupled with OR it doesn't recognize the condition.

This is my current code:

          var propnumber = Browser.inputBox('Enter RFI/RFQ Number', Browser.Buttons.OK);
          
          if(propnumber != "" || propnumber != 'cancel'){} else{
            SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
            return
          };
          var myName = Browser.inputBox("Enter the Component Name",Browser.Buttons.OK_CANCEL);

          if(myName != 'cancel')
          {
                  I do something
          }

As I mentioned in my description, my propnumber condition ignores the or and always accepts the value of cancel or blank. If I remove the or ( || ) then it works with one condition at a time.

I am sure this is something trivial any help appreciated.

CodePudding user response:

What's wrong

The logic in the following part of your code

if(propnumber != "" || propnumber != 'cancel'){
    // I assume some code will go here
} else{
    SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
    return
};

does not match the logic you've described here:

if no value is entered OR cancel is passed through, a break will occur.

Consider the case where propnumber is 'cancel':

propnumber != "" evaluates to true

propnumber != 'cancel' evaluates to false

Therefore the if(... || ...) condition in your code evaluates to true and the (currently empty) if block runs, rather than the else.

How to fix it

Option 1: A literal translation of the logic

if no value is entered OR cancel is passed through, a break will occur

would be

if(propnumber == "" || propnumber == 'cancel') {
    SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
    return
} else {
    // Some action
} 

Option 2: If you wish to swap the if and else clauses, you must negate the entire condition. So this will also work:

if(!(propnumber == "" || propnumber == 'cancel')) {
    // Some action
} else {
    SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
    return
} 

Note the added parentheses and single negation.

Option 3: use AND instead of OR in your existing code.

The expression !(A || B) is NOT logically equivalent to !A || !B. Instead, it is equivalent to !A && !B (see DeMorgan's Law). So this will also work:

if(propnumber != "" && propnumber != 'cancel') {
    // Some action
} else {
    SpreadsheetApp.getActiveSpreadsheet().toast('You must enter a value')
    return
} 
  • Related