These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Adding up fields as a loop

Duniagdra
Registered: Sep 24 2008
Posts: 140
Answered

Bellow is an example of the field names to be summed:

Row1.SkilMiscCalc0

Row1 represents the number of rows from 1 to 25.
Calc0 represents the number of columns from 0 to 16.

I was thinking trying something like splitting Row1 from the field name and while it is less than 26 to increment up, then incrementing Calc0 up to 16 as I try to add up all the rows of each column.

The field doing the totaling would be SkilMiscCalc0Total.

In essence I'm trying to figure a way to create a document level function that could be put into SkilMiscCalc0Total and total all fields of say Row1 through 25 .SkilMiscCalc0 and then the same function in SkilMiscCalc1Total would sum all the values of its column.

I was thinking of using do while with .split and .slice or .join, but I'm confusing myself in how to approach this. Can this be done?

Thanks in advance for any help.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

My Product Information:
Acrobat Pro 8.1.2, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes it can be done!! The most important thing of course is the naming so the add function can be generalized to work on all columns. I think you already have this bit down.

For example, using your existing names:
function AddColumns(nColNum){var sum=0;var cFldName = ".SkilMiscCalc" + nColNum;for(var i=1;i<25;i++)sum += this.getField("Row" + i + cFldName ).value; return sum;}

In this function you just pass in the column number, which the function uses to build the name of the field. You could also just pass in the name of the field within the row.

Or you could even use the name of the total field itself. Since this name can be obtained from the event object, the function wouldn't need any inputs.

For example, say the total field is named "SkilMiscCalc0Total". Then this function will add up all the columns named "SkilMiscCalc0"

function AddColumns(){var sum=0;if(/(.*)Total$/.test(event.targetName)){var cFldName = RegExp.$1;for(var i=1;i<25;i++)sum += this.getField("Row" + i + "." + cFldName ).value;}return sum;}

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Duniagdra
Registered: Sep 24 2008
Posts: 140
Thom,

Thanks for your time to help in this.

I've tried both function scripts and, while I'm not getting an error I'm also not getting a total. I've tried both scripts making adjustments accordingly and neither seem to be giving the sum of their respective column. I saved them as a document level script and put the function in the custom calculation script in field properties.

Jim

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to add a "Custom calculation script" to each "SkilMiscCalc#Total" to place the returned value from the function into the field's value property.

For the field "SkilMiscCalc1Total" could be:

event.value = AddColumns(1)
For the field "SkilMiscCalc2Total" could be:

event.value = AddColumns(2)
And so on for all the other rows.

With different coding you could populate all the row fields with one loop in a script:

// loop through all 16 columns and compute the sum for the columnfor (col = 0; col <16 + 1; col++) {this.getField("SkilMiscCalc" + col + "Total").value = AddColumns(col); // process column col} // end of loop

And you would just need to decide where to locate this script.

George Kaiser

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
If any of the fields whose value you want to use in the sum calculation could possible be blank, you should explicitly convert the field value to a number. Otherwise, you will get string concatentation if any of the fields is blank. You can use the + unary operator for this:

sum += +this.getField(...
This assumes that the fields have a numeric Format category. Otherwise you'd have to add code to see if the field value can be converted to a valid number.

George
Duniagdra
Registered: Sep 24 2008
Posts: 140
gkaiseril wrote:
For the field "SkilMiscCalc1Total" could be:event.value = AddColumns(1)
For the field "SkilMiscCalc2Total" could be:

event.value = AddColumns(2)
And so on for all the other rows.
George_Johnson wrote:
You can use the + unary operator for this:sum += +this.getField(...
Gentlemen,
Thanks for your assistance. It is greatly appreciated that you take the time to help.

I have tried both your suggestions and got the same.
Yes, my fields will have blanks at current and all are formatted to numeric.

As I was typing this, I went and tried both your suggestions together and adding a default of 0 to all fields to me summed and now it works. Thanks to all three of you for your help.

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

Duniagdra
Registered: Sep 24 2008
Posts: 140
Can I get help with this issue?

One table that used to work is no longer doing as it should and I'm baffled as to what is wrong. I'm attaching a temporary link to the page in question for a look at what is going on. It's the first table on the top left of the page. The totaling row is not calculating. Scripts are included as instructed from everything given by all three who assisted above. I think the more I look it over the more I see the same thing and am thinking perhaps a separate set of eyes might find something?

[url=http://www.godieselracing.com/calculations.pdf]Calculations.pdf[/url]

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
The document-level function is named "AddColumnsClac" but the calculation scripts are calling "AddColumnsCalc".
Duniagdra
Registered: Sep 24 2008
Posts: 140
George_Johnson wrote:
The document-level function is named "AddColumnsClac" but the calculation scripts are calling "AddColumnsCalc".
I'm not that stupid, am I? I had to re-read your post before seeing what you were pointing out. I blinded myself to the obvious and likely would have never seen it or would have been all too long before I did. Thanks george for pointing out what is an easy fix. Should I leave the linked document, or can I remove that copy from the site I maintain?

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
If you set your JavaScript preferences to show the console on errors, you would have seen it earlier. Regarding the file, I see no reason to keep it around. Also, the field named "SkilMiscStat5" should be named "Row17.SkilMiscStat5".
Duniagdra
Registered: Sep 24 2008
Posts: 140
George_Johnson wrote:
If you set your JavaScript preferences to show the console on errors, you would have seen it earlier. Regarding the file, I see no reason to keep it around. Also, the field named "SkilMiscStat5" should be named "Row17.SkilMiscStat5".
Thanks, I did catch that last night after you pointed out my other oversight. I went and enabled javascript preferences to show console.

Should I enable debugger also?
Options:

Enable javascript debugger after acrobat restarted
Store breakpoints in PDF

When exception is thrown:
Ignore -Trace -Break

Thanks in advance for any help provided,
Jim
The All Time Professional Amateur Hack