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

Form JavaScript help needed: Need to get value of a field (SCORE) and return text (PASS/FAIL)

New Here ,
Apr 05, 2020 Apr 05, 2020

Copy link to clipboard

Copied

I do not have any JavaScript experience and am hoping that someone can assist me with a Adobe form calculation.
I am creating a Pass/Fail scorecard that will take a value from a field (Calc_OScore) and then return a word (Perfect!, Pass, Fail) based on the score.

Due to my lack of experience and ignorance, my attempts to make this work have been nothing more than copy/paste trial and error from others' scripts and experiences.
I've tried this:

var text ;
if ("Calc_Score" == 16) {
text = "PERFECT!!";
} else if ("Calc_Score" > 11) {
text = "PASS";
} else {
text = "FAIL";
}

And one of these:

var int1=this.getField("E_score").value;
var int2=this.getField("S_score").value;
var int3=this.getField("P_score").value;
var int4=this.getField("C_score").value;
var tSum = Number(int1+int2+int3+int4);
var rVal = 'NULL';
if(tSum===16){
rVal='PERFECT SCORE!';
}else if(tSum>11){
rVal='NOT PERFECT BUT OKAY!';
}else{
rVal='FAIL';
}
return rVal;

 Can anyone help out here? If it makes it easier to understand what I'm trying to accomplish, here's what my formula in Excel looked like: =IFS(F31=16,"PERFECT!",F31>11,"PASS",TRUE,"FAIL"), where "F31" is now "Calc_OScore".

 I appreciate and welcome all feedback! 

TOPICS
Acrobat SDK and JavaScript

Views

749

Likes

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

correct answers 1 Correct answer

Community Expert , Apr 06, 2020 Apr 06, 2020

Opps, I didn't make enough changes to your code.

Here's the correct version:

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    event.value = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    event.value = "PASS";
} else {
    event.value = "FAIL";
}

Likes

Translate

Translate
Community Expert ,
Apr 06, 2020 Apr 06, 2020

Copy link to clipboard

Copied

Does the "Calc_OScore" calculate the sum now?

You are sort of close. Your code has the correct structure, but is missing some important bits.

Youc an find out more about how to write scripts for calculations here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations/

https://www.acrobatusers.com/tutorials/conditional-execution/

 

Put this code into the custom calculation script in the field that displays the text.

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    text = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    text = "PASS";
} else {
    text = "FAIL";
}

 
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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
New Here ,
Apr 06, 2020 Apr 06, 2020

Copy link to clipboard

Copied

@Thom_Parker

Thanks for checking this out for me.

"Calc_Score" is the field that adds up the scores from 4 different sections of the score card.  The Simplified field notation is E_score + S_score + P_score + C_score.  This calculation is working.

"O_Score" is the field that I would like the text to appear.  When I entered the code you provided into the Custom calculation script, it does not appear to have worked.  

In Preview, I updated the scores in each of the 4 sections and confirmed that "Calc_Score" returned the expected value (in this case, 16).  "O_Score" did not change, nor did it return any error.  

 

Screenshot_1.png

 

Likes

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 ,
Apr 06, 2020 Apr 06, 2020

Copy link to clipboard

Copied

Opps, I didn't make enough changes to your code.

Here's the correct version:

var calcVal = this.getField("Calc_Score").value;
if(calcVal == "")
   event.value = "";
if (Number(calcVal) == 16) {
    event.value = "PERFECT!!";
}else if (Number(calcVal) > 11) {
    event.value = "PASS";
} else {
    event.value = "FAIL";
}
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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
New Here ,
Apr 06, 2020 Apr 06, 2020

Copy link to clipboard

Copied

@Thom_Parker

Incredible!  It works perfectly.  I appeciate your time and knowledge.  

Likes

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
New Here ,
Apr 06, 2020 Apr 06, 2020

Copy link to clipboard

Copied

LATEST
Enviado desde mi iPhone

Likes

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