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

Calculating 3 lowest and 3 highest voted values into a new text box

New Here ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Hi, I'm a total novice, but I have been creating forms for health coaching where a client would vote for how they feel in certain areas from the values 0, 2, 4, 6, 8 and 10 on a wheel of life. I want to get the three lowest voted numbers into a new text box where the client can reflect on those and the three highest voted into another text box. Is this possible? Right now I have radio dials set up and once a dial is pressed the number then appears in a textbox next to the title of "Family Life," for example. If the client votes 10 for family life the textbox shows ten, but I'd like the new text boxes to list it as the name "Family Life." I hope this makes sense. It's so that during the session I won't need to waste time retyping in Family Life et al. I've attached my pdf for reference.

TOPICS
How to , JavaScript , PDF forms

Views

1.2K

Translate

Translate

Report

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 ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

To make a scheme like this work it's helpful to have both an easy way to connect the fields to the topic they represent, and to able to deal with the working fields as a group.  

 

For the grouping part, the field names are important, the "Score" fields all follow a pattern which will work, i.e. they are all prefixed with "Score-". It would be better if they were prefixed with "Score.", but just having a consistent pattern is good.

 

The topic will be assumed to be the tooltip for each Score field, which hasn't been filled out yet. In JavaScript the tooltip is the "userName" property.

 

Here's a script that will sort out the top 3 scores and place the topic text into the "Highest Scores" field.

Place it in the "Highest Scores" custom calculation script. 

To make this script work

1) put a topic description into the tooltip text for each "Score-" field.

2) Make sure all of the radio buttons work for setting the "Score-" field value.

 

 

// First collect all of the "Score-" fields into an array
var strFldName, aScoreFlds = [];
for(var i=0;i<this.numFields;i++)
{
    strFldName = this.getNthFieldName(i);
    if(strFldName.indexOf("Score-") == 0)
        aScoreFlds.push(this.getField(strFldName));
}
// Now sort fields from highest to lowest
aScoreFlds.sort(function(a,b){return b.value - a.value;});
// Extract topic text for top 3 entries
var aTop3 = aScoreFlds.slice(0,3).map(function(a){return a.userName;});
// Display in field
event.value = aTop3.join("\n");

 

 

 

 

Here's a script that will sort out the lowest 3 scores

Place this script into the custom calculation for the "Lowest Scores" field

 

 

// First collect all of the "Score-" fields into an array
var strFldName, aScoreFlds = [];
for(var i=0;i<this.numFields;i++)
{
    strFldName = this.getNthFieldName(i);
    if(strFldName.indexOf("Score-") == 0)
        aScoreFlds.push(this.getField(strFldName));
}
// Now sort fields from lowest to highest
aScoreFlds.sort(function(a,b){return a.value - b.value;});
// Extract top 3 entries, which are the lowest scores
var aTop3 = aScoreFlds.slice(0,3).map(function(a){return a.userName;});
// Display in field
event.value = aTop3.reverse().join("\n");

 

 

 

 

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

Votes

Translate

Translate

Report

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
New Here ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

LATEST

Thank you so much you. That's worked a treat!!! And, I really appreciate you explaining each step so clearly 😊

Votes

Translate

Translate

Report

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 ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Sure, it's possible. Put all the values in an array, sort it (using a custom sort function), and then get the bottom three items from it for the lowest results (indices 0,1,2) and the top three items for the highest results (arrLength-1, arrLength-2, arrLength-3).

Votes

Translate

Translate

Report

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