====== tablemath Plugin ======
---- plugin ----
description: Adds calculated column to built-in table syntax
author : James GuanFeng Lin
email : guanfenglin@gmail.com
type : syntax
lastupdate : 2009-08-24
compatible : !Igor
depends :
conflicts :
similar : tablecalc
tags : math, tables calculation
downloadurl: https://trello.com/1/cards/5aff17536c98afb44f811799/attachments/5aff177a3ab9aa6497a8d8a3/download/tablemath.zip
bugtracker :
sourcerepo :
donationurl:
screenshot_img:
----
> //There are significant changes to the previous version, please look at the [[#usage]] section for new syntax.//
===== Installation =====
- create a folder named "tablemath" in the plugin folder
- create 2 files named "syntax.php" and "script.js" in the in tablemath folder, copy the file content below into those 2 files
'James GuanFeng Lin',
'email' => 'guanfenglin@gmail.com',
'date' => '2009-08-24',
'name' => 'Table Math Plugin',
'desc' => 'Enables calculated columns in built in table syntax',
'url' => 'https://www.dokuwiki.org/plugin:tablemath',
);
}
function getType() { return 'substition'; }
function getSort() { return 1213; }
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern("~~=[a-zA-Z0-9_]*\([0-9,:]*\)~~", $mode, 'plugin_tablemath');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler) {
global $ID, $ACT, $INFO;
$match = str_replace('~~', '', $match);
$match = str_replace('=', '', $match);
$tmp = explode('(', $match);
if (count($tmp)>1)
{
$tmp[1] = str_replace(')', '', $tmp[1]);
return array('method'=>$tmp[0],'set'=>$tmp[1], 'divid'=>'tm'.rand());
}
return array();
}
function render($mode, &$renderer, $data) {
global $INFO, $ID, $conf;
if($mode == 'xhtml'){
// get discussion meta file name
$renderer->doc .= '';
return true;
}
return false;
}
}
function tablemath(divID,method,rangeStr)
{
var result;
var div = document.getElementById(divID);
var table = div.parentNode.parentNode.parentNode;
var setArray = tmSetToArray(table, rangeStr);
switch(method)
{
case 'sum':
break;
case 'avg':
break;
default:
method = 'unknown';
break;
}
eval('result = tm'+method+'(setArray);');
div.innerHTML = result;
}
function tmSetToArray(table, rangeStr)
{
//range should be like this x,y:x,y
var range = rangeStr.split(':');
var start = range[0].split(',');
var end = range[1].split(',');
var startX = start[0];
var startY = start[1];
var endX = end[0];
var endY = end[1];
var set = new Array();
for(var x = startX; x<=endX; x++)
{
for(var y = startY; y<=endY; y++)
{
set.push(tmParseValue(table.rows[y].cells[x]));
}
}
//alert(set);
return set;
}
function tmParseValue(cell)
{
var value = parseInt(cell.innerHTML);
if (isNaN(value))
{
value = 0;
}
return value;
}
function tmunknown(setArray)
{
return 'Error';
}
function tmsum(setArray)
{
var sum = 0;
for (var i=0; i
===== Usage =====
Put the syntax into the desired column at the last row of the table.
~~=(x,y:x1,y1)~~
^Method name^Description|
|sum|calculate the total value of defined set|
|avg|calculate the average value of defined set|
Range set e.g. ''1,1:2,2''
defines the range in table you want your calculation method should apply to
| 0,0 | 1,0 | 2,0 |
| 0,1 | 1,1 | 2,1 |
| 0,2 | 1,2 | 2,2 |
==== Example ====
^ header1 ^ header2 ^ header3 ^ Average ^
| A | 1 | 2 | ~~=avg(1,1:2,1)~~ |
| B | 2 | 3 | ~~=avg(1,2:2,2)~~ |
| C | 3 | 4 | ~~=avg(1,3:2,3)~~ |
| Total | ~~=sum(1,1:1,3)~~ | ~~=sum(2,1:2,3)~~ | ~~=sum(1,1:2,3)~~ |
===== Limitations =====
* Calculated column referencing to another calculated column will give incorrect values.
===== Feedback =====
Please check if the calculations work with decimal points e.g. ''10.2+1.5''
/alwin kumar Thu Mar 18 2010/