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

Code assistance for calculation and color

Community Beginner ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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;}

TOPICS
How to , JavaScript , PDF , PDF forms

Views

1.1K

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
3 ACCEPTED SOLUTIONS
Community Expert ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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.

View solution in original post

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 ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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.

 

 

View solution in original post

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 ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

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

Do the same for "No".

View solution in original post

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 ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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.

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 Beginner ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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

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 ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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.

 

 

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 Beginner ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

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.

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 ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

This is incorrect:

if (totall >= 4452 && ("LitBS" >= 1500 || "AlgBS" >= 1500 || "BioBS" >= 1500)) {

You must remove the quotes around the names of the variables. Otherwise they are treated as strings.

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 ,
Jan 22, 2024 Jan 22, 2024

Copy link to clipboard

Copied

Oops!

Sorry, I edited the script above.

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 Beginner ,
Jan 23, 2024 Jan 23, 2024

Copy link to clipboard

Copied

That works perfectly! Thank you so much for the assistance, I could have never done that without you!

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 Beginner ,
Jan 23, 2024 Jan 23, 2024

Copy link to clipboard

Copied

It is possible.. say I want to have another field that does the same thing, but makes it so that all the cells have to be =>1500, can I just replace the || with //?

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 ,
Jan 23, 2024 Jan 23, 2024

Copy link to clipboard

Copied

No. Replace it with && (Logical AND) ...

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 Beginner ,
Jan 23, 2024 Jan 23, 2024

Copy link to clipboard

Copied

Ok, thank you! This is so different from what I write for excel! 

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 Beginner ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

I just realized when I did my final test on the form that the color is changing based on all of the conditions, but a yes or no is not populating in the cell. Any idea what I may need to tweak?

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 ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

What script does you use for the cell?

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 ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

Not sure to understand.

In which fields do you want to populate "yes/no"?

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 Beginner ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

The same field that is turning green or red, or is that not something you can have the same field do? I am writing the form for HS kids to use and after testing it, I realized it probably also needs an alpha value. I understand the red is no, green is yes, but for those who may be color blind or just not on the same page as me, I figured I needed to tweak it. 

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 ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

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

Do the same for "No".

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 Beginner ,
Jan 24, 2024 Jan 24, 2024

Copy link to clipboard

Copied

Thank you!

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 Beginner ,
Jan 25, 2024 Jan 25, 2024

Copy link to clipboard

Copied

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?

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 ,
Jan 25, 2024 Jan 25, 2024

Copy link to clipboard

Copied

LATEST

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.

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