Planet PDF Forum Planet PDF Forum
  New Posts New Posts RSS Feed - Having some trouble formatting a calculation scrip
  FAQ FAQ  Forum Search   Register Register  Login Login


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 Post Reply
Author
AJPleat View Drop Down
New Member
New Member


Joined: 08 Jan 2018
Points: 4
Post Options Post Options   Quote AJPleat Quote  Post ReplyReply Direct Link To This Post Topic: Having some trouble formatting a calculation scrip
    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.
Back to Top
Sponsored Links


Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4091
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post Posted: 08 Jan 2018 at 8:20pm
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. 
Back to Top
AJPleat View Drop Down
New Member
New Member


Joined: 08 Jan 2018
Points: 4
Post Options Post Options   Quote AJPleat Quote  Post ReplyReply Direct Link To This Post Posted: 08 Jan 2018 at 8:35pm
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.
Back to Top
AJPleat View Drop Down
New Member
New Member


Joined: 08 Jan 2018
Points: 4
Post Options Post Options   Quote AJPleat Quote  Post ReplyReply Direct Link To This Post Posted: 08 Jan 2018 at 8:50pm
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.
Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4091
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post Posted: 08 Jan 2018 at 9:14pm
The script does not allow the entry of blank or zero.
Back to Top
AJPleat View Drop Down
New Member
New Member


Joined: 08 Jan 2018
Points: 4
Post Options Post Options   Quote AJPleat Quote  Post ReplyReply Direct Link To This Post Posted: 08 Jan 2018 at 10:22pm
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?
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.10
Copyright ©2001-2017 Web Wiz Ltd.

This page was generated in 0.035 seconds.