This plugin adds spreadsheet capabilities to TWiki topics. Formulae like %CALC{"$INT(7/3)"}% are evaluated at page view time. They can be placed in table cells and outside of tables. In other words, this plugin provides general formula evaluation capability, not just classic spreadsheet functions.
The formula next to "Total" is %CALC{"$SUM( $ABOVE() )"}%. (you see the formula instead of the sum in case the plugin is not installed or not enabled.)
The action of this plugin is triggered by the %CALC{"..."}% variable, which gets rendered according to the built-in function(s) found between the quotes.
Built-in function are of format $FUNCNAME(parameter)
Changed:
< <
Functions may be nested, e.g. %CALC{"$SUM( R2:C$COLUMN(0)..R$ROW(-1):C$COLUMN(0) )"}%
> >
Functions may be nested, such as %CALC{"$SUM( R2:C$COLUMN(0)..R$ROW(-1):C$COLUMN(0) )"}%
Functions are evaluated from left to right, and from inside to outside if nested
The function parameter can be text; a mathematical formula; a cell address; or a range of cell addresses
Changed:
< <
Multiple parameters form a list; they are separated by a comma, followed by optional space, e.g. %CALC{"$SUM( 3, 5, 7 )"}%
> >
Multiple parameters form a list; they are separated by a comma, followed by optional space, such as %CALC{"$SUM( 3, 5, 7 )"}%
A table cell can be addressed as R1:C1. Table address matrix:
R1:C1
R1:C2
R1:C3
R1:C4
R2:C1
R2:C2
R2:C3
R2:C4
A table cell range is defined by two cell addresses separated by "..", e.g. "row 1 through 20, column 3" is: R1:C3..R20:C3
Changed:
< <
Lists can refer to values and/or table cell ranges, e.g. %CALC{"$SUM( 3, 5, $T(R1:C7), R1:C11..R1:C15 )"}%
> >
Lists can refer to values and/or table cell ranges, such as %CALC{"$SUM( 3, 5, $T(R1:C7), R1:C11..R1:C15 )"}%
Formulae can only reference cells in the current or preceeding row of the current table; they may not reference cells below the current table row
Formulae can also be placed outside of tables; they can reference cells in the preceeding table
COUNTITEMS( list ) -- count individual items in a list
Syntax: $COUNTITEMS( list )
Example: %CALC{"$COUNTITEMS($ABOVE())"}% returns Closed: 1, Open: 2 assuming one cell above the current cell contains Closed and two cells contain Open
EVAL( formula ) -- evaluate a simple mathematical formula
Addition, substraction, multiplication, division and modulus of numbers are supported. Any nesting is permitted
Numbers may be decimal integers (1234), binary integers (0b1110011), octal integers (01234), hexadecimal integers (0x1234) or of exponential notation (12.34e-56)
Execute a spreadsheet formula, typically retrieved from a variable. This can be used to store a formula in a variable once and execute it many times using different parameters.
Syntax: $EXEC( formula )
Example: %CALC{"$SET(msg, $NOEXEC(Hi $GET(name)))"}% sets the msg variable with raw formula Hi $GET(name)
Example: %CALC{"$SET(name, Tom) $EXEC($GET(msg))"}% executes content of msg variable and returns Hi Tom
Example: %CALC{"$SET(name, Jerry) $EXEC($GET(msg))"}% returns Hi Jerry
FIND( string, text, start ) -- find one string within another string
Finds one text string, within another text, and returns the number of the starting position of string, from the first character of text. This search is case sensitive and is not a regular expression search; use $SEARCH() for regular expression searching. Starting position is 1; a 0 is returned if nothing is matched.
FORMATTIME( serial, text ) -- convert a serialized date into a date string
The following variables in text are expanded: $second (seconds, 00..59); $minute (minutes, 00..59); $hour (hours, 00..23); $day (day of month, 01..31); $month (month, 01..12); $mon (month in text format, Jan..Dec); $year (4 digit year, 1999); $ye (2 digit year, 99), $wd (day number of the week, 1 for Sunday, 2 for Monday, etc), $wday (day of the week, Sun..Sat), $weekday (day of the week, Sunday..Saturday), $yearday (day of the year, 1..365, or 1..366 in leap years). Date is assumed to be server time; add GMT to indicate Greenwich time zone.
GET( name ) -- get the value of a previously set variable
Specify the variable name (alphanumeric characters and underscores). An empty string is returned if the variable does not exist. Use $SET() to set a variable first. Unlike table ranges, variables live for the time of the page view and persist across tables, i.e. you can use it to summarize results across several tables.
Syntax: $GET( name )
Example: %CALC{"$GET(my_total)"}% returns the value of the my_total variable
IF( condition, value if true, value if 0 ) -- return a value based on a condition
The condition can be a number (where 0 means condition not met), or two numbers with a comparison operator < (less than), <= (less than or equal), == (equal), != (not equal), >= (greater than or equal), > (greater than).
Syntax: $IF( condition, value if true, value if 0 )
Example: %CALC{"$IF($T(R1:C5) > 1000, Over Budget, OK)"}% returns Over Budget if value in R1:C5 is over 1000, OK if not
Example: %CALC{"$IF($EXACT($T(R1:C2),), empty, $T(R1:C2))"}% returns the content of R1:C2 or empty if empty
INT( formula ) -- evaluate formula and round down to nearest integer
Addition, substraction, multiplication, division and modulus of numbers are supported. Any nesting is permitted
Numbers may be decimal integers (1234), binary integers (0b1110011), octal integers (01234), hexadecimal integers (0x1234) or of exponential notation (12.34e-56)
If you expect a single decimal integer value with leading zeros, use $INT( $VALUE( number ) )
LISTJOIN( separator, list ) -- convert a list into a string
By default, list items are separated by a comma and a space. Use this function to indicate a specific separator string, which may include $comma for comma, $n for newline, and $sp for space.
Syntax: $LISTJOIN( separator, list )
Example: %CALC{"$LISTJOIN($n, Apple, Orange, Apple, Kiwi)"}% returns the four items separated by new lines
LISTMAP( formula, list ) -- evaluate and update each element of a list
In the formula you can use $item to indicate the element; $index to show the index of the list, starting at 1. If $item is omitted, the item is appended to the formula.
Useful to change the order of plugin execution. For example, it allows preprossing to be done before %SEARCH{}% is evaluated. The percent character '%' can be escaped with $per
> >
Useful to change the order of plugin execution. For example, it allows preprocessing to be done before %SEARCH{}% is evaluated. The percent character '%' can be escaped with $percnt. The quote character '"' can be escaped with $quot.
PROPERSPACE( text ) -- properly space out WikiWords
Properly spaces out WikiWords preceeded by white space, parenthesis, or ][. Words listed in the DONTSPACE TWikiPreferences variable or DONTSPACE plugins setting are excluded
Syntax: $PROPERSPACE( text )
Example: Assuming DONTSPACE contains MacDonald: %CALC{"$PROPERSPACE(Old MacDonald had a ServerFarm, EeEyeEeEyeOh)"}% returns Old MacDonald had a Server Farm, Ee Eye Ee Eye Oh
REPLACE( text, start, num, new ) -- replace part of a text string
Replace num number of characters of text string text, starting at start, with new text new. Starting position is 1; use a negative start to count from the end of the text
Evaluates a simple formula and rounds the result up or down to the number of digits if digits is positive; to the nearest integer if digits is missing; or to the left of the decimal point if digits is negative
SEARCH( string, text, start ) -- search a string within a text
Finds one text string, within another text, and returns the number of the starting position of string, from the first character of text. This search is a RegularExpression search; use $FIND() for non-regular expression searching. Starting position is 1; a 0 is returned if nothing is matched
SET( name, value ) -- set a variable for later use
Specify the variable name (alphanumeric characters and underscores) and the value. The value may contain a formula; formulae are evaluated before the variable assignment; see $NOEXEC() if you want to prevent that. This function returns no output. Use $GET() to retrieve variables. Unlike table ranges, variables live for the time of the page view and persist across tables, i.e. you can use it to summarize results across several tables and also across included topics
Syntax: $SET( name, value )
Example: %CALC{"$SET(my_total, $SUM($ABOVE()))"}% sets the my_total variable to the sum of all table cells located above the current cell and returns an empty string
SETIFEMPTY( name, value ) -- set a variable only if empty
Specify the variable name (alphanumeric characters and underscores) and the value.
Syntax: $SETIFEMPTY( name, value )
Example: %CALC{"$SETIFEMPTY(result, default)"}% sets the result variable to default if the variable is empty or 0; in any case an empty string is returned
SETM( name, formula ) -- update an existing variable based on a formula
Specify the variable name (alphanumeric characters and underscores) and the formula. The formula must start with an operator to + (add), - (subtract), * (multiply), or / (divide) something to the variable. This function returns no output. Use $GET() to retrieve variables
Syntax: $SETM( name, formula )
Example: %CALC{"$SETM(total, + $SUM($LEFT()))"}% adds the sum of all table cells on the left to the total variable, and returns an empty string
SUBSTITUTE( text, old, new, instance, option ) -- substitute text
Substitutes new text for old text in a text string. instance specifies which occurance of old you want to replace. If you specify instance, only that instance is replaced. Otherwise, every occurance is changed to the new text. A literal search is performed by default; a RegularExpression search if the option is set to r
SUBSTRING( text, start, num ) -- extract a substring out of a text string
Extract num number of characters of text string text, starting at start. Starting position is 1; use a negative start to count from the end of the text
SUMDAYS( list ) -- sum the days in a list or range of cells
The total number of days in a list or range of cells containing numbers of hours, days or weeks. The default unit is days; units are indicated by a h, hours, d, days, w, weeks suffix. One week is assumed to have 5 working days, one day 8 hours
Syntax: $SUMDAYS( list )
Example: %CALC{"$SUMDAYS(2w, 1, 2d, 4h)"}% returns 13.5, the evaluation of (2*5 + 1 + 2 + 4/8)
SUMPRODUCT( list, list ) -- scalar product on ranges of cells
Syntax: $SUMPRODUCT( list, list, list... )
Example: %CALC{"$SUMPRODUCT(R2:C1..R4:C1, R2:C5..R4:C5)"}% evaluates and returns the result of ($T(R2:C1) * $T(R2:C5) + $T(R3:C1) * $T(R3:C5) + $T(R4:C1) * $T(R4:C5))
TRANSLATE( text, from, to ) -- translate text from one set of characters to another
The translation is done from a set to a set, one character by one. The text may contain commas; all three parameters are required. In the from and to parameters you can write $comma to escape comma, $sp to escape space
TIME( text ) -- convert a date string into a serialized date number
Changed:
< <
Serialized date is seconds since the Epoch, e.g. midnight, 01 Jan 1970. Current time is taken if the date string is empty. Supported date formats: 31 Dec 2009; 31 Dec 2009 GMT; 31 Dec 09; 31-Dec-2009; 31/Dec/2009; 2009/12/31; 2009-12-31; 2009/12/31; 2009/12/31 23:59; 2009/12/31 - 23:59; 2009-12-31-23-59; 2009/12/31 - 23:59:59; 2009.12.31.23.59.59. Date is assumed to be server time; add GMT to indicate Greenwich time zone
> >
Serialized date is seconds since the Epoch, e.g. midnight, 01 Jan 1970. Current time is taken if the date string is empty. Supported date formats: 31 Dec 2009; 31 Dec 2009 GMT; 31 Dec 09; 31-Dec-2009; 31/Dec/2009; 31 Dec 2003 - 23:59; 31 Dec 2003 - 23:59:59; 2009/12/31; 2009-12-31; 2009/12/31; 2009/12/31 23:59; 2009/12/31 - 23:59; 2009-12-31-23-59; 2009/12/31 - 23:59:59; 2009.12.31.23.59.59. DOY (Day of Year) formats: DOY2003.365, DOY2003.365.23.59, DOY2003.365.23.59.59. Date is assumed to be server time; add GMT to indicate Greenwich time zone
TIMEADD( serial, value, unit ) -- add a value to a serialized date
The unit is seconds if not specified; unit can be second, minute, hour, day, week, month, year. Note: An approximation is used for month and year calculations
Syntax: $TIMEADD( serial, value, unit )
Example: %CALC{"$TIMEADD($TIME(), 2, week)"}% returns the serialized date two weeks from now
TIMEDIFF( serial_1, serial_2, unit ) -- time difference between two serialized dates
The unit is seconds if not specified; unit can be specified as in $TIMEADD().
Notes: An approximation is used for month and year calculations. Use $ROUND() to round day unit to account for daylight savings time change. Use $FORMAT(), $FORMATTIMEDIFF() or $INT() to format real numbers
Specifically, how can I output some conditional text in a FormattedSearch?
You need to escape the CALC so that it executes once per search hit. This can be done by escaping the % signs of %CALC{...}% with $percnt. For example, to execute $IF($EXACT($formfield(Tested), Yes), %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-yes.gif, %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-no.gif) in the format="" parameter, write this:
%SEARCH{ .... format="| $topic | $percntCALC{$IF($EXACT($formfield(Tested), Yes), %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-yes.gif, %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-no.gif)}$percnt |" }%
How can I easily repeat a formula in a table?
Changed:
< <
To repeat the same formula in all cells of a table row define the formula once in a preferences setting and use that in the CALC. The preferences setting can be hidden in HTML comments. Example:
> >
To repeat the same formula in all cells of a table row define the formula once in a preferences setting and use that in the CALC. The preferences setting can be defined at the site level, web level or topic level, and may be hidden in HTML comments. Example:
<!--
* Set MYFORMULA = $EVAL($SUBSTITUTE(...etc...))
-->
| A | 1 | %CALC{%MYFORMULA%}% |
| B | 2 | %CALC{%MYFORMULA%}% |
| C | 3 | %CALC{%MYFORMULA%}% |
Above table is created manually. Another plugin could build the table dynamically, e.g. by pulling data out of a bug tracking system. The Spreadsheet plugin can be used to display table data statistics.
> >
Above table is created manually. The table can be build dynamically with a formatted search, or by a plugin that pulls data from an external source, such as a bug tracking system.
Plugin Settings
Plugin settings are stored as preferences variables. To reference
a plugin setting write %<plugin>_<setting>%, i.e. %SPREADSHEETPLUGIN_SHORTDESCRIPTION%
Set SHORTDESCRIPTION = Add spreadsheet calculation like "$SUM( $ABOVE() )" to TWiki tables and other topic text
> >
Set SHORTDESCRIPTION = Add spreadsheet calculation like "$SUM( $ABOVE() )" to TWiki tables or anywhere in topic text
Debug plugin: (See output in data/debug.txt)
Set DEBUG = 0
Do not handle %CALC{}% variable in included topic while including topic: (default: 1)
Set SKIPINCLUDE = 1
WikiWords to exclude from being spaced out by the $PROPERSPACE(text) function. This comma delimited list can be overloaded by a DONTSPACE preferences variable:
Set DONTSPACE = CodeWarrior, MacDonald, McIntosh, RedHat, SuSE
Plugin Installation Instructions
Note: You do not need to install anything on the browser to use this plugin. Below installation instructions are for the administrator who needs to install this plugin on the TWiki server.
Download the ZIP file from the SpreadSheetPlugin home
Unzip SpreadSheetPlugin.zip in your twiki installation directory. Content:
Added $ABS(), $LISTIF(); fixed $VALUE() to remove leading zeros; changed $FIND() and $SEARCH() to return 0 instead of empty string if no match
21 Mar 2004:
Added $LISTITEM(); fixed call to unofficial function
16 Mar 2004:
Added $LISTMAP(), $LISTREVERSE(), $LISTSIZE(), $LISTSORT(), $LISTUNIQUE(), $SETM(); retired $COUNTUNIQUE() in favor of $COUNTITEMS($LISTUNIQUE()); fixed evaluation order issue of $IF(); fixed missing eval error messages suppressed since version 06 Mar 2004; redirect stderr messages to warning
08 Mar 2004:
Added $LIST()
06 Mar 2004:
Added $AND(), $MOD(), $NOT(), $OR(), $PRODUCT(), $PROPER(), $PROPERSPACE(), $RAND(), $REPEAT(), $SIGN(), $VALUE(); added digits parameter to $ROUND(); renamed $MULT() to $PRODUCT(); $MULT() is deprecated and undocumented
Added support for lists (1, 2, 3) and lists of table ranges (R1:C1..R1:C5, R3:C1..R3:C5) for all functions that accept a table range; added $TIMEADD(); in $TIMEDIFF() added week unit; in $FORMATTIME() changed $weekday to $wd and added $wday and $weekday
Added $FIND(), $NOP(), $REPLACE(), $SEARCH(), $SUBSTITUTE(), contributed by TWiki:Main/PaulineCheung
19 Apr 2003:
Added $COUNTSTR(), $EXACT(), $IF(), $ROUND(), $TRIM(); added $FORMAT(), contributed by TWiki:Main/JimStraus; support % modulus operator in $EVAL(), $INT(), and $ROUND(); fixed bug in $DEF()