Skip to main content
WonderWomansApprentice
Inspiring
November 3, 2021
Answered

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

  • November 3, 2021
  • 3 replies
  • 3413 views

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!

This topic has been closed for replies.
Correct answer Nesa Nurani

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 = "";

3 replies

Thom Parker
Community Expert
November 3, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
WonderWomansApprentice
Inspiring
November 3, 2021

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

 

Thanks!

Thom Parker
Community Expert
November 4, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
November 3, 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 = "";
WonderWomansApprentice
Inspiring
November 3, 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!

 

Nesa Nurani
Community Expert
November 4, 2021

It shouldn't matter.

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

BarlaeDC
Community Expert
November 3, 2021

HI,

 

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

WonderWomansApprentice
Inspiring
November 3, 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!

BarlaeDC
Community Expert
November 3, 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/