Skip to main content
флорианб70160451
Known Participant
December 10, 2016
Question

Highlight smallest value in green and highest value in red

  • December 10, 2016
  • 1 reply
  • 2112 views

Dear all ,

My form has 5 fields (field #1 -  field #5) with numbers (prices). Is there any javascript, with which I can identify the lowest price (automatically highlighted in green color and the highest in red color? Important if one field value is empty = 0, than 0 should be not considered as number.

In addition the script should populate the lowest vaue in field #6, the average value in field #7 and the highest field value in field #8.

I would be greatfull for any ideas and comments.

BR

Florian

This topic has been closed for replies.

1 reply

Inspiring
December 10, 2016

Very similar to my answer to your other question, but a lot easier because we are working with field values that are straight up numbers, and don't have to first be converted to date objects.

//make an array of date values

var valArray = [[], []];

var total = 0;

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

    var Nm = "field #" + i, fld = this.getField(Nm);

    fld.textColor = color.black; //reset all the field colours

    if (fld.value && !isNaN(fld.value)) {

        valArray[0].push(fld.value);

        total += fld.value;

        valArray[1].push(Nm);

    }

}

//get the max value

var maxVal = Math.max.apply(null, valArray[0]);

var maxIndex = valArray[0].indexOf(maxVal);

var maxFld = this.getField(valArray[1][maxIndex]);

if (maxFld) maxFld.textColor = color.green;

this.getField("field #8").value = maxVal;

//get the min value

var minVal = Math.min.apply(null, valArray[0]);

var minIndex = valArray[0].indexOf(minVal);

var minFld = this.getField(valArray[1][minIndex]);

if (minFld) minFld.textColor = color.red;

this.getField("field #7").value = minVal;

//get the average value

var avgVal = total / valArray[0].length;

this.getField("field #6").value = avgVal;

флорианб70160451
Known Participant
December 13, 2016

Hello ZoPaars,

firstly many thanks for your support. Unfortunately not working correctly.

As I understod I need to make an Custom calculation script of following:

  1. //make an array of date values 
  2. var valArray = [[], []]; 
  3. var total = 0
  4. for (var i = 1; i < 6; i++) { 
  5.     var Nm = "field #" + i, fld = this.getField(Nm);      //Here I need to put the filed names, which need to be cpalculated? I tried "PreisA11", "PreisB11", etc.
  6.     fld.textColor = color.black; //reset all the field colours 
  7.     if (fld.value && !isNaN(fld.value)) { 
  8.         valArray[0].push(fld.value); 
  9.         total += fld.value; 
  10.         valArray[1].push(Nm); 
  11.     } 

Do I need to put this in textfields calcualtion?

  1. //get the max value 
  2. var maxVal = Math.max.apply(null, valArray[0]); 
  3. var maxIndex = valArray[0].indexOf(maxVal); 
  4. var maxFld = this.getField(valArray[1][maxIndex]); 
  5. if (maxFld) maxFld.textColor = color.green; 
  6. this.getField("field #8").value = maxVal; 
  7.  
  8. //get the min value 
  9. var minVal = Math.min.apply(null, valArray[0]); 
  10. var minIndex = valArray[0].indexOf(minVal); 
  11. var minFld = this.getField(valArray[1][minIndex]); 
  12. if (minFld) minFld.textColor = color.red; 
  13. this.getField("field #7").value = minVal; 
  14.  
  15. //get the average value 
  16. var avgVal = total / valArray[0].length; 
  17. this.getField("field #6").value = avgVal; 
флорианб70160451
Known Participant
December 14, 2016

You are probably best of adding this as a document level JavaScript function. To do that, search for the "Document JavaScripts" tool in the 'Tools' tab of Adobe Acrobat Pro DC. Note that if you have Adobe Acrobat Reader, you can't add any JavaScript.

Your code is almost right, but you should put the names of your fields in an array. I assumed that your fields were named "Field #1", "Field #2" etc. But your field names do not include any incremental enumeration, thus the loop won't work unless you have an array with the field names that you loop through.

I would recommend putting the following function as a document-level JavaScript:

function calcFields() {

    //make an array of the numbers and the field names that correspond with them

    var valArray = [[], []];

    var total = 0;

    var NamesArr = ["PreisA11, PreisB11", "PreisC11", "PreisD11"];

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

        var Nm = NamesArr, fld = this.getField(Nm);

        fld.textColor = color.black; //reset all the field colours

        if (fld.value && !isNaN(fld.value)) {

            valArray[0].push(fld.value);

            total += fld.value;

            valArray[1].push(Nm);

        }

    }

    //get the max value and make it red

    var maxVal = Math.max.apply(null, valArray[0]);

    var maxIndex = valArray[0].indexOf(maxVal);

    var maxFld = this.getField(valArray[1][maxIndex]);

    if (maxFld) maxFld.textColor = color.red;

    this.getField("MAX1").value = maxVal;

    //get the min value and make it green

    var minVal = Math.min.apply(null, valArray[0]);

    var minIndex = valArray[0].indexOf(minVal);

    var minFld = this.getField(valArray[1][minIndex]);

    if (minFld) minFld.textColor = color.green;

    this.getField("MIN1").value = minVal;

    //get the average value

    var avgVal = total / valArray[0].length;

    this.getField("AV1").value = avgVal;

};

I only see four field names in your other comments, but you can add as many as you want to the NamesArr.

Then you need the fields to call upon this function as soon as somebody 'commits' the value to the field.

You can do this by running the code below from the JavaScript console in Acrobat (open it with Ctrl+J).

//Add the formula to the four fields. Only run this code once, from the console!

var NamesArr = ["PreisA11, PreisB11", "PreisC11", "PreisD11"];

for (var i = 0; i < NamesArr.length; i++) this.getField(NamesArr).setAction("OnBlur", "calcFields();");


Hello ZoPaars, many thanks for your quick reply. Still facing with pproblems.

In my opinion the root problem is, that I dont know how to work with Arrays....

  • valArray = [[], []];  Do I need to ofill in the fildnames, where user puts in variable prices? Following way correct? var valArray = [["PreisA11"], ["PreisB11"], ["PreisС11"], ["PreisD11"]];

    Do do I need

  • var NamesArr = ["PreisA11", "PreisB11", "PreisС11", "PreisD11"];  Correct?