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

LC dynamic form: Calculating Average on repeating subform issue

sergey2511
Registered: Aug 1 2011
Posts: 9
Answered

Dear All,
 
could you please help to solve following problem:
I have a LiveCycle dynamic PDF form, which has a repeatable page, let's call it "dynamic", and on that page, there's "numeric1" field for entering the number. And also there's another "numeric2" field, which calculates the Average of "numeric1" on all pages - Avg(dynamic[*].numeric1). Pages are added by instanceManager.addInstance(1) command.
 
So here is my problem: when I make several pages, and fill out the numeric1 field, I get equal average numeric2 value on ALL pages. And I need to have those averages, which were actual for page1, page2, page(n).
 
My idea is that I need to make average formula working in cycle where i = first page number and n is current page number. The cycle should look like that:
 
for i=1 to n
Avg(dynamic[*].numeric1)
end
 
I know that code written above is very far away from syntax rules, so that's where I need the expert help. Could anyone please give me a right code (preffered in FormCalc) to achieve my goal.
 
I would be more than happy to hear other ideas, if you consider my approach as wrong.
 
Thanks for yor help!

My Product Information:
LiveCycle Designer, Windows
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Accepted Answer
I'm not sure if I understood you request, so may be this is not what you're looking for.

  1. var nPage = dynamic.index
  2. var nAvg = 0
  3.  
  4. for i = 0 upto nPage do
  5. nAvg = nAvg + dynamic[i].NumericField1 / (nPage + 1)
  6. endfor

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

sergey2511
Registered: Aug 1 2011
Posts: 9
thank you, radzmar, your posts are alway helpful. I have no chance to test it yet, but I'll surely get back here for my progress update.
sergey2511
Registered: Aug 1 2011
Posts: 9
Dear Radzmar,

I checked the solution you've offered and it solves the problem as I described it. However, there's some difference between average calculation methods. My point is that Avg function calculates only the average of the not-null-fields. So it takes only the not null values, but also the total number includes only not-empty fields. And your formula calculates an overall average value. Could you give me any advice please how can I keep the mentioned functionality of the Avg function?

Thanks a lot for your help!

Kind regards.
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
You can exclude fields with a 0 value with an if expression.

  1. var nPage = dynamic.index
  2. var nAvg = 0
  3.  
  4. for i = 0 upto nPage do
  5. if (dynamic[i].NumericField1 ne 0) then
  6. nAvg = nAvg + dynamic[i].NumericField1 / (nPage + 1)
  7. endif
  8. endfor

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs