#30930 - 04/21/10 02:20 AM
Re: adding a subtotal from a range of data
|
OL Newbie
Registered: 03/12/10
Posts: 20
Loc: The Netherlands
|
|
Top
|
|
|
|
#30931 - 04/21/10 10:44 AM
Re: adding a subtotal from a range of data
|
OL Expert
Registered: 10/14/05
Posts: 4956
Loc: Objectif Lune Montreal
|
Marcel, Making the subtotal for one line is a bit tricky, because there is nothing that is specifically made to mark the start of the various amounts. We cannot use the spacing between the various entries, because it's not constant(the third block, for example, has more spaces due to a longer name than the first two). We could sort-of simulate it using the dots in the currency values to know where to start, then make a presstalk function that returns the amount. Here's an example: function @TotalPerLines(&line:integer):string
define(&res, string, '')
define(&curline, string, '')
define(&i, integer, 0)
define(&totalamounts, currency, 0.0)
define(&s, string, '')
&curline := @(&line, 1, 1000)
if(pos('.', &curline) > 0)
&i := pos('.', &curline)
% Read characters backward until we find a space.
repeat
&i := &i - 1
until(mid(&curline, &i, 1) = ' ')
% Read the amounts.
&i := &i + 1
repeat
if((mid(&curline, &i, 1) = ' ') or (&i = length(&curline)))
if(&i = length(&curline))
&s := &s + mid(&curline, &i, 1)
endif()
&totalamounts := &totalamounts + strtocur(&s)
&s := ''
else()
&s := &s + mid(&curline, &i, 1)
endif()
&i := &i + 1
until(&i > length(&curline))
% Prepare the total to be displayed.
&res := curtostr(&totalamounts)
else()
% In cases where there is no dots(therefore no amounts), we return a blank string.
&res := ''
endif()
% Return the final result.
&Result := &res
endfunction() This function will add all three(or more) amount values that are at the end of the line you specify. So in a custom data selection, you could type: =@TotalPerLines(1) That would add up the values on the first line, and would display, in this case, 266.68. Note that if you use this function on a line with no currency values, it will return a blank string. Hope that helps! Regards, Rapha
|
Top
|
|
|
|
#30932 - 04/22/10 03:15 AM
Re: adding a subtotal from a range of data
|
OL Newbie
Registered: 03/12/10
Posts: 20
Loc: The Netherlands
|
|
Top
|
|
|
|
#30933 - 04/22/10 09:27 AM
Re: adding a subtotal from a range of data
|
OL Expert
Registered: 10/14/05
Posts: 4956
Loc: Objectif Lune Montreal
|
If the last value on the line is the total, you can get it by making a simple change to the above function. You can change: &totalamounts := &totalamounts + strtocur(&s) Into: &totalamounts := strtocur(&s) And you could rename the function into something like GetTotal instead of TotalPerLines, as it would be closer to what it does. After that, you can create a presstalk object, and loop through the lines of data to add up the totals and display it. Here's an example(assuming you renamed the function to GetTotal): define(&t, currency, 0.0)
define(&i, integer, 0)
for(&i, 1, 1, ¤t.lpp)
if(pos('.', @(&i, 1, 1000)) > 0)
&t := &t + strtocur(@GetTotal(&i))
endif()
endfor()
show(curtostr(&t)) This will add up the totals of each lines that have one, looping through every lines on your data page, and display it at the end. Of course, you could instead create a global variable, and put the value of &t in that variable, so it becomes usable at other places. Hope that helps! Regards, Rapha
|
Top
|
|
|
|
#30934 - 04/23/10 09:41 AM
Re: adding a subtotal from a range of data
|
OL Newbie
Registered: 03/12/10
Posts: 20
Loc: The Netherlands
|
Wow, I thought it would be simple... Ok I'll test this next week. Thanks
Regards Marcel Vlaardingerbroek
|
Top
|
|
|
|
|
|