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

Conditional formatting of calculated form field (2 conditions)

Explorer ,
Jan 30, 2023 Jan 30, 2023

I'm making a graduation assesment form. (Grades are 1 - 10)

The final grade FG is the (weighted) average of several partial grades PG (PG_1, PG_2 etc). The partial grades are calculated as the average of the grades for subgoals (1.1_grade, 1.2_grade etc; 2.1_grade, 2.2_grade etc). All calculations in my form work well, but there's 2 finetuning issues… Hope someone is willing to help or at least point me in some direction…

 

Question 1. The partial grade PG is supposed to show only after the subgrades are filled. In the same way, final grade FG doesn't show until all the partial grades are filled (= all 22 or so subgrades are filled).

I've solved this problem now using a validation script using text color: text color of partial grade is white (thus: invisible) until all subgrades (default: 0) are filled in.

 

 

var g11 =  this.getField("1.1_grade").valueAsString;
var g12 =  this.getField("1.2_grade").valueAsString;
var g13 =  this.getField("1.3_grade").valueAsString;
var g14 =  this.getField("1.4_grade").valueAsString;
var g15 =  this.getField("1.5_grade").valueAsString;

if (g11 == 0 || g12 == 0 || g13 == 0 || g14 == 0 || g15 == 0) {event.target.textColor = color.white ;}

else {event.target.textColor = color.black ;}

 

 

This works pretty well for the partial grades, but the above statement gets pretty long for the final grade (declaring all the 22 variables one by one and then making the if-statement).

 

==> is there a better/easier/cleaner way to do this?

 

Question 2: I want FG to show red when one of the PG is below 6.0 (or gives anothes type of 'alert', because that means the student has failed…). How can I achieve that - combined with condition 2?

TOPICS
JavaScript , PDF forms
2.3K
Translate
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 31, 2023 Jan 31, 2023

Remove that code and use this one as the field's custom Calculation script:

 

 

var gradeFields = ["1.1_grade", "1.2_grade", "1.3_grade", "1.4_grade", "1.5_grade", "2.1_grade"]; // etc.
var allFilled = true;
var anyBelow6 = false;
var total = 0;
for (var i=0; i<gradeFields.length; i++) {
	var f = this.getField(gradeFields[i]);
	if (f.valueAsString=="") {
		allFilled = false;
		break;
	}
	var grade = Number(f.valueAsString);
	if (grade<6) anyBelow6 = true;
	total+=grade;
}

if (allFilled) {
	event.value = total;
	if (anyBelow6) {
		event.target.textColor = color.red;
	} else event.target.textColor = color.black;
} else {
	event.value = "";
}

 

Edited: Sorry, thought it was 0.6, not 6... Fixed now.

 

Translate
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
Explorer ,
Jan 31, 2023 Jan 31, 2023

Thank you for your fast reply, @try67 !

Your code looks wonderful but what's missing here was the original calculation (see below); and it's too complicated for me to know where to add the calculation...

event.value =  0.4 * (this.getField("PG_1").value) + 0.3 * (this.getField("PG_2").value) + 0.2 * (this.getField("PG_3").value) + 0.1 * (this.getField("PG_4").value);

 

Translate
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
Explorer ,
Jan 31, 2023 Jan 31, 2023

And I'm also trying to round the value to the nearest half. 
I read a lot of threads about it, but can't get it to work... (like https://community.adobe.com/t5/acrobat-sdk-discussions/how-to-hide-trailing-zeros-in-a-number-roundi... and https://community.adobe.com/t5/illustrator-discussions/round-to-nearest-quarter/td-p/12531076).

Translate
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 31, 2023 Jan 31, 2023

If the code above is the full calculation then remove the total variable from my code, and replace this line:

event.value = total;

With this:

 

var v = 0.4 * Number(this.getField("PG_1").value) + 0.3 * Number(this.getField("PG_2").value) + 0.2 * Number(this.getField("PG_3").value) + 0.1 * Number(this.getField("PG_4").value);
event.value =  (Math.round(v * 2) / 2).toFixed(1);

 

Translate
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
Explorer ,
Jan 31, 2023 Jan 31, 2023

The calculation works perfect! But the FG should be red only when 1 or more of the PG are < 6.0.

In your code the red color is based on the subgoalgrades. 

Translate
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 31, 2023 Jan 31, 2023

I'm a bit lost... So you calculate the total based on the PG_X fields, but want to only show it based on the values of the Y_grade fields? Is there are relation between the names of the two, or are they totally different?

Translate
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
Explorer ,
Jan 31, 2023 Jan 31, 2023

Yes, you got it,@try67!
Total = final grade FG shouldn't show until all 22 subgoals (1.1_grade, 1.2_grade, ... 2.1_grade, 2.2_grade... etc = subgoalgrades ;)) have a grade. 
The simple average of these 22 subgoalgrades (in groups) lead to 4 partial grades (PG_1 ... PG_4; for 4 learning objectives).  These PGs are invisible (text color white) until all suboalgrades are filled. 
The real and final grade (FG) is calculated as a weighted average of the partial grades (as in the calculation shown above) and should be:
1. rounded to halves;
2. invisible until all subgoalgrades are filled;
3. red when one of the partial grades PG is <6.0.
Does this make things more clear...?

Translate
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 31, 2023 Jan 31, 2023

Try this:

 

var pgFields = ["PG_1", "PG_2", "PG_3", "PG_4"];
var anyBelow6 = false;
var total = 0;
for (var i=0; i<pgFields.length; i++) {
	var v = Number(this.getField(pgFields[i]).valueAsString);
	if (v<6) anyBelow6 = true;
	total+=(0.4-(i*0.1))*v;
}

var gradeFields = ["1.1_grade", "1.2_grade", "1.3_grade", "1.4_grade", "1.5_grade", "2.1_grade"]; // etc.
var allFilled = true;
for (var i=0; i<gradeFields.length; i++) {
	var f = this.getField(gradeFields[i]);
	if (f.valueAsString=="") {
		allFilled = false;
		break;
	}
}

if (allFilled) {
	event.value =  (Math.round(total * 2) / 2).toFixed(1);
	if (anyBelow6) {
		event.target.textColor = color.red;
	} else event.target.textColor = color.black;
} else {
	event.value = "";
}
Translate
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
Explorer ,
Jan 31, 2023 Jan 31, 2023

Almost there! (I'm sorry to keep bothering you, but I'm so happy with your help!!)
The condition for the red color red works perfect! Only... the Final Grade shows always (starting at 0.0 (red) and climbing up when filling the fields)... Something with 'allFilled'? Or did I do something wrong...? 

Translate
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 31, 2023 Jan 31, 2023

I'll need to see the actual file to be able to help you further with this.

 

Translate
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
Explorer ,
Feb 01, 2023 Feb 01, 2023

I tried to send you the file in a personal message (my client won't be thrilled to find his form online...) but file type pdf is not supported. Can I send it to you in another way...? 

Translate
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 ,
Feb 01, 2023 Feb 01, 2023
LATEST

I sent you my email address in a PM.

Translate
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