[pmwiki-users] FW: Spreadsheet style calculations
J. Meijer
commentgg at hotmail.com
Sun Dec 31 07:41:18 CST 2006
> > I'll take a look at the snipit you provided. Do you have the original > code handy somewhere, and some time to dig it out for me?> > ~ ~ Dave> > J. Meijer wrote:> > > > > > Dave,> > > > I have a script that sums row and sums columns putting in a markup in > > the summing row/column and quite a bit smarter then the code below. > > Haven't looked at it in a while.> > Below you'll find the relevant extract of the script that inserts itself between table markup and the pmwiki table function. It
internally reconstructs the table from data within <td> tags. So
it's able to do calculations on it. Hunt for embedded directives like
(:rowtotal:) and (:rowcount:). The difference between the 2 is that
rowtotal rounds to 2 digits (money format), while rowcount expects to
work with integers. The recipy could be easily generalized, it was just a quick hack. The number of digits should at least be a parameter. We could do much better! /jm
# redine ||cell||, ||!header cell||, ||!caption!|| markupMarkup('^||||', 'block', '/^\\|\\|.*\\|\\|.*$/e', "TotalFormatTableRow(PSS('$0'))"); # reset totals for use between tablesMarkup('resettotals','^||||', '/[(]:resettotals:[)]/e', 'PZZ($GLOBALS["ColumnTotals"]=array())');function TotalFormatTableRow($x) { global $ColumnTotals; $x=FormatTableRow($x); $y=strip_tags($x,'<td>'); if(!preg_match_all('/>(.*?)<\/td>(?:<td|$)/mi',$y,$m)) return $x; $tot=0; foreach($m[1] as $i=>$n) { if(substr($n,-3,1)==',') # undo locale for money format $n=str_replace(array('.',',','%+%'),array('%+%','.',','),$n); if(is_numeric($n)) { $tot+=$n; $ColumnTotals[$i]+=$n; } } foreach($m[1] as $i=>$c) { if(false!==strpos($c,'(:coltotal:)')) # presume money format $x=substr_replace($x,sprintf("%01.2f", $ColumnTotals[$i]),strpos($x,'(:coltotal:)'),12); elseif(false!==strpos($c,'(:colcount:)')) # presume integers $x=substr_replace($x,$ColumnTotals[$i],strpos($x,'(:colcount:)'),12); else continue; $tot+=$ColumnTotals[$i]; } foreach($m[1] as $i=>$c) if(false!=strpos($c,'(:rowtotal:)')) $ColumnTotals[$i]+=$tot; $x=str_replace('(:rowtotal:)',sprintf("%01.2f", $tot),$x); # intended for money formats $x=str_replace('(:rowcount:)',$tot,$x); # intended for integers $c=1; $x=preg_replace('/[(]:coltotal:[)]/mie','sprintf("%01.2f", $ColumnTotals[$c++])' ,$x); return $x; }
_________________________________________________________________
Get into the holiday spirit, chat with Santa on Messenger.
http://imagine-windowslive.com/minisites/santabot/default.aspx?locale=en-us
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/pmwiki-users/attachments/20061231/39844c3f/attachment.html
More information about the pmwiki-users
mailing list