• Global community
• Language:
• Deutsch
• English
• EspaĆ±ol
• FranĆ§ais
• PortuguĆŖs
• ę„ę¬čŖć³ćć„ććć£
Dedicated community for Japanese speakers
• ķźµ­ ģ»¤ė®¤ėķ°
Dedicated community for Korean speakers
Exit
• Calculate average with color conditional formattin...

# Calculate average with color conditional formatting

New Here ,
Aug 12, 2024 Aug 12, 2024

Copied

I want to create a form where you enter values x1-x8 which return an average (avg), and if the average is <0,25 and >0,50 the field should turn red.

I have used the following script in "Custom calculation script":

``````var total = 0;
var avg = 0;
for(var i=1; i<=8; i++){
if(this.getField("x"+i).valueAsString != "" && Number(this.getField("x"+i).valueAsString) != 0){
total += Number(this.getField("x"+i).valueAsString);
avg++;}}

if(avg != 0)
event.value = total/avg;
else
event.value = "";

var v = Number(event.value);
if (v<0,25 && v>0,50) {event.target.fillColor = color.red;}
else if (v>=0,25 && v<=0,50) {event.target.fillColor = color.transparent;}``````

The problem is that if the field turns red it stays red - it does not go back to transparent when the average value changes.

TOPICS
JavaScript , PDF forms

Views

53

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
2 Replies 2
Community Expert ,
Aug 12, 2024 Aug 12, 2024

Copied

Change 0,25 to 0.25

and 0,50 to 0.50

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 Expert ,
Aug 12, 2024 Aug 12, 2024

Copied

LATEST

It's not possible for a number to be both less than 0.25 and greater than 0.50 at the same time, you wanted to use || (or) instead of && (and):

if (v < 0.25 || v > 0.50)

You may also want to check that the field is not empty or 0 before changing color.