[SOLVED] Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
PAS_Drafter
Posts: 339
Joined: Mon Aug 02, 2021 6:29 am
Location: California, USA

[SOLVED] Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post by PAS_Drafter »

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/United States (en_US)


I'm trying to use a Nested IF THEN in FreeCAD spreadsheets, but it doesn't appear to be working right.

I've used the following command where the user can enter 1, 2, or 3 in one cell to indicate that the value at this other cell will be 10, 20, or 30

=VarOpt == 1 ? VarChoice1 : VarOpt == 2 ? VarChoice2 : VarChoice3

I expected VarOpt == 1 ? Var Choice 1 to give me 10. If VarOpt == 1 failed, I expected it to go to the next level and evaluate VarOpt == 2. If that was true, then it would give me 20. If neither were true, it would give me 30

However when I implement it, I do not get what I expected. It seems like it only evaluates the last check. If I put in 1, it gives me 30, but if I enter 2 it does give me 20.

Is nesting not supported?

Also, I haven't been able to find any documentation on using AND or OR statements. Are they supported?

Finally, it would be nice to have an ELSE statement as well to give me a value if none of the other conditions are met.
Attachments
NestedIFTHEN.FCStd
(2.39 KiB) Downloaded 16 times
Last edited by PAS_Drafter on Fri Jan 21, 2022 7:51 pm, edited 1 time in total.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post by openBrain »

You have to use parentheses to prioritize the expressions. ;)
PAS_Drafter
Posts: 339
Joined: Mon Aug 02, 2021 6:29 am
Location: California, USA

Re: Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post by PAS_Drafter »

Interesting way of doing it.

=VarOpt == 1 ? VarChoice1 : (VarOpt == 2 ? VarChoice2 : (VarOpt == 3 ? VarChoice3 : VarChoice4))

Seemed to do the trick and I was able to add an additional level.

THANKS
Attachments
NestedIFTHEN.FCStd
(2.39 KiB) Downloaded 24 times
PAS_Drafter
Posts: 339
Joined: Mon Aug 02, 2021 6:29 am
Location: California, USA

Re: Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post by PAS_Drafter »

Is there a way to use OR or AND Booleans in the statement? Can do it the hard way, but if those are available, just makes the code a little cleaner. :D

I've searched for IF THEN in the forum, but because they are such common words, of course it does not give me much info. I looked in the Spreadsheet manual Spreadsheet Workbenchand expressions area and didn't see anything on this. It would be nice if someone could add some information about these in the spreadsheet manual as I expect this to be a common function used there.

But then again, if no one has asked up to now, maybe it isn't that common. :shock:
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Nesting IF THEN statements and using ELSE AND OR in Spreadsheet

Post by openBrain »

AFAIK AND and OR aren't available, so you have to go the long way. :)
If you have complex expressions, probably a good way is to have an intermediate truth table in the spreadsheet.
Post Reply