Count Specific Text

New Here ,
Jun 03, 2019

Copy link to clipboard

Copied

I'm new to to this and looking for help creating Java script to count multiple letters.  Example: A**A, or AA would be added up and total displayed in bottom cell under category "A**A". I attached a photo for reference. Thanks for the help.

https://ibb.co/6WdxMcf

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

OK. Let's say they are called Code1 to Code32 and you want to count all the fields whose values start with "A" and end with "B" and show the result in a field called Total2. You can use this code as the custom calculation script of Total2:

var total = 0;

for (var i=1; i<=32; i++) {

    var f = this.getField("Code"+i).valueAsString;

    var v = f.valueAsString;

    if (v.charAt(0)=="A" && v.charAt(v.length-1)=="B") total++;

}

event.value = total;

You can easily adjust this code for the other total fields by changing the strings in line #5 to "A"/"A" and "B"/"A" for Total1 and Total3, respectively.

TOPICS
Acrobat SDK and JavaScript

Views

125

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

Count Specific Text

New Here ,
Jun 03, 2019

Copy link to clipboard

Copied

I'm new to to this and looking for help creating Java script to count multiple letters.  Example: A**A, or AA would be added up and total displayed in bottom cell under category "A**A". I attached a photo for reference. Thanks for the help.

https://ibb.co/6WdxMcf

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

OK. Let's say they are called Code1 to Code32 and you want to count all the fields whose values start with "A" and end with "B" and show the result in a field called Total2. You can use this code as the custom calculation script of Total2:

var total = 0;

for (var i=1; i<=32; i++) {

    var f = this.getField("Code"+i).valueAsString;

    var v = f.valueAsString;

    if (v.charAt(0)=="A" && v.charAt(v.length-1)=="B") total++;

}

event.value = total;

You can easily adjust this code for the other total fields by changing the strings in line #5 to "A"/"A" and "B"/"A" for Total1 and Total3, respectively.

TOPICS
Acrobat SDK and JavaScript

Views

126

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
Jun 03, 2019 0
Most Valuable Participant ,
Jun 04, 2019

Copy link to clipboard

Copied

You need to clearly define the rules of how this is supposed to work. What does "**" mean? What does "-" mean?

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
Reply
Loading...
Jun 04, 2019 0
New Here ,
Jun 04, 2019

Copy link to clipboard

Copied

Sorry A-A, A**A is user input. House A**A, but user can input AA or A-A and still have it counted.

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
Reply
Loading...
Jun 04, 2019 0
Most Valuable Participant ,
Jun 04, 2019

Copy link to clipboard

Copied

I still don't understand. Are those literal strings? If so, why in your example is the result for "A**A" 3, even though it only appears only once in the table?

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
Reply
Loading...
Jun 04, 2019 0
Most Valuable Participant ,
Jun 04, 2019

Copy link to clipboard

Copied

Ah, I think I see what you mean now... So "A**A" means it start with "A" and ends with "A", basically?

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
Reply
Loading...
Jun 04, 2019 0
Most Valuable Participant ,
Jun 04, 2019

Copy link to clipboard

Copied

What are the names of the fields in which the values are entered?

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
Reply
Loading...
Jun 04, 2019 0
New Here ,
Jun 04, 2019

Copy link to clipboard

Copied

Nothing to do with programming, it's just the the way it's listed on the form. Unit A, but they have it listed as A**A. So I wanted the option for users to input it either that way, AA, or A-A for unit "A**A". I didn't create the original form. I agree the ** don't make sense.

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
Reply
Loading...
Jun 04, 2019 0
New Here ,
Jun 04, 2019

Copy link to clipboard

Copied

I haven't created any fields/ names yet, I was going to based on whatever example code giving.

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
Reply
Loading...
Jun 04, 2019 0
Most Valuable Participant ,
Jun 04, 2019

Copy link to clipboard

Copied

OK. Let's say they are called Code1 to Code32 and you want to count all the fields whose values start with "A" and end with "B" and show the result in a field called Total2. You can use this code as the custom calculation script of Total2:

var total = 0;

for (var i=1; i<=32; i++) {

    var f = this.getField("Code"+i).valueAsString;

    var v = f.valueAsString;

    if (v.charAt(0)=="A" && v.charAt(v.length-1)=="B") total++;

}

event.value = total;

You can easily adjust this code for the other total fields by changing the strings in line #5 to "A"/"A" and "B"/"A" for Total1 and Total3, respectively.

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
Reply
Loading...
Jun 04, 2019 0
New Here ,
Jun 04, 2019

Copy link to clipboard

Copied

Awesome, thanks!

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
Reply
Loading...
Jun 04, 2019 0