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

Calculations based on .includes

Community Beginner ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

Hi all,

 

Trying to figure out the custom calculation script to be able to add multiple values of different form fields if a form field includes a value above 300. For context, I am trying to sum the number of upper division credits, (courses at 300 level or above,) for a form that I use to meet with students. In the attached file, I would love for "UD Credits" to add the value of "a" if "Fall 1" includes in a string a value above 300, such as MATH 321. I would then need to add the respective values of "aa," "aaa," "aaaa" and so on based on their respective left form fields. 

 

Sorry to ask this question! I'm very, very new at javascript and I've tried to experiment with the script by using boolean logic, .includes, etc. but I'm just not able to make anything work!

 

Screen Shot 2020-02-05 at 12.15.59 PM.png 

TOPICS
Acrobat SDK and JavaScript

Views

305

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

There are several parts to creating such a script.

1) the script needs to draw a relationship between the "Fall" and the "a" fields. This is usually done with field naming, although you could also create an array of fields as well. 

 

2) The script need to be able to identify the number part of the data. Use RegExp pattern matching for this.

Here's an article:https://acrobatusers.com/tutorials/text-matching-regular-expressions/

 

3) The method: Loop over the list of "Fall" names,  if pattern is found, and is > 300, the add associated value;

 

 

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

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

Thank you so much for the quick response! Sorry to ask, but do you have an example of the script you are describing? I'm reading over the RegExp article you've listed and I'm not sure how I might link it to the >300 script to write... Thanks again! I really appreciate 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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

this regular expression will identify 3 numbers by themselves.

 

var rg3Digit = /\b(\d{3})\b/;

 

The \b part means word boundary. So this would not form for something like "Something 345a". For that you would need to modify the RegExp.

Notice that the digits are grouped so they can be extracted for use with the comparison. 

 

You'll need to read up on JavaScript Regular Expressions to use it in a script. 

 

 

 

 

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

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

Thanks again. From what I'm reading about regular expressions, I'm just confused as to how to implement the method you've described in your first reply. I'm pretty sure this is just me not knowing JavaScript, but what am I to do with the var rg3Digit = (/\b(\d{3})\b/); script?

 

My first instinct is to do the following:

var fall1 = "Fall 1"

var rg3Digit = fall1.includes(/\b(\d{3})\b/);

 

which should then output true

 

and then I would also have a script where

if (rg3Digit = true) 

var udCredits = "a"

 

but then I don't necessarily know how to sum this for all of the Fall 1-5 field forms. I'm sorry I'm really inept at scripting!

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

So in the article I've linked above it shows how to use the regular expression "test()" function to find pattern matches. It's important to read and test out the material. You should be doing this from the Acobat JavaScript Console Widow. There's a tutorial here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

What the article doesn't show is how to group pattern elements can be extracted. Like this:

 

if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300))

    nSum += ... value from associated field ...;

 

 

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

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

Hi Thom,

 

Thank you so much for keeping with me! So what I have so far then is:

 

var rg3Digit = /\b(\d{3})\b/;

var cFallValue = this.getField ("Fall 1");

if(rg3Digit.test(cFallValue) && (Number(RegExp.$1) > 300));

nSum += this.getField ("a");

 

which then says via JavaScript Console Window that nSum is undefined which I'm not too sure how to fix. I figure I'll learn how to do it with just one example as I try to figure out how to do it en masse unless it's nominally easier to do it in bulk... When I run app.alert for the output, it works though!

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

The nSum variable need to be defined and initialized before it can be used. 

 

var nSum = 0;

 

Now all you need to do is put this in a loop that tests and adds all the rows.

 

 

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

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 ,
Feb 05, 2020 Feb 05, 2020

Copy link to clipboard

Copied

LATEST

I've tried adding the var nSum = 0; before but no matter where I place it, it pops up as undefined?

 

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");

 

ReferenceError: nSum is not defined 1:Console:Exec undefined

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