Skip to main content
Participating Frequently
January 22, 2024
Answered

Code assistance for calculation and color

  • January 22, 2024
  • 3 replies
  • 1704 views

I am hoping someone familiar with java can help. (I am new to this type of script). I created a pdf form and am struggling with formatting 2 of the cells. The first cell is a drop down. I want it to fill green if "pass" is chosen and red if "fail" is chosen, but remain white if neither are chosen. Here is what I came up with, but I keep getting a syntax error:

 

if (event.value=="Pass") {
event.target.fillColor = color.green;
if (event.value=="Pass") {
event.target.fillColor = color.green;
if (event.value=="Fail") }
event.target.fillColor = color.green;
} else {
event.target.fillColor = color.white;
}

 

The other cell I am trying to format needs to sum the values of 3 other cells and if they are =>4452 AND at least one of the cells is >=1500 I need the cell to fill green. If both statements are not true, I need it to fill red. 

 

This is what I have come up with so far. I am not getting an error, but it is also not working:

 

let "LitBestScore" = /* your value */;

let "AlgBestScore" = /* your value */;

let "BioBestScore" = /* your value */;

 

// Check if the sum is >= 4452 and at least one cell is 1500 or higher

if (("LitBestScore" + "AlgBestScore" + "BioBestScore") >= 4452 && ("LitBestScore" >= 1500 || "AlgBestScore" >= 1500 || "BioBestScore" >= 1500)) {

    console.log("Yes");

} else {

    console.log("No");

}

var v = Number(event.value);

if (v>=0 && v<=4451) {event.target.fillColor = color.red;}

else if (v>4451) {event.target.fillColor = color.green;}

This topic has been closed for replies.
Correct answer Nesa Nurani

If you want to show Yes/No in a field, then change: console.println("Yes") to: event.value = "Yes"

Do the same for "No".

3 replies

Participating Frequently
January 25, 2024

Not sure if anyone has any suggestions, but here is the form I created for our students (https://drive.google.com/file/d/1ry4SWJPZAMU0WA64eWow3w8qpCUoaXTJ/view?usp=sharing). It works beautifully on my computer and on teacher computers that have the full version of adobe installed. Our students use a chromebook with adobe pdf viewer and the form will calculate, but it is ignoring the script that changes the color to green when they have met that requirement. Anyone have any suggestions/work arounds, etc for why this isn't working and how to make it function?

try67
Community Expert
Community Expert
January 25, 2024

The mobile version of Reader has very poor support for scripts, unfortunately.

Instruct your students to view it on a regular computer if they want it to work correctly.

JR Boulay
Community Expert
Community Expert
January 22, 2024

2.

Java and JavaScript share only the first 4 letters of their names. They are different languages.

You must not use "let" or "console.log" in Acrobat JavaScript since they are not supported.

 

Try this (not tested) as a Calculation script in the Total field, you may need to edit the field's names:

var LitBS = Number(this.getField("LitBestScore").value);
var AlgBS = Number(this.getField("AlgBestScore").value);
var BioBS = Number(this.getField("BioBestScore").value);
var totall = LitBS + AlgBS + BioBS;

// Check if the sum is >= 4452 and at least one cell is 1500 or higher
if (totall >= 4452 && (LitBS >= 1500 || AlgBS >= 1500 || BioBS >= 1500)) {
	event.target.fillColor = color.green;
	console.clear();
	console.println("Yes");
}
else {event.target.fillColor = color.red;
	console.clear();
	console.println("No");
}

 

 

Edit: unwanted quotes removed.

 

 

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
January 22, 2024

Gotcha. I have never worked with java or adobe like this before. I have all the code written in excel, but was told we could not use that format for this form. 

I just dropped that into my cell, but it turns it green regardless of the sum of the numbers. Would it be easier to have one cell that calculates the value and have a separate, larger cell behind it that just turns red or green around it based on whether the number in the cell is >= 4452? It still needs to read to see that at least 1 of the BestScore cells (the ones we are pulling the sum on) is at least 1500. I wasn't sure if it would be easier to pull them apart.

JR Boulay
Community Expert
Community Expert
January 22, 2024

1.

Use this as a Validation script:

 

 

if (event.value=="Pass") {event.target.fillColor = color.green;}
else if (event.value=="Fail") {event.target.fillColor = color.red;}
else {event.target.fillColor = color.white;}

 

 

And be sure to tick the "Commit Selected Value Immediatly" option under the Options tab.

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
January 22, 2024

Thank you JR, that worked perfectly for my fill issue! 🙂