Help with multiple if statements
I need a formula to check whether the value of 9 fields is greater than 0.
If > 0 then calculate as per my formula below. If 6 fields have a number then all of the calculated values are added together. The formula works with one line. As soon as I duplicate the line, set the new field values and put a plus sign in between, I get an error.
Script syntax error, unexpected token if
So this line works fine by itself:
if (@16pp Sheetwork > 0) { @Qty / 1000 * @16pp Sheetwork * 16.00 + 55} else {0}
If I add another line I get the error
if (@16pp Sheetwork > 0) { @Qty / 1000 * @16pp Sheetwork * 16.00 + 55} else {0} +
if (@8pp Sheetwork > 0) { @Qty / 1000 * @8pp Sheetwork * 18.00 + 50} else {0}
Can some please help with the correct syntax. Thanks.
Regards
Steve
-
Hi Stephen,
you are on the right track. You get an error, because you are trying to add two if-statements, while what you are trying to do is add two numbers, based on the conditions of two if-statements. To solve your problem you can use two variables, e.g.
result1
andresult2
, that you assign values within each if statement, and then add those to variables at the end of your calculation:// First, let's set the values to zero (that way you don't need the "else" parts below): var result1 = 0; var result2 = 0; // Now set the values depending on your conditions: if (@16pp Sheetwork > 0) { result1 = @Qty / 1000 * @16pp Sheetwork * 16.00 + 55; } if (@8pp Sheetwork > 0) { result2 = @Qty / 1000 * @8pp Sheetwork * 18.00 + 50; } // Finally we will add those two values as the final result: result1 + result2;
BTW: JavaScript provides some syntactic sugar that let's you write this in an even more compact way. It's simply a shorthand for the if/else:
var result1 = (@16pp Sheetwork > 0) ? @Qty / 1000 * @16pp Sheetwork * 16.00 + 55 : 0; var result2 = (@8pp Sheetwork > 0) ? @Qty / 1000 * @8pp Sheetwork * 18.00 + 50 : 0; result1 + result2;
Best,
StefandieKollaborateure.com - Podio Training+Consulting+Development — auch auf Deutsch
-
Hi Steve,
for the reason why your IF-construction doesn't work see here http://www.w3schools.com/js/js_if_else.asp.
But you need also the IF-condition: Sum only if 6 or more fields have a value.
This script does it (it's only one possibility to calculate what you want). The text behind // is my comment which can be deleted). I use the short syntax: (if) value > 0 ? than this : else thatvar $qty = @Qty / 1000; var $16pp = @16pp Sheetwork ; var $8pp = @8pp Sheetwork; // add the other 7 fields var $16ppCheck = $16pp > 0 ? 1 : 0; var $8ppCheck = $8pp > 0 ? 1 : 0; //add the other 7 var $valueCheck = $16ppCheck + $8ppCheck // + the other 7; this checks, if there are minimum 6 fields > 0 var $16ppCalc = $16pp > 0 ? $qty * $16pp * 16.00 + 55 : 0; var $8ppCalc = $8pp > 0 ? $qty * $8pp * 18.00 + 50 : 0; // add the other 7 var $sumCalc = $16ppCalc + $8ppCalc //+ the other 7 $valueCheck >= 6 ? $sumCalc : 0 // this gets the sum if more min. 6 fields have a value
Rainer
Please sign in to leave a comment.
Comments
5 comments