Copy link to clipboard
Copied
Hey Peeps,
I have come to this wonderful community many times for answers with great success. I have another one for you.
I'm working on an assessment PDF with 6 categories. Each of these categories have it own letters that the assessment selections can add up to when people click a checkbox. On the left and right column the letters each add up and populate at the bottom of the columns and then a line below that, there is the grand total for each corresponding letter. These grand totals also populate in text fields Total, Total_2, Total_3, Total_4, Total_5, and Total_6 I'm attaching the document for you to all see.
What I need to do is:
1) Have each of the corresponding text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) have their own letter values (in this case each corresponding text box would go as follows: RIASEC).
2) Then I need to be able to have the top 3 highest values from the 6 text fields (Total, Total_2, Total_3, Total_4, Total_5, Total_6) to then be determined.
3) From those 3 totals, the corresponding letters (RIASEC) for the highest values will then auto-fill into the bottom 3 boxes with the top ranking letter going into the text field "MY INTEREST CODE", the second ranking in text field "undefined", and the third ranking in text field "undefined_2".
Thank you so much in advance!
Copy link to clipboard
Copied
It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":
var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;
function sortTotals(a,b) {
return b.total-a.total;
}
The edited file is also attached.
Copy link to clipboard
Copied
It's easier to do such things when the fields are named consistently. I renamed the first total field as "Total_1" and the last two "interest code" fields as "MY INTEREST CODE_2" and "MY INTEREST CODE_3" and then used the following code as the custom calculation script for "MY INTEREST CODE_1":
var letters = "RIASEC";
var totals = [];
for (var i=1; i<=6; i++) {
totals.push({letter: letters.charAt(i-1), total: Number(this.getField("Total_"+i).valueAsString)});
}
totals.sort(sortTotals);
event.value = totals[0].letter;
this.getField("MY INTEREST CODE_2").value = totals[1].letter;
this.getField("MY INTEREST CODE_3").value = totals[2].letter;
function sortTotals(a,b) {
return b.total-a.total;
}
The edited file is also attached.
Copy link to clipboard
Copied
Thank you so much for this! And I'm going to fan girl for a moment. Try67, you've helped me with so much code for the documents I've created. I appreciate you as a teacher and Community Expert.
Copy link to clipboard
Copied
Hi @Alyssa5FBC ,
You can achieve this with JavaScript in your PDF form. Assign each total field (Total, Total_2, etc.) its corresponding letter (RIASEC). Then, sort the values in descending order and populate the top three into "MY INTEREST CODE," "undefined," and "undefined_2." Use JavaScript like below and learn from this site:
This should work—😊