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

I can't get my calculations to work depending on whether the check box is check or not.

New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

If someone checks the check box, I want the amount of kms collected and totaled at the bottom. If the check mark is not checked, do not grab the number of kms in that line. I just learned JS in the last week in order to make this calculation work on a fillable PDF...so excuse my lack of knowledge and/or background!! I have got it to add up the amounts before, but it didn't work if I unchecked a box and didn't work when I started to add more lines. It seemed to not like when the check array returns a NULL. Currently, it won't add anything and it's returning a zero. Can anyone see where I have gone wrong in the code below? Thanks!

var check = ["Check.1", "Check.2", "Check.3"];

var km = ["KMsRow1", "KMsRow2", "KMsRow3"];

function myKM()

{var KM = 0;

var i = 0;

var c = 0;

for (i = 0; i < 20; i++)

  {if(this.getField(check) == 1)

    {KM.value += this.getField(km);

     event.value = parseFloat(KM);}

   else

    {event.value = parseFloat(KM);}}}

myKM(km, check);

TOPICS
Acrobat SDK and JavaScript , Windows

Views

251

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
community guidelines

correct answers 1 Correct answer

Engaged , May 10, 2018 May 10, 2018

First of all, congrats for naming your field the way you did.  It will render the code much simpler.  Instead of hardcoding your arrays the way you did, save time by populating your arrays with JS.  The push() method of the array object will append an element at the end of you array by updating the original array:

var aCheck = []

var aKm = []

for (var x=1;x<=20;x++){

    aCheck.push("Check."+x)

    aKm.push("KMsRow"+x)

}

notice that I used "a" as a prefix for arrays.  It is easier to remember that this

...

Likes

Translate

Translate
LEGEND ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Have you opened the Acrobat JavaScript debugging console to see if there have been any errors encountered in running your script?

I do not see a "return" statement within your function. This might not be necessary, but it would at least add a terminus for the function call. I also do not see any parameters in the function's definition, so this means your function uses the values for "km", and "check" from an object with a scope beyond the function. Your function appears to assume that there are at least 20 elements in the arrays for "km" and "check" but you only define 3. This should at least create an error for the getField methods returning a null value.

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
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

I'm not getting any errors with the above code. there will be 20 values in both arrays, I'm just trying to get it to work before I type them all in. I will go in and define the rest to see if that helps. 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
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

I have done that now...same outcome.

var check = ["Check.1", "Check.2", "Check.3", "Check.4", "Check.5", "Check.6", "Check.7", "Check.8", "Check.9", "Check.10", "Check.11", "Check.12", "Check.13", "Check.14", "Check.15", "Check.16", "Check.17", "Check.18", "Check.19", "Check.20", ];

var km = ["KMsRow1", "KMsRow2", "KMsRow3", "KMsRow4", "KMsRow5", "KMsRow6", "KMsRow7", "KMsRow8", "KMsRow9", "KMsRow10", "KMsRow11", "KMsRow12", "KMsRow13", "KMsRow14", "KMsRow15", "KMsRow16", "KMsRow17", "KMsRow18", "KMsRow19", "KMsRow20"];

function myKM()

{var KM = 0;

var i = 0;

var c = 0;

for (i = 0; i < 20; i++)

  {if(this.getField(check) == 1)

    {KM.value += this.getField(km);

     event.value = parseFloat(KM);}

   else

    {event.value = parseFloat(KM);}}}

myKM(km, check);

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
community guidelines
Community Expert ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

The problem is with this line:

KM.value += this.getField(km);

MK is a number. It doesn't have a value property, and the Field object does. Change it to:

KM += this.getField(km).value;

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
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Just tried it, didn't work.

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
community guidelines
Community Expert ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

If you want help you need to provide details.

What happened, exactly? Did it produce the wrong results? No results at all?

Were there any error messages in the JS Console? etc.

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
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Nothing happens, the cell still shows zero at the bottom where the total KMs of the checked lines should be. No error, no obvious calculation being run.

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
community guidelines
Community Expert ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

You'll need to share the file (via Dropbox, Google Drive, etc.) for further help.

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
community guidelines
Community Expert ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

When you test a check box you must test the value of the check box.

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
community guidelines
Engaged ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

First of all, congrats for naming your field the way you did.  It will render the code much simpler.  Instead of hardcoding your arrays the way you did, save time by populating your arrays with JS.  The push() method of the array object will append an element at the end of you array by updating the original array:

var aCheck = []

var aKm = []

for (var x=1;x<=20;x++){

    aCheck.push("Check."+x)

    aKm.push("KMsRow"+x)

}

notice that I used "a" as a prefix for arrays.  It is easier to remember that this variable contains an array.  Use prefixes like f for fields, v for values, n for numbers and s for strings.  Trust me, as a begginer, this will help you identify bugs much quicker.

Unless you absolutly need these arrays somewhere else, you do not have to declare them since you created a loop afterwards that loops 20 times.  So either you create arrays and loop throught THAT, OR, you loop 20 times and do want you need in the loop.

to loop inside ALL the elements of an array, use the "in" command like so:

for (x in aCheck){

   console.println(aCheck)  //will print all elements of the array to the console

}

to evaluate if a checkbox is checked, do not evaluate it's value as you did, use the isBoxChecked() method instead.  it returns a boolean.

Do you need this to be a function?  You created a function that as no argument ***function myKM("Nothing here")*** yet you are trying to pass two arguments to it ***myKM(km, check)***.  That will not work.

So unless you need the arrays, I would not use them and go strait with the loop.  Copy this in the custom calculate tab of you "Total" field

var sum = 0                                                                            //Will store the sum

for (var i = 0; i < 20; i++){

var cCheck = this.getField("Check."+x).isBoxChecked(0)     //stores true or false

var vKm = this.getField("KMsRow"+x).value                         //stores the value of the current row

if (cCheck) sum += vKm                                                        //If true (checked), adds value to the sum

}

event.value = sum                                                                  //assign a value equal to the sum to current field

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
community guidelines
New Here ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

Thank you for all of your suggestions, MatLac! Very helpful!

I tried your code and am now getting this error...

TypeError: this.getField("Check." + i) is null

5:Field:Calculate

I realized that I had set the output of the check mark to 1, so I removed that since your code doesn't require it. But it had the same outcome as above.

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
community guidelines
Community Expert ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

What are the names of the check boxes?

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
community guidelines
Engaged ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

This means it is trying to find a field that doesn't exist (either because it is out of range 1-20, or you spelled it wrong).

I see you separated iterations of Check with a dot "." but you did not do the same thing with KMsRow

Is that right?

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
community guidelines
New Here ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

Of course! It's Check.1 so I needed to make i=1 !! Perfect, it is working! thank you, thank you, thank you!!

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
community guidelines
Engaged ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

See, even I was wrong because declared a variable "i" for the loop but referenced it as "x" inside the loop, I started my iteration at 0 (like I usally do) instead of 1, and, I used i < 20 for the limit, which would have not taken into consideration the 20th field, lol.

that's 3 bugs in such a short script, lol

so for disambiguation:

var sum = 0                                                                            //Will store the sum

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

var cCheck = this.getField("Check."+i).isBoxChecked(0)     //stores true or false

var vKm = this.getField("KMsRow"+i).value                         //stores the value of the current row

if (cCheck) sum += vKm                                                        //If true (checked), adds value to the sum

}

event.value = sum

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
community guidelines
New Here ,
May 10, 2018 May 10, 2018

Copy link to clipboard

Copied

LATEST

LOL

VERY much appreciated!!

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
community guidelines