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

count number of specific answers

Explorer ,
Aug 24, 2017 Aug 24, 2017

Copy link to clipboard

Copied

Hi,

I've created a PDF form with yes/no questions. In the last field I want to see the summary of the amount of 'ja' (= yes) answers.

The field ID's range from Tekstveld 1 (= Text Field) to Tekstveld 37, with no. 38 as the 'total' field.

I've found a Java code and tried to edit it to my needs, but I'm not getting it...

// Initialize counter variable

var sum = 0;

// Loop through the fields

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

// Add one if Teksveld is set to ja

    if (getField("Tekstveld" + i).value !== "ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Can someone help me to get it right?

Thanks!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

869
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 , Aug 25, 2017 Aug 25, 2017

As I suggested, there's a space between the text and the number in all of the field names... Use this code:

// Initialize counter variable

var sum = 0;

// Loop through the fields

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

// Add one if Tekstveld is set to ja

    if (getField("Tekstveld " + i).valueAsString.toLowerCase()=="ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Also, for some reason you've set the Format of the total field as "Zip code". Change it to None or to Number.

I would

...

Votes

Translate
Community Expert ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

- You need to change i<37 to i<=37

- You need to change value !== "ja" to value=="ja"

The rest seems fine.

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Hi try67,

Thanks for responding so quickly

Still having issues though, I still must be doing something wrong. This is the new code:

// Initialize counter variable
var sum = 0;
// Loop through the fields
for (var i = 1; i <= 37; i += 1) {
// Add one if Tekstveld is set to ja
    if (getField("Tekstveld" + i).value== "ja") {sum += 1;}
}
// Set this field's value to the sum
event.value = sum;

If this code is correct, maybe I'm adding the code to wrong place? This is where I added the code: Properties -> Calculate -> Custom calculation script

Votes

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 ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

The code seems correct and the location is also correct.

Change the value of one of the fields in the file and then press Ctrl+J and check if there are any error messages in the JS Console window that opens.

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

I changed 37 to 64. So new code is:

// Initialize counter variable

var sum = 0;

// Loop through the fields

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

// Add one if Tekstveld is set to ja

    if (getField("Tekstveld" + i).value== "ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

This is the error log:

Acrobat EScript Built-in Functions Version 10.0
Acrobat SOAP 10.0

SyntaxError: syntax error
4:
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
SyntaxError: syntax error
4:
SyntaxError: syntax error
4:
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate

Votes

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 ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

That means that some of the field names you're trying to access don't exist. Use this code to get a better idea of which ones they are:

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

    var fname = "Tekstveld" + i;

    var f = this.getField(fname);

    if (f==null) {console.println(fname + " doesn't exist.");}

    else if (f.value== "ja") {sum += 1;}

}

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

wow, all text fields don't seem to exist...?

Isn't the code referring to the names of the fields?

Do Ihave to change something in the properties?

Acrobat EScript Built-in Functions Version 10.0
Acrobat SOAP 10.0

SyntaxError: syntax error
4:
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
SyntaxError: syntax error
4:
SyntaxError: syntax error
4:
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:Calculate
TypeError: getField("Tekstveld" + i) is null
6:Field:CalculateTekstveld1 doesn't exist.
Tekstveld2 doesn't exist.
Tekstveld3 doesn't exist.
Tekstveld4 doesn't exist.
Tekstveld5 doesn't exist.
Tekstveld6 doesn't exist.
Tekstveld7 doesn't exist.
Tekstveld8 doesn't exist.
Tekstveld9 doesn't exist.
Tekstveld10 doesn't exist.
Tekstveld11 doesn't exist.
Tekstveld12 doesn't exist.
Tekstveld13 doesn't exist.
Tekstveld14 doesn't exist.
Tekstveld15 doesn't exist.
Tekstveld16 doesn't exist.
Tekstveld17 doesn't exist.
Tekstveld18 doesn't exist.
Tekstveld19 doesn't exist.
Tekstveld20 doesn't exist.
Tekstveld21 doesn't exist.
Tekstveld22 doesn't exist.
Tekstveld23 doesn't exist.
Tekstveld24 doesn't exist.
Tekstveld25 doesn't exist.
Tekstveld26 doesn't exist.
Tekstveld27 doesn't exist.
Tekstveld28 doesn't exist.
Tekstveld29 doesn't exist.
Tekstveld30 doesn't exist.
Tekstveld31 doesn't exist.
Tekstveld32 doesn't exist.
Tekstveld33 doesn't exist.
Tekstveld34 doesn't exist.
Tekstveld35 doesn't exist.
Tekstveld36 doesn't exist.
Tekstveld37 doesn't exist.

Votes

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 ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

What are the exact names of the fields?

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

The names are exactly as written in the error log....

So: Tekstveld1, Tekstveld2 etc.

Votes

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 ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

Are you sure there isn't a space or a period between the text and the

number? Can you share the file with us (via Dropbox, Google Drive, Adobe

Cloud, etc.)?

On Fri, Aug 25, 2017 at 1:00 PM, esterk42897401 <forums_noreply@adobe.com>

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

I've tried different ways, none of them seem to be working.

this is the file: Dropbox - Website-effectiviteitstest.pdf

Votes

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 ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

As I suggested, there's a space between the text and the number in all of the field names... Use this code:

// Initialize counter variable

var sum = 0;

// Loop through the fields

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

// Add one if Tekstveld is set to ja

    if (getField("Tekstveld " + i).valueAsString.toLowerCase()=="ja") {sum += 1;}

}

// Set this field's value to the sum

event.value = sum;

Also, for some reason you've set the Format of the total field as "Zip code". Change it to None or to Number.

I would also recommend that you use a check-box or at least a drop-down instead of open text fields.

If someone enters "JA", for example, or "Ja", or "X", or anything else instead of "ja", the script above will not count it, which is not good.

Edit: added the code, and fixed it so that it will recognize all versions of "ja", regarding of upper/lower-case letters.

Votes

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
Explorer ,
Aug 25, 2017 Aug 25, 2017

Copy link to clipboard

Copied

LATEST

Yes!! It works

That zipcode setting must have come up when I was testing locations... silly of me to have left it there.

Thanks a lot, I really appreciate your help!!

Votes

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