Hi, welcome to the Foxit Planet PDF Forum. If you have PDF or Adobe Acrobat questions then the right place to ask them is here, in this forum. |
Having some trouble formatting a calculation scrip |
Post Reply ![]() |
Author | |
AJPleat ![]() New Member ![]() Joined: 08 Jan 2018 Points: 4 |
![]() ![]() ![]() ![]() Posted: 08 Jan 2018 at 6:22pm |
I'm trying to create a custom calculation script that takes an inputted variable from one field, subtracts it by 10, divides it by 2, rounds it down, then outputs the number. For those who this sounds familiar to it's because I'm creating a fillable D20 character sheet.
The fields I'm working with (at the moment) are Strength for the input and STRMOD for the output. So far nothing I've done has worked - the output field remains resolutely at 0, or now blank completely. Any help would be appreciated - this is my first time working with PDF scripting.
I'm also going to need one that just outputs half of another field's input, although I imagine that'll be much simpler. |
|
![]() |
|
Sponsored Links | |
![]() |
|
gkaiseril ![]() Senior Member ![]() Joined: 15 Jul 2011 Location: USA Points: 4105 |
![]() ![]() ![]() ![]() |
One possible custom calculation script could be:
// custom calculation for the STRMOD field; event.value = ""; // clear field; // get the inpupt field object; var oStrength = this.getField("Strength"); // check for error; if(oStrength == null) { app.alert("Error accessing Strength field", 1, 0); } else { // get the value of the field; var nStrength = oStrength.value; if(nStrength != 0) { // display value; console.show(); console.clear(); console.println("Strength = " + nStrength); // subract 10 nStrength = nStrength - 10; console.println("Strength - 10 = " + nStrength); // divide by 2 nStrength = nStrength / 2; console.println("(Strength - 10) / 2 = " + nStrength); // turncate to nearest lower intiger nStrength = Math.floor(nStrength); console.println("Math.floor((Strength - 10) / 2) = " + nStrength); // set field value; event.value = nStrength; } // end nStrength not equal to 0; } // end Strength not null; Note if there are other scripts in your form and they have any error or encounter an error while running, this script may not complete processing; Note division by 0 results in an error condition, so you need to account for this possibility in your scripts.
|
|
![]() |
|
AJPleat ![]() New Member ![]() Joined: 08 Jan 2018 Points: 4 |
![]() ![]() ![]() ![]() |
That definitely worked, thanks! I assume to have something similar but without the subtraction by 10 I just remove the lines that reference that.
It's definitely a lot more complex than I thought it would be, which explains why I was getting nowhere. |
|
![]() |
|
AJPleat ![]() New Member ![]() Joined: 08 Jan 2018 Points: 4 |
![]() ![]() ![]() ![]() |
Actually this isn't working properly.
If I put in 0 for the Strength score, it doesn't give me a result when it should give me -5. The math breaks down somewhere. |
|
![]() |
|
gkaiseril ![]() Senior Member ![]() Joined: 15 Jul 2011 Location: USA Points: 4105 |
![]() ![]() ![]() ![]() |
The script does not allow the entry of blank or zero.
|
|
![]() |
|
AJPleat ![]() New Member ![]() Joined: 08 Jan 2018 Points: 4 |
![]() ![]() ![]() ![]() |
I'm not entirely certain of why. I know you said dividing by 0 will produce an error (because that's not possible in math), but you wouldn't be dividing by zero. You'd be subtracting 10 from zero, getting -10, then dividing -10 by 2.
If it won't work regardless, is there a way to insert an if/then for an entry of blank/zero that will skip the rest of the script and just output a -5? |
|
![]() |
Post Reply ![]() |
|
Tweet
|
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |