Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript Help - custom calculation in one field based off value in two other fields

Explorer ,
Nov 03, 2021 Nov 03, 2021

Good morning - I'm trying to figure out how to return the value of a field based off of the values of two different fields where I have a table of data (6 columns, 42 rows).  I'm guessing that setting it up in either an array or in an object may be the solution, but not sure how to implement. The fields are:

var s1 = getField("CrimHistory").value;
var s2 = getField("TotalOffenseLevel").value;
var s3 = getField("Min").value;

The data I'm looking to return is from this table, where the X axis is TotalOffenseLevel and the Y axis is CrimHistory (columns/rows named for ease of reference only). 

   I  II III  IV  V   VI
1  0, 0,  0,  0,  0,  0,
2  0, 0,  0,  0,  0,  1,
3  0, 0,  0,  0,  2,  3,
4  0, 0,  0,  2,  4,  6,
5  0, 0,  1,  4,  6,  9,
6  0, 1,  2,  6,  9,  12,
7  0, 2,  4,  8,  12, 15,
...
[table is truncated for brevity]

 

So, if s1 = "II" and s2 = "7", the output in s3 should = "2".  Thanks in advance for any assistance you can provide!

TOPICS
How to , JavaScript , PDF forms
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Try something like this as custom calculation script of "Min" field:

var s1 = this.getField("CrimHistory").valueAsString;
var s2 = Number(this.getField("TotalOffenseLevel").value);
var a = [[0,0,0,0,0,0],
         [0,0,0,0,0,1],
         [0,0,0,0,2,3],
         [0,0,0,2,4,6],
         [0,0,1,4,6,9],
         [0,1,2,6,9,12],
         [0,2,4,8,12,15]];

var num = "";
if(s1=="I")num=0;
else if(s1=="II")num=1;
else if(s1=="III")num=2;
else if(s1=="IV")num=3;
else if(s1=="V")num=4;
else if(s1=="VI")num=5;

if(s1 != "" && s2 != 0)
event.value = a[s2-1][num];
else event.value = "";

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

HI,

 

Is the table above in fields in Acrobat? or is it just in the JavaScript?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2021 Nov 03, 2021

No, the data in the table isn't in any fields - it would either be in the script or maybe in an object called by the script.  Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

HI,

 

This page would help with the array solution ( which is probably what I would go for in this case).

 

https://www.javascripttutorial.net/javascript-multidimensional-array/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2021 Nov 03, 2021

Thanks - I'll give that a look and see if I can flex my very nascent JS muscles to make it work. 🙂 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Try something like this as custom calculation script of "Min" field:

var s1 = this.getField("CrimHistory").valueAsString;
var s2 = Number(this.getField("TotalOffenseLevel").value);
var a = [[0,0,0,0,0,0],
         [0,0,0,0,0,1],
         [0,0,0,0,2,3],
         [0,0,0,2,4,6],
         [0,0,1,4,6,9],
         [0,1,2,6,9,12],
         [0,2,4,8,12,15]];

var num = "";
if(s1=="I")num=0;
else if(s1=="II")num=1;
else if(s1=="III")num=2;
else if(s1=="IV")num=3;
else if(s1=="V")num=4;
else if(s1=="VI")num=5;

if(s1 != "" && s2 != 0)
event.value = a[s2-1][num];
else event.value = "";
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2021 Nov 03, 2021

I tried this, but it didn't work. It didn't provide any error but also didn't create any output.  Does it matter that "CrimHistory" is also a calcuated field? This is the script that creates its output:

var CrimHistory = Number(this.getField("CHScore").value); 
if (CrimHistory >= 0 && CrimHistory <= 1 ) event.value = "I" ; 
else if (CrimHistory >= 2 && CrimHistory <= 3 ) event.value = "II" ; 
else if (CrimHistory >= 4 && CrimHistory <= 6 ) event.value = "III" ;
else if (CrimHistory >= 7 && CrimHistory <= 9 ) event.value = "IV" ; 
else if (CrimHistory >= 10 && CrimHistory <= 12 ) event.value = "V" ; 
else if (CrimHistory >= 13 ) event.value = "VI" ; 
else event.value = "Error";

 

Thanks!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

It shouldn't matter.

Where did you put script? Can you share your file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 04, 2021 Nov 04, 2021

Nesa, I really appreciate the help.  Attached is the file and I've highlighted the relevant sections on Page 2.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2021 Nov 04, 2021

You need to put words(strings) inside quotations like this: "life"

last line of table should look like this: ["life","life","life","life","life","life"]];

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 04, 2021 Nov 04, 2021
LATEST

Of coure!  Thank you!  Much, much appreciated! 🙂 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Check the field calculation order.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

Since the y axis is already a number it is suitable for an array, whereas the x axis uses a roman numeral, which is a string, and better for an object selection. So I'd suggest a hybrid.

Then the data selection for s3 is straight forward. 

 

 

 

 
var oMinTable = { II:[0, 0,  0,  0,  0,  0, 0],
                  III:[0, 0,  0,  0,  0,  1, 2],
                  III:[0, 0,  0,  0,  1,  2, 4],
};             
var s1 = getField("CrimHistory").value;
var s2 = getField("TotalOffenseLevel").value;
event.value = oMinTable[s1][s2];

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2021 Nov 03, 2021

You say the data selection is straightforward, but I'm not sure I understand.  Can you be more specific?

 

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2021 Nov 03, 2021

The data selection is straight forward because the two inputs are used directly. No extra logic is needed to massage the inputs.  

I changed my previous code for use in the calculation for "Min" field.  Calculations always set "event.value". Sorry about the confusion. 

Also calculation order is important. 

Here's another version of the code that protects against empty and invalid inputs.

 

var oMinTable = { II:[0, 0,  0,  0,  0,  0, 0],
                  III:[0, 0,  0,  0,  0,  1, 2],
                  III:[0, 0,  0,  0,  1,  2, 4],
};             
var s1 = getField("CrimHistory").value;
var s2 = getField("TotalOffenseLevel").value;
var aRow = oMinTable[s1];
if(aRow && aRow[s2])
    event.value = aRow[s2];
else
    event.value = "";

 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines