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

Highlight smallest value in green and highest value in red

  • December 10, 2016
  • 23 replies
  • 2284 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.

23 replies

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 13, 2016

I tried this...But for sure there is a mistake...

//make an array of date values 

var valArray = [[], []]; 

var total = 0; 

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

    var Nm = ("PreisA11,PreisB11","PreisC11","PreisD11")+ 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("MAX1").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("MIN1").value = minVal; 

 

//get the average value 

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

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