Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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 = "";
Copy link to clipboard
Copied
HI,
Is the table above in fields in Acrobat? or is it just in the JavaScript?
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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/
Copy link to clipboard
Copied
Thanks - I'll give that a look and see if I can flex my very nascent JS muscles to make it work. 🙂
Copy link to clipboard
Copied
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 = "";
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
It shouldn't matter.
Where did you put script? Can you share your file?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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"]];
Copy link to clipboard
Copied
Of coure! Thank you! Much, much appreciated! 🙂
Copy link to clipboard
Copied
Check the field calculation order.
Copy link to clipboard
Copied
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];
Copy link to clipboard
Copied
You say the data selection is straightforward, but I'm not sure I understand. Can you be more specific?
Thanks!
Copy link to clipboard
Copied
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 = "";
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more