Summing Based on String
Copy link to clipboard
Copied
Hello,
I'm revisiting another issue that I've had before. For the attached screenshot I want to create a Javascript where I can do the following:
If "Fall 1" contains a string that has any number greater than 300, such as "SOC 301," then add value of "a" to "UD Credits." I've tried to use the following script:
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFallValue = this.getField ("Fall 1");
if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300));
nSum += this.getField ("a");
but I get the following error:
ReferenceError: nSum is not defined 1:Console:Exec undefined
Thanks for any help in advance! I'm pretty script illiterate and I hope to learn more about how to do this to help in my work!
Copy link to clipboard
Copied
You're not executing the full code. In order to do that you have to select all of it with the mouse of keyboard, and then run it using Ctrl+Enter. There are other issues with it, but first run it correctly and then you'll be able to debug it further.
Copy link to clipboard
Copied
Thank you for your response!
For "Fall 1" = HIST 301
and for "a" = 4,
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFallValue = this.getField ("Fall 1");
if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300));
nSum += this.getField ("a");
result:
0[object Field]
Could you please help me interpret this? The field "UD Credits" doesn't have even the value 0 popping up at the moment... Again, thank you so much!
Copy link to clipboard
Copied
Have a close look at the last line of your code...
Copy link to clipboard
Copied
I'm not too sure what I'm supposed to be looking for as an error... I'm sorry! Is it the +=?
Copy link to clipboard
Copied
No, that part's fine. The issue is you're adding the Field object itself, not its value...
Copy link to clipboard
Copied
So this.getField is the incorrect function to use? Which function might be better? Or do I just put this.getField("a").value?
Thanks again!
Copy link to clipboard
Copied
Yes to your last question. You have to use the field value.
Copy link to clipboard
Copied
You are exactly correct, the code needs to reference the Field Value, i.e. the "value" property.
this.getField(<name>).value
This needs to be done in the two places in the code where the field object is used.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you all! So I have right now:
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFallValue = this.getField("Fall 1").value;
if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300));
nSum += this.getField("a").value;
But the "UD Credits" is still not showing a value with "HIST 301" for "Fall 1" and "4" for "a".
I'm also not too certain how I can then apply this code to the rest of the respective "Fall 2" and "aa" pair and so on and so forth...
Copy link to clipboard
Copied
Use this code:
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFallValue = this.getField("Fall 1").value;
if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300))
nSum += Number(this.getField("a").valueAsString);
Do you want to add all of these values together, or use each one separately?
What do you want to do with this value, eventually? Apply it to a field?
Copy link to clipboard
Copied
I put in the code and I still don't get a return value in the field I put the code into. The hope is to just sum these values together and just have it show up in the "UD Credits" field. I tried putting the code into Run Custom Validation Script as well as Custom Calculation Script but not much happens...
Copy link to clipboard
Copied
This is only going to work as a calculation script on the "UD Credits" field, and you are missing the final piece.
Add this line to the end of the script
event.value = nSum;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you so much!!! It worked!! I guess my only question is now, how would I apply this code to "Fall 2" and "aa" and so on and so forth?
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFallValue = this.getField("Fall 1").value;
if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("a").valueAsString)
event.value = nSum
Copy link to clipboard
Copied
It works!! Thank you so much!!! I guess my last question would be on how to apply this code to the rest of "Fall 2" and "aa" and so on and so forth. As of right now, I have the script listed below, but I was curious if there was a better way to code this or if there's a limit to how much code a pdf could calculate? Thank you so much for your help.
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFall1Value = this.getField("Fall 1").value;
var cFall2Value = this.getField("Fall 2").value;
var cFall3Value = this.getField("Fall 3").value;
var cFall4Value = this.getField("Fall 4").value;
var cFall5Value = this.getField("Fall 5").value;
if(rg3Digit.test(cFall1Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("a").valueAsString)
if(rg3Digit.test(cFall2Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aa").valueAsString)
if(rg3Digit.test(cFall3Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaa").valueAsString)
if(rg3Digit.test(cFall4Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaaa").valueAsString)
if(rg3Digit.test(cFall5Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaaaa").valueAsString)
event.value = nSum
Copy link to clipboard
Copied
It works!! Thank you so much!!! I guess my last question would be on how to apply this code to the rest of "Fall 2" and "aa" and so on and so forth. As of right now, I have the script listed below, but I was curious if there was a better way to code this or if there's a limit to how much code a pdf could calculate? Thank you so much for your help.
var nSum = 0;
var rg3Digit = /\b(\d{3})\b/;
var cFall1Value = this.getField("Fall 1").value;
var cFall2Value = this.getField("Fall 2").value;
var cFall3Value = this.getField("Fall 3").value;
var cFall4Value = this.getField("Fall 4").value;
var cFall5Value = this.getField("Fall 5").value;
if(rg3Digit.test(cFall1Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("a").valueAsString)
if(rg3Digit.test(cFall2Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aa").valueAsString)
if(rg3Digit.test(cFall3Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaa").valueAsString)
if(rg3Digit.test(cFall4Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaaa").valueAsString)
if(rg3Digit.test(cFall5Value) && (Number(RegExp.$1) > 299))
nSum += Number(this.getField("aaaaa").valueAsString)
event.value = nSum
Copy link to clipboard
Copied
It works!! Thank you so much!!! I guess my last question would be on how to apply this code to the rest of "Fall 2" and "aa" and so on and so forth. I was curious if there was a better way to code this than to repeat what I already have or if there's a limit to how much code a pdf could calculate? Thank you so much for your help!

