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

Acrobat Javascript: Rating 1-5 represented as % value to gain average

New Here ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

I have a table with 10 rows in an acrobat doc. In one of the columns I need the user to rate the question 1-5. I figured it was easiest to do this with a text field where the user can write 1, 2, 3, 4 or 5. I need to represent these ratings as percentages where: 1=50% 2=75% 3=90% 4=95% 5=100%. At the bottom of the table is a total box where I need the average percentage of the 10 boxes.... any ideas? Im looking to replicate the excel spreadsheet below

Screen Shot 2018-04-30 at 16.10.13.png

TOPICS
Acrobat SDK and JavaScript

Views

1.5K

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

correct answers 1 Correct answer

Engaged , May 02, 2018 May 02, 2018

All in all, "Actual Score" will have this script:

var valSum = 0

var qty = 10

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[0])

}

event.value = valSum

and "Bench....."  will have this script:

var valSum = 0

var qty = 10

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[1])

}

valSum /= qty

event.value = valSum

I highligthed the

...

Votes

Translate

Translate
Engaged ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

You should use dropdown list instead as you can set values of 1 to 5 as well as exportValues 50 to 100 as you mentionned.

Then, your script would calculate the average between the exportValues and add "%" to the final string.

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
LEGEND ,
Apr 30, 2018 Apr 30, 2018

Copy link to clipboard

Copied

You can use or a drop down box with export value set to the decimal value of the percentage assigned to choice and a no selection set to 0. The result should be in a text field set to the "Percentage" format. You can then use the "Field is the _____ of the following fields" option and set the action to "average" and then select the specific fields to include in the calculation. Using the "Percentage" format will automatically add the "%" sign and adjust the decimal point for the percentage result display.

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 ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

Many thanks this has worked fine and now adds up to the correct % for 'bench mark achieved'.
What I now need to know is how to get the total for 'Actual score for works'.

I've set the format to 'number' and then in 'calculate' - 'value is the sum+ of the following fields' - but rather than adding up the 0-5 rated values it is just adding the amount of fields. So if I have 5 rated in each of the 10 boxes the total should be 50, but is showing as 10. Any ideas?

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
Engaged ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

When entering an export value that is different than the value chosen by the user, it is that export value that is returned by the value property.

If you wish to calculate both values, we will have to cheat and create a custom calculate script

name each of your field the same way:  Dropdown.1, , Dropdown.2 , etc

replace each export value with a string representing the association of the value and the export value needed separated by a hyphen "-"

export value = 1-0.5      2-0.75     etc

then, use this script to calulate:

//Declare a variable that will be used to sum

var valSum = 0

//Number of fields to scan

var qty = 10

//Loop through all 10 fields (I assumed you have 10)

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

//Separate value from export value and store them in an array

var aExpVal = myExpVal.split("-")

//Indice [0] for value, [1] for export value, sum eveything

valSum += Number(aExpVal[0])

}

valSum /= qty  //Remove this part if you want sum not average

event.value = valSum

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
LEGEND ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

I enter the decimal value into the export value and then use the number constrictor to convert the string number character to a number.

If the first calculation option is used, there is no need for any of this since the background JavaScript functions will perform the necessary calculations. Note this average method assumes that all fields will be used in the calculation of the average.

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
Engaged ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

My understanding of her problem is she wants to calculate both the average from the export value which is the percentages AND the sum of the values (1 to 5) chosen by the user to 2 different fields.

I believe that when she assigns an export value to one of the choices, the value property of the field returns that export value and the original value is not returnable.  This is why I designed a way to export both values in the same calculation and split them appart afterwards.

She could also have used a JSON object to associate values and export values or a 2 associative arrays.

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Hi both

Many thanks for your help so far - it's been great.
I am a complete novice to javascript and am now a little lost...

So to confirm, I followed gkaiseril's advice:

You can use or a drop down box with export value set to the decimal value of the percentage assigned to choice and a no selection set to 0. The result should be in a text field set to the "Percentage" format. You can then use the "Field is the _____ of the following fields" option and set the action to "average" and then select the specific fields to include in the calculation. Using the "Percentage" format will automatically add the "%" sign and adjust the decimal point for the percentage result display.

This has worked perfectly - when all 10 fields are rated as '5' I end up with 100% in the 'Bench mark achieved' box.

Mat Lac - what you said: My understanding of her problem is she wants to calculate both the average from the export value which is the percentages AND the sum of the values (1 to 5) chosen by the user to 2 different fields.

Is correct. the user will select a rating 0-5 and then I need the sum of these 10 values to appear in the box 'Actual score for works' (see screenshot below).
I tried following your script but I apologise, I'm not sure what is meant to go where - as I said I'm a complete novice...

Thanks in advance!

Kirst

Screen Shot 2018-05-02 at 13.00.38.png

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
Engaged ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

I inserted comments in the script for you to decipher what everything does but it is not mandatory for you to understand it. Comments, in JS, are preceded by these ---->  //

like this:

//this is a comment

they are not interpreted by JS so you can type whatever.  Programmers use them to insert hints, explain behaviors, or simply to prevent a section to execute while debbuging.

You can comment a whole section like this:

/*

this si a comment

this is too

*/

That being said, you already calculated the average of some fields by using the calculate tab of the field (I'm guessing a name) "maxScorePossible".  You used the first box on that tab which asks you to choose check which field to calculate.

the second box lets you script with simplified notation which is somethings like (2+2)*8 / (4+2)

Now, the third box lets you create your own calulate script.  This is what you need.  You will copy the scipt I wrote in the 3rd box of the calculate tab of the "actualScore" field.  Do not forget to remove the line as explain by one of the comments in the script and do not forget to rename your dropdown lists since the script uses a loop.

Here again is the script, without the comments:

var valSum = 0

var qty = 10

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[0])

}

event.value = valSum

Don't hesitate if you need more help

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Ok so I've named all the drop down boxes - named Dropdown 1 - Dropdown 10

Screen Shot 2018-05-02 at 14.30.51.png

I still have the options for the properties set up as before:

Screen Shot 2018-05-02 at 14.28.15.png

I've copied the script as above:

Screen Shot 2018-05-02 at 14.33.20.png

But the Actual score box is not working - it's just just showing as 0.

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Change:

var myExpVal = this.getField("Dropdown."+x).valueAsString

To:

var myExpVal = this.getField("Dropdown "+x).valueAsString

Also, tick the option to commit the selected value immediately, under the properties of the drop-downs.

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Tried this and now the actual score is appearing as '10' - the number of fields as opposed to the total of the ratings which should be '50'

Screen Shot 2018-05-02 at 14.52.46.png

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
Engaged ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

All in all, "Actual Score" will have this script:

var valSum = 0

var qty = 10

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[0])

}

event.value = valSum

and "Bench....."  will have this script:

var valSum = 0

var qty = 10

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[1])

}

valSum /= qty

event.value = valSum

I highligthed the differences between the two

Now the later one doesn't take into account a N/A box.  It will attempt to average all ten fields even if only one is filled.  If you might encounter a situation where the user will not make 10 choices, you need to adjust the second script (the first one only adds 0 to the sum so it is not a problem)

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
Engaged ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

To adress this problem, we will use another variable as our divisor

var valSum = 0

var qty = 10

var divisor = 1

for (var x=1;x<=qty;x++){

var myExpVal = this.getField("Dropdown."+x).valueAsString

var aExpVal = myExpVal.split("-")

valSum += Number(aExpVal[1])

if (Number(aExpVal[1]) != 0) divisor++

}

if (divisor > 1) divisor--

valSum /= divisor

event.value = valSum

What this does is while cycling through our 10 fields, it will increment our divisor each time the export value is not 0 (meaning it is not N/A).  I initiated divisor to 1 to avoid a /0 error.  After our divisor is set (after the loop), I decrement it once to remove the initial 1.

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Ha ha - thanks for your patience!!

Ok so one more question - in the export values - where I have 'N/A' would the export value for this be 'N/A-0' ??

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
Engaged ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

As said earlier, use 0-0

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 ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

LATEST

We have lift off! Much appreciated.
For your sanity - I promise to stick to the day job!!

Thanks so much, have a great day

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
Engaged ,
May 02, 2018 May 02, 2018

Copy link to clipboard

Copied

Because you did not rename de boxes as I said, lol.  This is important.  I said "Dropdown.1" , not "Dropdown 1".  I used a DOT (.) not a space.

It doesn't really matter what name you use, as long as you refer to the same one when calling the getField() method.  The only thing that matters is the numbers incrementing from 1 to 10

You also need to change the export value as this:  1-0.5 2-0.75 etc and make sur N/A has an export value of 0-0 or it will try to sum the "N/A" as a string.

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