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

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

New Here ,
Jan 16, 2017 Jan 16, 2017

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;

TOPICS
PDF forms
2.4K
Translate
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 ,
Jan 16, 2017 Jan 16, 2017

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

Translate
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 ,
Jan 16, 2017 Jan 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.

acrobat.jpg

Simon.

Translate
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 ,
Jan 16, 2017 Jan 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++;

    };

}

Translate
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 ,
Jan 16, 2017 Jan 16, 2017

Much appreciated you responding so quickly, will give it a go.

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

Hi, thanks again for the help, just about there but have an issue with a form field value not resetting once a value has been added.

e.g. If we enter a value of 2 in a cell and then delete that, it still retains a value of 2 rather than zero?

Thanks again, Simon.

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

The script will only update the value of the field when you exit the field you're editing, not while you're still editing it.

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

Hi thanks for the info, the form value seems to remain even after exiting that field?

If we overwrite that '2'with a "1" then that 1 registers fine but if we replace that value with a n/a then the value of "2" is still registered?

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

I don't quite understand what you mean, I'm afraid... If you enter "n/a" it will not be counted at all.

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

Sorry, don't quite understand it myself.

So in the above image, you will see the questions, lets say on question 60 we enter in a value of "2", we go to the next field and realise that "2" should have been "n/a" or the value deleted it all together (left blank), so we delete the "2" and enter "n/a".

The total script at the end should calculate the percentage, but it still seems to include the "2" value rather than nothing.

The calculation script is:

var score = 0;

var countValid = 0;

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++;

     } else if (answer == 'n/a' || answer == 'N/A') {

        f.value = 'n/a';

    } else {

        f.value = ''

    }

}

result = 100*(score / (2 * (countValid)));

event.value = Math.round(result);

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

Check the JS-Console for error messages. Also, why are you trying to change the value of the answer field in your code?
If you want to convert it to lower-case you need to do so in that field's custom validation script, not here.

Translate
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 ,
Jan 24, 2017 Jan 24, 2017

ok thanks again, we'll try that.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

Hi So looking at each field, we want to have some validation like, if the answer isn't 0,1 or 2 then we want to make sure the value is set to n/a?

So something like...

var answer = event.valueAddString;

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

    event.value = 'n/a';

}

Thanks again for all your help, much appreciated.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

The value of what? The answer field itself or the score completion field?

And your logical expression above is tautological. It will always be true.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

Hi sorry just saw that, we want the value of the answer field.

updated expression we're using is as below:

var answer = event.valueAsString;

if (answer != "0" && answer != "1" && answer != "2") {

event.value = 'n/a';

}

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

This is to go in the validation box of every QUESTION field. All we wan't is for the value to change to n/a if it is not 1, 2, or 0.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

Instead of this mess just use a drop-down with these four options, and "N/A" is the default one...

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

OK, we have a lot of forms to build so would be nice to know if our route could be done but appreciate your time.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

We have taken your advice and started using drop-downs instead of text inputs, which has made validation a lot simpler, however, we are still having the same issue with the percentage calculation. When the user answers only two questions with the answer '2' they are given a percentage of 100 because 4 is the maximum possible score if only two questions are applicable. This works fine with all results, for example, two answers of '1' gives a percentage of 50, however our problem occurs when we change an answer. If we answer '2', '2', '1' and '0' we get a percentage of 63 (62.5 rounded up) but if the '0' is later changed to an 'n/a' the percentage does not change to 83 as it should. We think there is an issue in our 'countValid' variable not updating the number of answers that are now valid (only 0, 1, and 2 are valid); perhaps you could take a look at our code and see what might be going wrong?

var score = 0;

var countValid = 0;

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++;

}

}

result = 100 (score / (2  (countValid)));

event.value = Math.round(result);

Many thanks.

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

Can you post the actual file?

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

Hi, form is here. sorry couldnt see where to upload it on here.

Dropbox - form

Translate
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 ,
Jan 25, 2017 Jan 25, 2017

You can't upload a file directly. Dropbox is fine.

I had a look at the file. First of all, there are some issues with your code. For some reason you insist on trying to change the value of the answer fields, even when they are drop-downs. Don't.

Also, the code above doesn't handle the situation where all the fields are N/A correctly.

So use this code instead:

var score = 0;

var countValid = 0;

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++;

    }

}

if (countValid==0) event.value = 0;

else {

    var result = 100*(score / (2 * (countValid)));

    event.value = Math.round(result);

}

As to the problem you described: I see it but I think it happens because you didn't enable the option to commit the value of the drop-down fields immediately (under Properties - Options). When that option is not ticked the value is only updated when you exit the field, not when you change it. Do that and you should see the result changing right away.

As a side-note, I think you can combine this script with the one of the Total field, which is basically the same except for the last part. That way you won't have duplicate identical codes and maintaining the code will be easier. Just add this code at the end and you'll be able to remove the other code entirely:

this.getField("total").value = score;

Translate
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 ,
Jan 25, 2017 Jan 25, 2017
LATEST

You're amazing, such a relief! Thank you, it works!!

Translate
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