Skip to main content
Friendly_Harmony1674
Participating Frequently
April 24, 2023
Answered

Need to calculate /score radio buttons based on selection

  • April 24, 2023
  • 2 replies
  • 3258 views

Hello all,

I need to calculate radio buttons based on their selection for scoring.  

Questions are horizontal/rows, while scoring is vertical/columns. I've found similar questions, but not quite what I need.   "Not at all" answers are 0, "Several Days" answers are 1, "More than Half the days" are 2, and "Nearly every day" are 3.  Any assistance is greatly appreciated!  

Question one group is named GAD01, question 2 is GAD02, etc to question 7 group named GAD07. I know the formula needs ot be plugged into the column total fields (GAD_Col1_Total, etc)

Your guidance is appreciated! Thank you

 

 

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

If you want to calculate a sum then there is no need to calculate the first column since it will always be zero.

Use this in "GAD Total Score" field;

 

var t1 = 0;
var t2 = 0;
var t3 = 0;
for(var i=1; i<=7; i++){
 if(this.getField("GAD0"+i).valueAsString == "1")t1++;
 if(this.getField("GAD0"+i).valueAsString == "2")
 t2+= Number(this.getField("GAD0"+i).valueAsString);
 if(this.getField("GAD0"+i).valueAsString == "3")
 t3+= Number(this.getField("GAD0"+i).valueAsString);}
this.getField("GAD_Col2_Total").value = t1;
this.getField("GAD_Col3_Total").value = t2;
this.getField("GAD_Col4_Total").value = t3;
event.value = t1+t2+t3;

 

 

 

 

2 replies

Alena_J
Participating Frequently
June 4, 2024

Thanks for the thread, helped me as well...

 

PS: I might have severe anxiety O.o

Thom Parker
Community Expert
Community Expert
April 24, 2023

This code depends on the export value of the radio buttons being the numbers you've specified. You didn't explicitly say this was the case, but I'm assuming that's want you meant. 

 

You also didn't specify what you meant by column total? Are you just counting the number of buttons checked in a column? or adding the export values?

 

The code below counts the buttons checked in the first column.

 

 

// Calculation for column 1, (Not at all) assuming export value is 0
var nSum = 0;
for(var i=1;i<=7;i++)
{
    if(this.getField("GAD0" + i).value == 0)
       nSum++;
}
event.value = nSum;


    

 

 

This code could be greatly simplified (and generalized) if group naming was used for the radio button fields. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Friendly_Harmony1674
Participating Frequently
April 25, 2023

Hi Thom,

Awesome! Thank you for your kindness in asking for clarification. 

Yes, I need to total up the export values for each individual column.  All export values in first column (titled "Not at all") are Zero.  All export values in second column are 1, All export values in third column are 2, and all export values in last column (titled Nearly Every Day) are 3.

Group naming is used for the questions/field:

1 is named GAD01

2 is named GAD02

3 is GAD03

4 is GAD04

5 is GAD05

6 is GAD06

7 is GAD07

Thank you so much - this will make our non-profit therapist super happy to have it automatically score! 

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
April 25, 2023

If you want to calculate a sum then there is no need to calculate the first column since it will always be zero.

Use this in "GAD Total Score" field;

 

var t1 = 0;
var t2 = 0;
var t3 = 0;
for(var i=1; i<=7; i++){
 if(this.getField("GAD0"+i).valueAsString == "1")t1++;
 if(this.getField("GAD0"+i).valueAsString == "2")
 t2+= Number(this.getField("GAD0"+i).valueAsString);
 if(this.getField("GAD0"+i).valueAsString == "3")
 t3+= Number(this.getField("GAD0"+i).valueAsString);}
this.getField("GAD_Col2_Total").value = t1;
this.getField("GAD_Col3_Total").value = t2;
this.getField("GAD_Col4_Total").value = t3;
event.value = t1+t2+t3;