TWiki Spreadsheet Plugin

This Plugin adds spreadsheet capabilities to TWiki topics. Formulae like CGI CGI %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.

Example:

Region: Sales:
Northeast 320
Northwest 580
South 240
Europe 610
Asia 220
Total: 1970

      Interactive example:

Formula: %CALC{""}%  
Result:     TWiki Guest

The formula next to "Total" is CGI CGI %CALC{"$SUM( $ABOVE() )"}%.
(you see the formula instead of the sum in case the Plugin is not installed or not enabled.)

On this page:

Syntax Rules

The action of this Plugin is triggered by the CGI CGI %CALC{"..."}% variable, which gets rendered according to the built-in function(s) found between the quotes.

  • Built-in function are of format CGI CGI $FUNCNAME(parameter)
  • Functions may be nested, e.g. CGI CGI %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
  • Multiple parameters form a list; they are separated by a comma, followed by optional space, e.g. CGI CGI %CALC{"$SUM( 3, 5, 7 )"}%
  • A table cell can be addressed as CGI CGI R1:C1. Table address matrix:
    CGI CGI R1:C1 CGI CGI R1:C2 CGI CGI R1:C3 CGI CGI R1:C4
    CGI CGI R2:C1 CGI CGI R2:C2 CGI CGI R2:C3 CGI CGI R2:C4
  • A table cell range is defined by two cell addresses separated by CGI CGI "..", e.g. "row 1 through 20, column 3" is: CGI CGI R1:C3..R20:C3
  • Lists can refer to values and/or table cell ranges, e.g. CGI CGI %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

Built-in Functions

Conventions for Syntax:

  • Required parameters are indicated in CGI CGI ( bold )
  • Optional parameters are indicated in CGI CGI ( bold italic )

ABOVE( ) -- address range of cells above the current cell

  • Syntax: CGI CGI $ABOVE( )
  • Example: CGI CGI %CALC{"$SUM($ABOVE())"}% returns the sum of cells above the current cell
  • Related: CGI $LEFT(), CGI $RIGHT()

ABS( num ) -- absolute value of a number

  • Syntax: CGI CGI $ABS( num )
  • Example: CGI CGI %CALC{"$ABS(-12.5)"}% returns CGI CGI 12.5
  • Related: CGI $SIGN(), CGI $EVEN(), CGI $ODD()

AND( list ) -- logical AND of a list

  • Syntax: CGI CGI $AND( list )
  • Example: CGI CGI %CALC{"$AND(1, 0, 1)"}% returns CGI CGI 0
  • Related: CGI $NOT(), CGI $IF(), CGI $OR()

AVERAGE( list ) -- average of a list or a range of cells

  • Syntax: CGI CGI $AVERAGE( list )
  • Example: CGI CGI %CALC{"$AVERAGE(R2:C5..R$ROW(-1):C5)"}% returns the average of column 5, excluding the title row
  • Related: CGI $LIST(), CGI $MAX(), CGI $MEDIAN(), CGI $MIN()

CHAR( number ) -- ASCII character represented by number

  • Syntax: CGI CGI $CHAR( number )
  • Example: Example: CGI CGI %CALC{"$CHAR(97)"}% returns CGI CGI a
  • Related: CGI $CODE()

CODE( text ) -- ASCII numeric value of character

  • The ASCII numeric value of the first character in text
  • Syntax: CGI CGI $CODE( text )
  • Example: CGI CGI %CALC{"$CODE(abc)"}% returns CGI CGI 97
  • Related: CGI $CHAR()

COLUMN( offset ) -- current column number

  • The current table column number with an optional offset
  • Syntax: CGI CGI $COLUMN( offset )
  • Example: CGI CGI %CALC{"$COLUMN()"}% returns CGI CGI 2 for the second column
  • Related: CGI $ROW(), CGI $T()

COUNTITEMS( list ) -- count individual items in a list

  • Syntax: CGI CGI $COUNTITEMS( list )
  • Example: CGI CGI %CALC{"$COUNTITEMS($ABOVE())"}% returns CGI CGI Closed: 1, Open: 2 assuming one cell above the current cell contains CGI CGI Closed and two cells contain CGI CGI Open
  • Related: CGI $COUNTSTR(), CGI $LIST()

COUNTSTR( list, str ) -- count the number of cells in a list equal to a given string

  • Count the number of cells in a list equal to a given string (if str is specified), or counts the number of non empty cells in a list
  • Syntax: CGI CGI $COUNTSTR( list, str )
  • Example: CGI CGI %CALC{"$COUNTSTR($ABOVE())"}% counts the number of non empty cells above the current cell
  • Example: CGI CGI %CALC{"$COUNTSTR($ABOVE(), DONE)"}% counts the number of cells equal to CGI CGI DONE
  • Related: CGI $COUNTITEMS(), CGI $LIST()

DEF( list ) -- find first non-empty list item or cell

  • Returns the first list item or cell reference that is not empty
  • Syntax: CGI CGI $DEF( list )
  • Example: CGI CGI %CALC{"$DEF(R1:C1..R1:C3)"}%
  • Related: CGI $COUNTSTR(), CGI $LISTIF(), CGI $LIST()

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 (CGI 1234), binary integers (CGI 0b1110011), octal integers (CGI 01234), hexadecimal integers (CGI 0x1234) or of exponential notation (CGI 12.34e-56)
  • Syntax: CGI CGI $EVAL( formula )
  • Example: CGI CGI %CALC{"$EVAL( (5 * 3) / 2 + 1.1 )"}% returns CGI CGI 8.6
  • Related: CGI $EXEC(), CGI $INT(), CGI $MOD(), CGI $ROUND(), CGI $VALUE()

EVEN( num ) -- test for even number

  • Syntax: CGI CGI $EVEN( num )
  • Example: CGI CGI %CALC{"$EVEN(2)"}% returns CGI CGI 1
  • Related: CGI $ABS(), CGI $MOD(), CGI $ODD(), CGI $SIGN()

EXACT( text1, text2 ) -- compare two text strings

  • Compares two text strings and returns CGI CGI 1 if they are exactly the same, or CGI CGI 0 if not
  • Syntax: CGI CGI $EXACT( text1, text2 )
  • Example: CGI CGI %CALC{"$EXACT(foo, Foo)"}% returns CGI CGI 0
  • Example: CGI CGI %CALC{"$EXACT(foo, $LOWER(Foo))"}% returns CGI CGI 1
  • Related: CGI $IF(), CGI $TRIM()

EXEC( formula ) -- execute a spreadsheet formula

  • 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: CGI CGI $EXEC( formula )
  • Example: CGI CGI %CALC{"$SET(msg, $NOEXEC(Hi $GET(name)))"}% sets the CGI msg variable with raw formula CGI Hi $GET(name)
  • Example: CGI CGI %CALC{"$SET(name, Tom) $EXEC($GET(msg))"}% executes content of CGI msg variable and returns CGI Hi Tom
  • Example: CGI CGI %CALC{"$SET(name, Jerry) $EXEC($GET(msg))"}% returns CGI Hi Jerry
  • Related: CGI $EVAL(), CGI $GET(), CGI $NOEXEC(), CGI $SET()

EXISTS( topic ) -- check if topic exists

  • Topic can be CGI TopicName? or a CGI TopicName? . Current web is used if web is not specified.
  • Syntax: CGI CGI $EXISTS( topic )
  • Example: CGI CGI %CALC{"$EXISTS(WebHome)"}% returns CGI CGI 1
  • Example: CGI CGI %CALC{"$EXISTS(ThisDoesNotExist)"}% returns CGI CGI 0

EXP( num ) -- exponent (e) raised to the power of a number

  • EXP is the inverse of the LN function
  • Syntax: CGI CGI $EXP( num )
  • Example: CGI CGI %CALC{"$EXP(1)"}% returns CGI CGI 2.71828182845905
  • Related: CGI $LN(), CGI $LOG()

FIND( string, text, start ) -- find one string within another string

  • Finds one text CGI string, within another CGI text, and returns the number of the starting position of CGI string, from the first character of CGI text. This search is case sensitive and is not a regular expression search; use CGI $SEARCH() for regular expression searching. Starting position is 1; a 0 is returned if nothing is matched.
  • Syntax: CGI CGI $FIND( string, text, start )
  • Example: CGI CGI %CALC{"$FIND(f, fluffy)"}% returns CGI CGI 1
  • Example: CGI CGI %CALC{"$FIND(f, fluffy, 2)"}% returns CGI CGI 4
  • Example: CGI CGI %CALC{"$FIND(@, fluffy, 1)"}% returns CGI CGI 0
  • Related: CGI $REPLACE(), CGI $SEARCH()

FORMAT( type, precision, number ) -- format a number to a certain type and precision

  • Supported CGI type:
    • CGI COMMA for comma format, such as CGI 12,345.68
    • CGI DOLLAR for Dollar format, such as CGI $12,345.68
    • CGI KB for Kilo Byte format, such as CGI 1205.63 KB
    • CGI MB for Mega Byte format, such as CGI 1.18 MB
    • CGI KBMB for Kilo/Mega/Giga/Tera Byte auto-adjust format
    • CGI NUMBER for number, such as CGI 12345.7
    • CGI PERCENT for percent format, such as CGI 12.3%
  • The CGI precision indicates the the number of digits after the dot
  • Syntax: CGI CGI $FORMAT( type, prec, number )
  • Example: CGI CGI %CALC{"$FORMAT(COMMA, 2, 12345.6789)"}% returns CGI CGI 12,345.68
  • Example: CGI CGI %CALC{"$FORMAT(DOLLAR, 2, 12345.67)"}% returns CGI CGI $12,345.68
  • Example: CGI CGI %CALC{"$FORMAT(KB, 2, 1234567)"}% returns CGI CGI 1205.63 KB
  • Example: CGI CGI %CALC{"$FORMAT(MB, 2, 1234567)"}% returns CGI CGI 1.18 MB
  • Example: CGI CGI %CALC{"$FORMAT(KBMB, 2, 1234567)"}% returns CGI CGI 1.18 MB
  • Example: CGI CGI %CALC{"$FORMAT(KBMB, 2, 1234567890)"}% returns CGI CGI 1.15 GB
  • Example: CGI CGI %CALC{"$FORMAT(NUMBER, 1, 12345.67)"}% returns CGI CGI 12345.7
  • Example: CGI CGI %CALC{"$FORMAT(PERCENT, 1, 0.1234567)"}% returns CGI CGI 12.3%
  • Related: CGI $FORMATTIME(), CGI $FORMATTIMEDIFF(), CGI $ROUND()

FORMATGMTIME( serial, text ) -- convert a serialized date into a GMT date string

  • The date string represents the time in Greenwich time zone. Same variable expansion as in CGI $FORMATTIME().
  • Syntax: CGI CGI $FORMATGMTIME( serial, text )
  • Example: CGI CGI %CALC{"$FORMATGMTIME(1041379200, $day $mon $year)"}% returns CGI CGI 01 Jan 2003
  • Related: CGI $FORMATTIME(), CGI $FORMATTIMEDIFF(), CGI $TIME(), CGI $TIMEADD(), CGI $TIMEDIFF(), CGI $TODAY()

FORMATTIME( serial, text ) -- convert a serialized date into a date string

  • The following variables in CGI text are expanded: CGI $second (seconds, 00..59); CGI $minute (minutes, 00..59); CGI $hour (hours, 00..23); CGI $day (day of month, 01..31); CGI $month (month, 01..12); CGI $mon (month in text format, Jan..Dec); CGI $year (4 digit year, 1999); CGI $ye (2 digit year, 99), CGI $wd (day number of the week, 1 for Sunday, 2 for Monday, etc), CGI $wday (day of the week, Sun..Sat), CGI $weekday (day of the week, Sunday..Saturday), CGI $yearday (day of the year, 1..365, or 1..366 in leap years). Date is assumed to be server time; add CGI GMT to indicate Greenwich time zone.
  • Syntax: CGI CGI $FORMATTIME( serial, text )
  • Example: CGI CGI %CALC{"$FORMATTIME(0, $year/$month/$day GMT)"}% returns CGI CGI 1970/01/01 GMT
  • Related: CGI $FORMATGMTIME(), CGI $TIME(), CGI $FORMATTIMEDIFF(), CGI $TIMEADD(), CGI $TIMEDIFF(), CGI $TODAY()

FORMATTIMEDIFF( unit, precision, time ) -- convert elapsed time to a string

  • Convert elapsed CGI time to a human readable format, such as: CGI 12 hours and 3 minutes
  • The input CGI unit can be CGI second, CGI minute, CGI hour, CGI day, CGI month, CGI year. Note: An approximation is used for month and year calculations.
  • The CGI precision indicates the number of output units to use
  • Syntax: CGI CGI $FORMATTIMEDIFF( unit, precision, time )
  • Example: CGI CGI %CALC{"$FORMATTIMEDIFF(min, 1, 200)"}% returns CGI CGI 3 hours
  • Example: CGI CGI %CALC{"$FORMATTIMEDIFF(min, 2, 200)"}% returns CGI CGI 3 hours and 20 minutes
  • Example: CGI CGI %CALC{"$FORMATTIMEDIFF(min, 1, 1640)"}% returns CGI CGI 1 day
  • Example: CGI CGI %CALC{"$FORMATTIMEDIFF(min, 2, 1640)"}% returns CGI CGI 1 day and 3 hours
  • Example: CGI CGI %CALC{"$FORMATTIMEDIFF(min, 3, 1640)"}% returns CGI CGI 1 day, 3 hours and 20 minutes
  • Related: CGI $FORMATTIME(), CGI $TIME(), CGI $TIMEADD(), CGI $TIMEDIFF()

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 CGI $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: CGI CGI $GET( name )
  • Example: CGI CGI %CALC{"$GET(my_total)"}% returns the value of the CGI my_total variable
  • Related: CGI $EXEC(), CGI $NOEXEC(), CGI $SET(), CGI $SETIFEMPTY(), CGI $SETM()

IF( condition, value if true, value if 0 ) -- return a value based on a condition

  • The condition can be a number (where CGI CGI 0 means condition not met), or two numbers with a comparison operator CGI CGI < (less than), CGI CGI <= (less than or equal), CGI CGI == (equal), CGI CGI != (not equal), CGI CGI >= (greater than or equal), CGI CGI > (greater than).
  • Syntax: CGI CGI $IF( condition, value if true, value if 0 )
  • Example: CGI CGI %CALC{"$IF($T(R1:C5) > 1000, Over Budget, OK)"}% returns CGI CGI Over Budget if value in R1:C5 is over 1000, CGI CGI OK if not
  • Example: CGI CGI %CALC{"$IF($EXACT($T(R1:C2),), empty, $T(R1:C2))"}% returns the content of R1:C2 or CGI CGI empty if empty
  • Example: CGI CGI %CALC{"$SET(val, $IF($T(R1:C2) == 0, zero, $T(R1:C2)))"}% sets a variable conditionally
  • Related: CGI $AND(), CGI $EXACT(), CGI $LISTIF(), CGI $NOT(), CGI $OR()

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 (CGI 1234), binary integers (CGI 0b1110011), octal integers (CGI 01234), hexadecimal integers (CGI 0x1234) or of exponential notation (CGI 12.34e-56)
  • If you expect a single decimal integer value with leading zeros, use CGI CGI $INT( $VALUE( number ) )
  • Syntax: CGI CGI $INT( formula )
  • Example: CGI CGI %CALC{"$INT(10 / 4)"}% returns CGI CGI 2
  • Example: CGI CGI %CALC{"$INT($VALUE(09))"}% returns CGI CGI 9
  • Related: CGI $EVAL(), CGI $ROUND(), CGI $VALUE()

LEFT( ) -- address range of cells to the left of the current cell

  • Syntax: CGI CGI $LEFT( )
  • Example: CGI CGI %CALC{"$SUM($LEFT())"}% returns the sum of cells to the left of the current cell
  • Related: CGI $ABOVE(), CGI $RIGHT()

LENGTH( text ) -- length of text in bytes

  • Syntax: CGI CGI $LENGTH( text )
  • Example: CGI CGI %CALC{"$LENGTH(abcd)"}% returns CGI CGI 4
  • Related: CGI $LISTSIZE()

LIST( range ) -- convert content of a cell range into a list

LISTIF( condition, list ) -- remove elements from a list that do not meet a condition

  • In addition to the condition described in CGI $IF(), you can use CGI CGI $item to indicate the current element, and CGI CGI $index for the list index, starting at 1
  • Syntax: CGI CGI $LISTIF( condition, list )
  • Example: CGI CGI %CALC{"$LISTIF($item > 12, 14, 7, 25)"}% returns CGI CGI 14, 25
  • Example: CGI CGI %CALC{"$LISTIF($NOT($EXACT($item,)), A, B, , E)"}% returns non-empty elements CGI CGI A, B, E
  • Example: CGI CGI %CALC{"$LISTIF($index > 2, A, B, C, D)"}% returns CGI CGI C, D
  • Related: CGI $IF(), CGI $LIST(), CGI $LISTITEM(), CGI $LISTMAP(), CGI $LISTREVERSE(), CGI $LISTSIZE(), CGI $LISTSORT(), CGI $LISTUNIQUE(), CGI $SUM()

LISTITEM( index, list ) -- get one element of a list

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 CGI separator string, which may include CGI $comma for comma, CGI $n for newline, and CGI $sp for space.
  • Syntax: CGI CGI $LISTJOIN( separator, list )
  • Example: CGI CGI %CALC{"$LISTJOIN($n, Apple, Orange, Apple, Kiwi)"}% returns the four items separated by new lines
  • Related: CGI $LIST(), CGI $LISTSIZE()

LISTMAP( formula, list ) -- evaluate and update each element of a list

LISTRAND( list ) -- get one random element of a list

LISTREVERSE( list ) -- opposite order of a list

LISTSIZE( list ) -- number of elements in a list

LISTSHUFFLE( list ) -- shuffle element of a list in random order

LISTSORT( list ) -- sort a list

LISTTRUNCATE( size, list ) -- truncate list to size

LISTUNIQUE( list ) -- remove all duplicates from a list

LN( num ) -- natural logarithm of a number

  • LN is the inverse of the EXP function
  • Syntax: CGI CGI $LN( num )
  • Example: CGI CGI %CALC{"$LN(10)"}% returns CGI CGI 2.30258509299405
  • Related: CGI $EXP(), CGI $LOG()

LOG( num, base ) -- logarithm of a number to a given base

  • base-10 logarithm of a number (if base is 0 or not specified), else logarithm of a number to the given base
  • Syntax: CGI CGI $LOG( num, base )
  • Example: CGI CGI %CALC{"$LOG(1000)"}% returns CGI CGI 3
  • Example: CGI CGI %CALC{"$LOG(16, 2)"}% returns CGI CGI 4
  • Related: CGI $EXP(), CGI $LN()

LOWER( text ) -- lower case string of a text

  • Syntax: CGI CGI $LOWER(text)
  • Example: CGI CGI %CALC{"$LOWER( $T(R1:C5) )"}% returns the lower case string of the text in cell CGI CGI R1:C5
  • Related: CGI $PROPER(), CGI $PROPERSPACE(), CGI $TRIM(), CGI $UPPER()

MAX( list ) - biggest value of a list or range of cells

  • Syntax: CGI CGI $MAX( list )
  • Example: To find the biggest number to the left of the current cell, write: CGI CGI %CALC{"$MAX($LEFT())"}%
  • Related: CGI $LIST(), CGI $MEDIAN(), CGI $MIN(), CGI $PERCENTILE()

MEDIAN( list ) -- median of a list or range of cells

  • Syntax: CGI CGI $MEDIAN( list )
  • Example: CGI CGI %CALC{"$MEDIAN(3, 9, 4, 5)"}% returns CGI CGI 4.5
  • Related: CGI $LIST(), CGI $MAX(), CGI $MIN(), CGI $PERCENTILE()

MIN( list ) -- smallest value of a list or range of cells

MOD( num, divisor ) -- reminder after dividing CGI CGI num by CGI CGI divisor

  • Syntax: CGI CGI $MOD( num, divisor )
  • Example: CGI CGI %CALC{"$MOD(7, 3)"}% returns CGI CGI 1
  • Related: CGI $EVAL()

NOEXEC( formula ) -- do not execute a spreadsheet formula

  • Prevent a formula from getting executed. This is typically used to store a raw formula in a variable for later use as described in CGI $EXEC().
  • Syntax: CGI CGI $NOEXEC( formula )
  • Example: CGI CGI %CALC{"$SET(msg, $NOEXEC(Hi $GET(name)))"}% sets the CGI msg variable with the formula CGI Hi $GET(name) without executing it
  • Related: CGI $EVAL(), CGI $EXEC(), CGI $GET(), CGI $SET()

NOP( text ) -- no-operation

  • Useful to change the order of Plugin execution. For example, it allows preprossing to be done before CGI %SEARCH{}% is evaluated. The percent character '%' can be escaped with CGI $per
  • Syntax: CGI CGI $NOP( text )

NOT( num ) -- reverse logic of a number

  • Returns 0 if CGI CGI num is not zero, 1 if zero
  • Syntax: CGI CGI $NOT( num )
  • Example: CGI CGI %CALC{"$NOT(0)"}% returns CGI CGI 1
  • Related: CGI $AND(), CGI $IF(), CGI $OR()

ODD( num ) -- test for odd number

  • Syntax: CGI CGI $ODD( num )
  • Example: CGI CGI %CALC{"$ODD(2)"}% returns CGI CGI 0
  • Related: CGI $ABS(), CGI $EVEN(), CGI $MOD(), CGI $SIGN()

OR( list ) -- logical OR of a list

  • Syntax: CGI CGI $OR( list )
  • Example: CGI CGI %CALC{"$OR(1, 0, 1)"}% returns CGI CGI 1
  • Related: CGI $AND(), CGI $IF(), CGI $NOT()

PERCENTILE( num, list ) -- percentile of a list or range of cells

  • Calculates the num-th percentile, useful to establish a threshold of acceptance. num is the percentile value, range 0..100
  • Syntax: CGI CGI $PERCENTILE( num, list )
  • Example: CGI CGI %CALC{"$PERCENTILE(75, 400, 200, 500, 100, 300)"}% returns CGI CGI 450
  • Related: CGI $LIST(), CGI $MAX(), CGI $MEDIAN(), CGI $MIN()

PI( ) -- mathematical constant Pi, 3.14159265358979

  • Syntax: CGI CGI $PI( )
  • Example: CGI CGI %CALC{"$PI()"}% returns CGI CGI 3.14159265358979

PRODUCT( list ) -- product of a list or range of cells

  • Syntax: CGI CGI $PRODUCT( list )
  • Example: To calculate the product of the cells to the left of the current one use CGI CGI %CALC{"$PRODUCT($LEFT())"}%
  • Related: CGI $LIST(), CGI $PRODUCT(), CGI $SUM(), CGI $SUMPRODUCT()

PROPER( text ) -- properly capitalize text

  • Capitalize letters that follow any character other than a letter; convert all other letters to lowercase letters
  • Syntax: CGI CGI $PROPER( text )
  • Example: CGI CGI %CALC{"$PROPER(a small STEP)"}% returns CGI CGI A Small Step
  • Example: CGI CGI %CALC{"$PROPER(f1 (formula-1))"}% returns CGI CGI F1 (Formula-1)
  • Related: CGI $LOWER(), CGI $PROPERSPACE(), CGI $TRIM(), CGI $UPPER()

PROPERSPACE( text ) -- properly space out WikiWords

  • Properly spaces out WikiWords preceeded by white space, parenthesis, or CGI CGI ][. Words listed in the DONTSPACE TWikiPreferences variable or DONTSPACE Plugins setting are excluded
  • Syntax: CGI CGI $PROPERSPACE( text )
  • Example: Assuming DONTSPACE contains MacDonald: CGI CGI %CALC{"$PROPERSPACE(Old MacDonald had a ServerFarm, EeEyeEeEyeOh)"}% returns CGI CGI Old MacDonald had a Server Farm, Ee Eye Ee Eye Oh
  • Related: CGI $LOWER(), CGI $PROPER(), CGI $TRIM(), CGI $UPPER()

RAND( max ) -- random number

  • Random number, evenly distributed between 0 and CGI CGI max, or 0 and 1 if max is not specified
  • Syntax: CGI CGI $RAND( max )
  • Related: CGI $EVAL(), CGI $LISTRAND(), CGI $LISTSHUFFLE()

REPEAT( text, num ) -- repeat text a number of times

  • Syntax: CGI CGI $REPEAT( text, num )
  • Example: CGI CGI %CALC{"$REPEAT(/\, 5)"}% returns CGI CGI /\/\/\/\/\

REPLACE( text, start, num, new ) -- replace part of a text string

  • Replace CGI num number of characters of text string CGI text, starting at CGI start, with new text CGI new. Starting position is 1; use a negative CGI start to count from the end of the text
  • Syntax: CGI CGI $REPLACE( text, start, num, new )
  • Example: CGI CGI %CALC{"$REPLACE(abcdefghijk,6,5,*)"}% returns CGI CGI abcde*k
  • Related: CGI $FIND(), CGI $SEARCH(), CGI $SUBSTITUTE(), CGI $TRANSLATE()

RIGHT( ) -- address range of cells to the right of the current cell

  • Syntax: CGI CGI $RIGHT( )
  • Example: CGI CGI %CALC{"$SUM($RIGHT())"}% returns the sum of cells to the right of the current cell
  • Related: CGI $ABOVE(), CGI $LEFT()

ROUND( formula, digits ) -- round a number

  • Evaluates a simple CGI CGI formula and rounds the result up or down to the number of digits if CGI CGI digits is positive; to the nearest integer if digits is missing; or to the left of the decimal point if digits is negative
  • Syntax: CGI CGI $ROUND( formula, digits )
  • Example: CGI CGI %CALC{"$ROUND(3.15, 1)"}% returns CGI CGI 3.2
  • Example: CGI CGI %CALC{"$ROUND(3.149, 1)"}% returns CGI CGI 3.1
  • Example: CGI CGI %CALC{"$ROUND(-2.475, 2)"}% returns CGI CGI -2.48
  • Example: CGI CGI %CALC{"$ROUND(34.9, -1)"}% returns CGI CGI 30
  • Related: CGI $INT(), CGI $FORMAT()

ROW( offset ) -- current row number

  • The current table row number with an optional offset
  • Syntax: CGI CGI $ROW( offset )
  • Example: To get the number of rows excluding table heading (first row) and summary row (last row you are in), write: CGI CGI %CALC{"$ROW(-2)"}%
  • Related: CGI $COLUMN(), CGI $T()

SEARCH( string, text, start ) -- search a string within a text

  • Finds one text CGI string, within another CGI text, and returns the number of the starting position of CGI string, from the first character of CGI text. This search is a RegularExpression search; use CGI $FIND() for non-regular expression searching. Starting position is 1; a 0 is returned if nothing is matched
  • Syntax: CGI CGI $SEARCH( string, text, start )
  • Example: CGI CGI %CALC{"$SEARCH([uy], fluffy)"}% returns CGI CGI 3
  • Example: CGI CGI %CALC{"$SEARCH([uy], fluffy, 3)"}% returns CGI CGI 6
  • Example: CGI CGI %CALC{"$SEARCH([abc], fluffy,)"}% returns CGI CGI 0
  • Related: CGI $FIND(), CGI $REPLACE()

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 CGI $NOEXEC() if you want to prevent that. This function returns no output. Use CGI $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: CGI CGI $SET( name, value )
  • Example: CGI CGI %CALC{"$SET(my_total, $SUM($ABOVE()))"}% sets the CGI my_total variable to the sum of all table cells located above the current cell and returns an empty string
  • Related: CGI $EXEC(), CGI $GET(), CGI $NOEXEC(), CGI $SETIFEMPTY(), CGI SETM()

SETIFEMPTY( name, value ) -- set a variable only if empty

  • Specify the variable name (alphanumeric characters and underscores) and the value.
  • Syntax: CGI CGI $SETIFEMPTY( name, value )
  • Example: CGI CGI %CALC{"$SETIFEMPTY(result, default)"}% sets the CGI result variable to CGI default if the variable is empty or 0; in any case an empty string is returned
  • Related: CGI $GET(), CGI $SET()

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 CGI CGI + (add), CGI CGI - (subtract), CGI CGI * (multiply), or CGI CGI / (divide) something to the variable. This function returns no output. Use CGI $GET() to retrieve variables
  • Syntax: CGI CGI $SETM( name, formula )
  • Example: CGI CGI %CALC{"$SETM(total, + $SUM($LEFT()))"}% adds the sum of all table cells on the left to the CGI total variable, and returns an empty string
  • Related: CGI $GET(), CGI $SET(), CGI $SETIFEMPTY()

SIGN( num ) -- sign of a number

  • Returns -1 if CGI CGI num is negative, 0 if zero, or 1 if positive
  • Syntax: CGI CGI $SIGN( num )
  • Example: CGI CGI %CALC{"$SIGN(-12.5)"}% returns CGI CGI -1
  • Related: CGI $ABS(), CGI $EVAL(), CGI $EVEN(), CGI $INT(), CGI $NOT(), CGI $ODD()

SQRT( num ) -- square root of a number

  • Syntax: CGI CGI $SQRT( num )
  • Example: CGI CGI %CALC{"$SQRT(16)"}% returns CGI CGI 4

SUBSTITUTE( text, old, new, instance, option ) -- substitute text

  • Substitutes CGI new text for CGI old text in a CGI text string. CGI instance specifies which occurance of CGI old you want to replace. If you specify CGI 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 CGI option is set to CGI CGI r
  • Syntax: CGI CGI $SUBSTITUTE( text, old, new, instance, option )
  • Example: CGI CGI %CALC{"$SUBSTITUTE(Good morning, morning, day)"}% returns CGI CGI Good day
  • Example: CGI CGI %CALC{"$SUBSTITUTE(Q2-2002,2,3)"}% returns CGI CGI Q3-3003
  • Example: CGI CGI %CALC{"$SUBSTITUTE(Q2-2002,2,3,3)"}% returns CGI CGI Q2-2003
  • Example: CGI CGI %CALC{"$SUBSTITUTE(abc123def,[0-9],9,,r)"}% returns CGI CGI abc999def
  • Related: CGI $REPLACE(), CGI $TRANSLATE()

SUM( list ) -- sum of a list or range of cells

  • Syntax: CGI CGI $SUM( list )
  • Example: To sum up column 5 excluding the title row, write CGI CGI %CALC{"$SUM(R2:C5..R$ROW(-1):C5)"}% in the last row; or simply CGI CGI %CALC{"$SUM($ABOVE())"}%
  • Related: CGI $LIST(), CGI $PRODUCT(), CGI $SUMPRODUCT(), CGI $WORKINGDAYS()

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 CGI CGI h, CGI CGI hours, CGI CGI d, CGI CGI days, CGI CGI w, CGI CGI weeks suffix. One week is assumed to have 5 working days, one day 8 hours
  • Syntax: CGI CGI $SUMDAYS( list )
  • Example: CGI CGI %CALC{"$SUMDAYS(2w, 1, 2d, 4h)"}% returns CGI CGI 13.5, the evaluation of CGI (2*5 + 1 + 2 + 4/8)
  • Related: CGI $SUM(), CGI $TIME(), CGI $FORMATTIME()

SUMPRODUCT( list, list ) -- scalar product on ranges of cells

  • Syntax: CGI CGI $SUMPRODUCT( list, list, list... )
  • Example: CGI CGI %CALC{"$SUMPRODUCT(R2:C1..R4:C1, R2:C5..R4:C5)"}% evaluates and returns the result of CGI CGI ($T(R2:C1) * $T(R2:C5) + $T(R3:C1) * $T(R3:C5) + $T(R4:C1) * $T(R4:C5))
  • Related: CGI $LIST(), CGI $PRODUCT(), CGI $SUM()

T( address ) -- content of a cell

  • Syntax: CGI CGI $T( address )
  • Example: CGI CGI %CALC{"$T(R1:C5)"}% returns the text in cell CGI CGI R1:C5
  • Related: CGI $COLUMN(), CGI $ROW()

TRANSLATE( text, from, to ) -- translate text from one set of characters to another

  • The translation is done CGI from a set CGI to a set, one character by one. The CGI text may contain commas; all three parameters are required. In the CGI from and CGI to parameters you can write CGI $comma to escape comma, CGI $sp to escape space
  • Syntax: CGI CGI $TRANSLATE( text, from, to )
  • Example: CGI CGI %CALC{"$TRANSLATE(boom,bm,cl)"}% returns CGI CGI cool
  • Example: CGI CGI %CALC{"$TRANSLATE(one, two,$comma,;)"}% returns CGI CGI one; two
  • Related: CGI $REPLACE(), CGI $SUBSTITUTE()

TIME( text ) -- convert a date string into a serialized date number

  • 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: CGI 31 Dec 2009; CGI 31 Dec 2009 GMT; CGI 31 Dec 09; CGI 31-Dec-2009; CGI 31/Dec/2009; CGI 2009/12/31; CGI 2009-12-31; CGI 2009/12/31; CGI 2009/12/31 23:59; CGI 2009/12/31 - 23:59; CGI 2009-12-31-23-59; CGI 2009/12/31 - 23:59:59; CGI 2009.12.31.23.59.59. Date is assumed to be server time; add CGI GMT to indicate Greenwich time zone
  • Syntax: CGI CGI $TIME( text )
  • Example: CGI CGI %CALC{"$TIME(2003/10/14 GMT)"}% returns CGI CGI 1066089600
  • Related: CGI $FORMATGMTIME(), CGI $FORMATTIME(), CGI $FORMATTIMEDIFF(), CGI $TIMEADD(), CGI $TIMEDIFF(), CGI $TODAY(), CGI $WORKINGDAYS()

TIMEADD( serial, value, unit ) -- add a value to a serialized date

  • The CGI unit is seconds if not specified; unit can be CGI second, CGI minute, CGI hour, CGI day, CGI week, CGI month, CGI year. Note: An approximation is used for month and year calculations
  • Syntax: CGI CGI $TIMEADD( serial, value, unit )
  • Example: CGI CGI %CALC{"$TIMEADD($TIME(), 2, week)"}% returns the serialized date two weeks from now
  • Related: CGI $FORMATTIME(), CGI $FORMATGMTIME(), CGI $TIME(), CGI $TIMEDIFF(), CGI $TODAY()

TIMEDIFF( serial_1, serial_2, unit ) -- time difference between two serialized dates

  • The CGI unit is seconds if not specified; unit can be specified as in CGI $TIMEADD(). Note: An approximation is used for month and year calculations. Use CGI $FORMAT(), CGI $FORMATTIMEDIFF() or CGI $INT() to format real numbers
  • Syntax: CGI CGI $TIMEDIFF( serial_1, serial_2, unit )
  • Example: CGI CGI %CALC{"$TIMEDIFF($TIME(), $EVAL($TIME()+90), minute)"}% returns CGI CGI 1.5
  • Related: CGI $FORMAT(), CGI $FORMATGMTIME(), CGI $FORMATTIME(), CGI $FORMATTIMEDIFF(), CGI $INT(), CGI $TIME(), CGI $TIMEADD(), CGI $TODAY(), CGI $WORKINGDAYS()

TODAY( ) -- serialized date of today at midnight GMT

  • In contrast, the related CGI $TIME() returns the serialized date of today at the current time, e.g. it includes the number of seconds since midnight GMT
  • Syntax: CGI CGI $TODAY( )
  • Example: CGI CGI %CALC{"$TODAY()"}% returns the number of seconds since Epoch
  • Related: CGI $FORMATTIME(), CGI $FORMATGMTIME(), CGI $TIME(), CGI $TIMEADD(), CGI $TIMEDIFF()

TRIM( text ) -- trim spaces from text

  • Removes all spaces from text except for single spaces between words
  • Syntax: CGI CGI $TRIM( text )
  • Example: CGI CGI %CALC{"$TRIM( eat  spaces  )"}% returns CGI CGI eat spaces
  • Related: CGI $EXACT(), CGI $PROPERSPACE()

UPPER( text ) -- upper case string of a text

  • Syntax: CGI CGI $UPPER( text )
  • Example: CGI CGI %CALC{"$UPPER($T(R1:C5))"}% returns the upper case string of the text in cell CGI CGI R1:C5
  • Related: CGI $LOWER(), CGI $PROPER(), CGI $PROPERSPACE(), CGI $TRIM()

VALUE( text ) -- convert text to number

  • Extracts a number from CGI CGI text. Returns CGI CGI 0 if not found
  • Syntax: CGI CGI $VALUE( text )
  • Example: CGI CGI %CALC{"$VALUE(US$1,200)"}% returns CGI CGI 1200
  • Example: CGI CGI %CALC{"$VALUE(PrjNotebook1234)"}% returns CGI CGI 1234
  • Example: CGI CGI %CALC{"$VALUE(Total: -12.5)"}% returns CGI CGI -12.5
  • Related: CGI $EVAL(), CGI $INT()

WORKINGDAYS( serial_1, serial_2 ) -- working days between two serialized dates

  • Working days are Monday through Friday (sorry, Israel!)
  • Syntax: CGI CGI $WORKINGDAYS( serial_1, serial_2 )
  • Example: CGI CGI %CALC{"$WORKINGDAYS($TIME(2004/07/15), $TIME(2004/08/03))"}% returns CGI CGI 13
  • Related: CGI $SUMDAYS(), CGI $TIME(), CGI $TIMEDIFF()

FAQ

Can I use CALC in a formatted search?

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 CGI % signs of CGI %CALC{...}% with CGI $percnt. For example, to execute CGI $IF($EXACT($formfield(Tested), Yes), %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-yes.gif, %PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/choice-no.gif) in the CGI format="" parameter, write this:

CGI %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?

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:

<!--
   * Set MYFORMULA = $EVAL($SUBSTITUTE(...etc...))
-->
| A | 1 | %CALC{%MYFORMULA%}% |
| B | 2 | %CALC{%MYFORMULA%}% |
| C | 3 | %CALC{%MYFORMULA%}% |

Bug Tracking Example

Bug#: Priority: Subject: Status: Days to fix
Bug:1231 Low File Open ... Open 3
Bug:1232 High Memory Window ... Fixed 2
Bug:1233 Medium Usability issue ... Assigned 5
Bug:1234 High No arrange ... Fixed 1
Total: 4 High: 2
Low: 1
Medium: 1
. Assigned: 1
Fixed: 2
Open: 1
Total: 11

The last row is defined as:

| Total: %CALC{"$ROW(-2)"}% \ 
  | %CALC{"$COUNTITEMS( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% | . \ 
  | %CALC{"$COUNTITEMS( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% \ 
  |  Total: %CALC{"$SUM( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% |

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.

Plugin Settings

Plugin settings are stored as preferences variables. To reference a plugin setting write CGI CGI %<plugin>_<setting>%, i.e. CGI CGI %SPREADSHEETPLUGIN_SHORTDESCRIPTION%

  • One line description, is shown in the TextFormattingRules topic:
    • Set SHORTDESCRIPTION = Add spreadsheet calculation like CGI "$SUM( $ABOVE() )" to TWiki tables and other topic text

  • Debug plugin: (See output in CGI data/debug.txt)
    • Set DEBUG = 0

  • Do not handle CGI %CALC{}% variable in included topic while including topic: (default: 1)
    • Set SKIPINCLUDE = 1

  • WikiWords to exclude from being spaced out by the CGI CGI $PROPERSPACE(text) function. This comma delimited list can be overloaded by a DONTSPACE preferences variable: