Skip to main content
simon-1854
Known Participant
January 16, 2017
Question

How do you output a value using the Custom Calculation Script box?

  • January 16, 2017
  • 1 reply
  • 2632 views

Hi there,

I have a form and am trying to calculate a Total, but when I run the form, the field stays blank and my knowledge is limited. The javascript reads as follows:

Many thanks.

// First we need our array

arr = [

q1,

q2,

q3,

q4,

q5,

q6,

q7,

q8,

q9,

q10,

q11,

q12,

q13,

q14,

q15,

q16,

q17,

q18,

q19,

q20,

q21,

q22,

q23,

q24,

q25,

q26,

q27,

q28,

q29,

q30,

q31,

q32,

q33,

q34,

q35,

q36,

q37,

q38,

q39,

q40,

q41,

q42,

q43,

q44,

q45,

q46,

q47,

q48,

q49,

q50,

q51,

q52,

q53,

q54,

q55,

q56,

q57,

q58,

q59,

q60,

q61,

q62,

q63

];

// This variable will be our total score

var score = 0;

// This variable will be the number of valid good practice indicators

var countValid = 0;

// We iterate through all the answers and establish both the score and the number of valid good practice indicators

for (var i = 0; i < arr.length; i++) {

    if (arr == 0 || arr == 1 || arr == 2) {

        score = score + arr;

        countValid++;

    };

}

// Now we have our precentage completion figure

var percentCompletion = score / (2 * (countValid));

event.value = percentCompletion;

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
January 16, 2017

Where did you define the values of all of the qX variables in your array?

simon-1854
Known Participant
January 16, 2017

Hi thanks for quick response.

So this form has 63 questions, and for each question through indesign i have labelled them q1 -> q63.

Screenshot may help,  i guess not quite sure how to grab those fields as never used CCS in acrobat before and jscript not my forte.

Simon.

try67
Community Expert
Community Expert
January 16, 2017

Ah OK, so these are field names... In that case you'll need to use the getField method to access them and then the value (or valueAsString) property to get their values. Also, since they are consistently named there's no real need for an array.

Use this code instead of your current for-loop, and you can drop the array definition:

var numQuestions = 63;

for (var i = 1; i <= numQuestions; i++) {

    var f = this.getField("q"+i);

    if (f==null) {

        app.alert("Error! No field called: q" + i);

        continue;

    }

    var answer = f.valueAsString;

    if (answer == "0" || answer == "1" || answer == "2") {

        score = score + Number(answer);

        countValid++;

    };

}