12 Replies Latest reply on Mar 30, 2015 6:58 AM by pcwilson78

# i need to convert

Hi,

I know nothing of Java script but I have an excel formula of

=IF(D20<5, D20*29, IF(D20<11, D20*28, IF(D20<21, D20*27, D20*26)))

I need to convert this to a fillable form on a PDF.  I know i need to convert it to a javascript

I have form field call QTY1.1 so I need to replace D20 with QTY1.1... That's as far as I have got !

really would appreciate some help

• ###### 1. Re: i need to convert

function MyFormel(Qty1) {
if (Qty1 < 5) {
var x = Qty1 * 29
} else if (Qty1 < 11) {
var x = Qty1 * 28
} else if (Qty1 < 21) {
var x = Qty1 * 27
} else {
x = Qty1 * 26
}
return x
}

this.getField("ResultField").value = MyFormel(this.getField("Qty1").value )

or.. if you will correct the same Field, where you typed the source value, than you must write the followed instead off the previous line.

this.getField("Qty1").value = MyFormel(this.getField("Qty1").value )

write the code above in the Field Properties -> Actions -> LostFocus -> CustomScript

• ###### 2. Re: i need to convert

Do you have multiple Qty1 fields?

I would consider using a document level function like:

function QuanityPrice(d20) {

var nTotal = 0;

if(d20 < 5 ) d20 = d20 * 29;

else if(d20 < 11) d20 = d20 * 28;

else if(d20 < 21) d20 = d20 * 27;

else d20 = d20 * 26;

return d20;

}

Then for the row one only needs custom calculation script like for the row with QTY1.1:

var nRow = 1;

event.value = QuanityPrice(this.getField("QTY1." + nRow).value);

Then for other rows one only needs to update the nRow variable to match the child # for each row.

• ###### 3. Re: i need to convert

Dear GKaiseril,

pcwilson wrote "I know nothing of Java script".

I think for the first its easier for him to write the script in the Custom Script istead off document level which of course would be better for the future programming or expanding.

Do you not think your script is similar to mine.

I have 20 Points you more than 5000. Certainly you are more experienced than me. For what is the variable nTotal needed?

• ###### 4. Re: i need to convert

I favour the simple approach, anything to help people learn JavaScript is good. Though personally I disapprove of actually writing the lines of script, because in so many cases the questioner learns nothing except how to copy/paste. (This is proved because they are back soon asking for another script, or a change to the same one). I prefer to help people learn how to solve their problems, but it is a lot more work for both sides. Certainly, more than I have time for in JavaScript, so I do not complain, only comment. I guess I like a half way house which writes some of the code but then maybe has

-- repeat the above idea for each of the other cases

...

-- now set the value for use in all other cases

I worry about the inconsistent use of "var" in your script. I like to see a "var x" at the outer level of the script, then setting x within conditions. I know that var x has a scope of the whole function, but it just looks wrong to be, being used to reading other languages where it would only have the scope of the condition.

• ###### 5. Re: i need to convert

also works same without the var. But the x variable becomes to global without the vairable decleration "var"

function MyFormel(Qty1) {
if (Qty1 < 5) {
var x = Qty1 * 29
} else if (Qty1 < 11) {
var x = Qty1 * 28
} else if (Qty1 < 21) {
var x = Qty1 * 27
} else {
var x = Qty1 * 26                      // var -> had forgetten respectively typing error

}
return x
}

this.getField("ResultField").value = MyFormel(this.getField("Qty1").value )

or.. if you will correct the same Field, where you typed the source value, than you must write the followed instead off the previous line.

this.getField("Qty1").value = MyFormel(this.getField("Qty1").value )

write the code above in the Field Properties -> Actions -> LostFocus -> CustomScript

• ###### 6. Re: i need to convert

Dear Test Screen Name,

in much you're right. But i think for a beginner it's not a Problem to copy and paste in order to prevent the frustration over the non-functioning code snippet. In many cases, this protects to give up.

On the Basis of legibility and expandibility (especially for beginners) i used the {} and put the parts of the if-condition in two lines. Of Course it's not necessarily.

is this a little bit better ?

function MyFormel(Qty1) {

var x = 0                                     // x stands for the unknown cell, which is not described in Detail and resultat at the same time
if (Qty1 < 5) {
x = Qty1 * 29
} else if (Qty1 < 11) {
x = Qty1 * 28
} else if (Qty1 < 21) {
x = Qty1 * 27
} else {
x = Qty1 * 26

}
return x
}

this.getField("ResultField").value = MyFormel(this.getField("Qty1").value )

or.. if you will correct the same Field, where you typed the source value, than you must write the followed instead off the previous line.

this.getField("Qty1").value = MyFormel(this.getField("Qty1").value )

write the code above in the Field Properties -> Actions -> LostFocus -> CustomScript

• ###### 7. Re: i need to convert

I like that. And your point about overcoming initial frustration is well made. Thank you for contributing.

• ###### 8. Re: i need to convert

Thanks for the help guys.  This is brilliant.  I wasn't expecting the full script

It helps me to pull apart what I have done and compare it against something that works.

Providing the person asking the question is genuinely interested and not just after a quick answer, I think providing a script helps a lot.

I have now used the above scripts to help do other things.

• ###### 9. Re: i need to convert

Hi pcwilson,

• ###### 10. Re: i need to convert

Hi,

Apologies, I thought I had this working but still it returns a 0 value.Please see attached screen shot.

Very frustrating.  I cant see where this isnt working.!?

• ###### 11. Re: i need to convert

in your script you have changed my variable "x" to "Result1", but not the return. You must write return Result1.

Do you know that // stands for commentline. The command after // is ignored

Where you put the script ? In which field.

can you make a screenshot from the excel sample?

preferably with prefilled fields,so I can understand it

i will know which fields are given ?

which field you must type in ?

which field is your result field ?

• ###### 12. Re: i need to convert

Thanks hasand59089870

I managed to go over and check my mistake.  It seems the formfield had linked itself with another and was causing problems.

I have managed now to apply the same kind of script (with a few changes) to another problem I was encountering.