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

Summing Based on String

Community Beginner ,
Mar 16, 2020 Mar 16, 2020

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!

Screen Shot 2020-02-05 at 12.15.59 PM.pngexpand image

1.9K
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 ,
Mar 16, 2020 Mar 16, 2020

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.

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 Beginner ,
Mar 16, 2020 Mar 16, 2020

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!

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 ,
Mar 16, 2020 Mar 16, 2020

Have a close look at the last line of your code...

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 Beginner ,
Mar 16, 2020 Mar 16, 2020

I'm not too sure what I'm supposed to be looking for as an error... I'm sorry! Is it the +=?

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 ,
Mar 16, 2020 Mar 16, 2020

No, that part's fine. The issue is you're adding the Field object itself, not its 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
Community Beginner ,
Mar 16, 2020 Mar 16, 2020

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!

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
LEGEND ,
Mar 16, 2020 Mar 16, 2020

Yes to your last question. You have to use the field 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
Community Expert ,
Mar 17, 2020 Mar 17, 2020

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. 

 

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

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 Beginner ,
Mar 17, 2020 Mar 17, 2020

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... 

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 ,
Mar 17, 2020 Mar 17, 2020

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?

 

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 Beginner ,
Mar 18, 2020 Mar 18, 2020

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... 

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 ,
Mar 18, 2020 Mar 18, 2020

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;

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

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 Beginner ,
Mar 19, 2020 Mar 19, 2020

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

 

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 Beginner ,
Mar 19, 2020 Mar 19, 2020

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
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 Beginner ,
Mar 19, 2020 Mar 19, 2020

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
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 Beginner ,
Mar 19, 2020 Mar 19, 2020
LATEST

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!

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