none
[openafs-wiki.git] / TWiki / SpreadSheetPlugin.mdwn
1 # <a name="TWiki Spreadsheet Plugin"></a> TWiki Spreadsheet Plugin
2
3 This Plugin adds speadsheet capabilities to %WIKITOOLNAME% topics. Formulae like <code>**%CALC\{"$INT(7/3)"\}%**</code> 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.
4
5 Example:
6
7 <table border="1" cellpadding="0" cellspacing="0">
8   <tr>
9     <th bgcolor="#99CCCC"><strong> Region: </strong></th>
10     <th bgcolor="#99CCCC"><strong> Sales: </strong></th>
11   </tr>
12   <tr>
13     <td> Northeast </td>
14     <td align="right"> 320 </td>
15   </tr>
16   <tr>
17     <td> Northwest </td>
18     <td align="right"> 580 </td>
19   </tr>
20   <tr>
21     <td> South </td>
22     <td align="right"> 240 </td>
23   </tr>
24   <tr>
25     <td> Europe </td>
26     <td align="right"> 610 </td>
27   </tr>
28   <tr>
29     <td> Asia </td>
30     <td align="right"> 220 </td>
31   </tr>
32   <tr>
33     <td> Total: </td>
34     <td align="right"> 1970 </td>
35   </tr>
36 </table>
37
38 The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <br /> (you see the formula instead of the sum in case the Plugin is not installed or not enabled.)
39
40 ## <a name="Syntax Rules"></a> Syntax Rules
41
42 The action of this Plugin is triggered by the <code>**%CALC\{"..."\}%**</code> variable, which gets rendered according to the built-in function(s) found between the quotes.
43
44 - Built-in function are of format <code>**$FUNCNAME(parameter)**</code>
45 - Functions may be nested, e.g. <code>**%CALC\{"$SUM( R2:C$COLUMN(0)..R$ROW(-1):C$COLUMN(0) )"\}%**</code>
46 - Functions are evaluated from left to right, and from inside to outside if nested
47 - The function parameter can be text; a mathematical formula; a cell address; or a range of cell addresses
48 - Multiple parameters form a list; they are separated by a comma, followed by optional space, e.g. <code>**%CALC\{"$SUM( 3, 5, 7 )"\}%**</code>
49 - A table cell can be addressed as <code>**R1:C1**</code>. Table address matrix: <table border="1" cellpadding="0" cellspacing="0">
50   <tr>
51     <td><code><b>R1:C1</b></code></td>
52     <td><code><b>R1:C2</b></code></td>
53     <td><code><b>R1:C3</b></code></td>
54     <td><code><b>R1:C4</b></code></td>
55   </tr>
56   <tr>
57     <td><code><b>R2:C1</b></code></td>
58     <td><code><b>R2:C2</b></code></td>
59     <td><code><b>R2:C3</b></code></td>
60     <td><code><b>R2:C4</b></code></td>
61   </tr>
62 </table>
63 - A table cell range is defined by two cell addresses separated by <code>**".."**</code>, e.g. "row 1 through 20, column 3" is: <code>**R1:C3..R20:C3**</code>
64 - Lists can refer to values and/or table cell ranges, e.g. <code>**%CALC\{"$SUM( 3, 5, $T(R1:C7), R1:C11..R1:C15 )"\}%**</code>
65 - 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
66 - Formulae can also be placed outside of tables; they can reference cells in the preceeding table
67
68 ## <a name="Built-in Functions"></a> Built-in Functions
69
70 <table border="1" cellpadding="0" cellspacing="0">
71   <tr>
72     <th bgcolor="#99CCCC"><strong> Function </strong></th>
73     <th bgcolor="#99CCCC"><strong> Description </strong></th>
74   </tr>
75   <tr>
76     <td><code><b>"$ABOVE()"</b></code></td>
77     <td> The address range of cells above the current cell </td>
78   </tr>
79   <tr>
80     <td><code><b>"$AND(list)"</b></code></td>
81     <td> The logcial AND of a list. Example: <code><b>%CALC{"$AND(1, 0, 1)"}%</b></code> returns <code><b>0</b></code></td>
82   </tr>
83   <tr>
84     <td><code><b>"$AVERAGE(list)"</b></code></td>
85     <td> The average of a list or a range of cells. Example: To get the average of column 5 excluding the title row, write in the last row: <code><b>%CALC{"$AVERAGE( R2:C5..R$ROW(-1):C5 )"}%</b></code></td>
86   </tr>
87   <tr>
88     <td><code><b>"$CHAR(number)"</b></code></td>
89     <td> The ASCII character represented by number. Example: <code><b>%CALC{"$CHAR(97)"}%</b></code> returns <code><b>a</b></code></td>
90   </tr>
91   <tr>
92     <td><code><b>"$CODE(text)"</b></code></td>
93     <td> The ASCII numeric value of the first character in text. Example: <code><b>%CALC{"$CODE(abc)"}%</b></code> returns <code><b>97</b></code></td>
94   </tr>
95   <tr>
96     <td><code><b>"$COLUMN(offset)"</b></code></td>
97     <td> The current column number with an optional offset </td>
98   </tr>
99   <tr>
100     <td><code><b>"$COUNTITEMS(list)"</b></code></td>
101     <td> Counts individual items in a list. Example: <code><b>%CALC{"$COUNTITEMS( $ABOVE() )"}%</b></code> returns <code><b>Closed: 1, Open: 2</b></code> assuming one cell above the current cell contains <code><b>Closed</b></code> and two cells contain <code><b>Open</b></code></td>
102   </tr>
103   <tr>
104     <td><code><b>"$COUNTSTR(list, str)"</b></code></td>
105     <td> Counts 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. Example: To count the number of non empty cells above the current cell, write <code><b>%CALC{"$COUNTSTR( $ABOVE() )"}%</b></code>; to count the number of cells equal to <code><b>DONE</b></code>, write <code><b>%CALC{"$COUNTSTR( $ABOVE(), DONE )"}%</b></code>; </td>
106   </tr>
107   <tr>
108     <td><code><b>"$DEF(list)"</b></code></td>
109     <td> Returns the first list item or cell reference that is not empty. Example: <code><b>%CALC{"$DEF( R1:C1..R1:C3 )"}%</b></code></td>
110   </tr>
111   <tr>
112     <td><code><b>"$EVAL(formula)"</b></code></td>
113     <td> Evaluates a simple formula. Only addition, substraction, multiplication, division and modulus of numbers are supported. Any nesting is permitted. Example: <code><b>%CALC{"$EVAL( (5 * 3) / 2 + 1.1 )"}%</b></code> returns <code><b>8.6</b></code></td>
114   </tr>
115   <tr>
116     <td><code><b>"$EXACT(text1, text2)"</b></code></td>
117     <td> Compares two text strings and returns <code><b>1</b></code> if they are exactly the same, or <code><b>0</b></code> if not. Example: <code><b>%CALC{"$EXACT( foo, Foo )"}%</b></code> returns <code><b>0</b></code>; <code><b>%CALC{"$EXACT( foo, $LOWER(Foo) )"}%</b></code> returns <code><b>1</b></code></td>
118   </tr>
119   <tr>
120     <td><code><b>"$FIND(search_string, text, start_index)"</b></code></td>
121     <td> Finds one text string <code>search_string</code>, within another text string <code>text</code>, and returns the number of the starting position of <code>search_string</code>, from the first character of <code>text</code>. This search is case sensitive and is not a regular expression search; use <code>$SEARCH()</code> for regular expression searching. Starting position is 1; an empty string is returned if nothing is matched. Examples: %BR% <code><b>%CALC{"$FIND(f, fluffy)"}%</b></code> returns <code><b>1</b></code> %BR% <code><b>%CALC{"$FIND(f, fluffy, 2)"}%</b></code> returns <code><b>4</b></code> %BR% <code><b>%CALC{"$FIND(@, fluffy, 1)"}%</b></code> returns an empty string </td>
122   </tr>
123   <tr>
124     <td><code><b>"$FORMAT(type, prec, number)"</b></code></td>
125     <td> Formats a number to a certain type and precision. Types with examples:<br />  - <code><b>%CALC{"$FORMAT( COMMA, 2, 12345.6789 )"}%</b></code> returns <code><b>12,345.68</b></code><br />  - <code><b>%CALC{"$FORMAT( DOLLAR, 2, 12345.67 )"}%</b></code> returns <code><b>$12,345.68</b></code><br />  - <code><b>%CALC{"$FORMAT( NUMBER, 1, 12345.67 )"}%</b></code> returns <code><b>12345.7</b></code><br />  - <code><b>%CALC{"$FORMAT( PERCENT, 1, 0.1234567 )"}%</b></code> returns <code><b>12.3%</b></code></td>
126   </tr>
127   <tr>
128     <td><code><b>"$FORMATTIME(serial, text)"</b></code></td>
129     <td> Convert a serialized date into a date string; the following variables in text are expanded: <code>$second</code> (seconds, 00..59); <code>$minute</code> (minutes, 00..59); <code>$hour</code> (hours, 00..23); <code>$day</code> (day of month, 01..31); <code>$month</code> (month, 01..12); <code>$mon</code> (month in text format, Jan..Dec); <code>$year</code> (4 digit year, 1999); <code>$ye</code> (2 digit year, 99), <code>$wd</code> (day number of the week, 1 for Sunday, 2 for Monday, etc), <code>$wday</code> (day of the week, Sun..Sat), <code>$weekday</code> (day of the week, Sunday..Saturday), <code>$yearday</code> (day of the year, 1..365, or 1..366 in leap years). Date is assumed to be server time; add <code>GMT</code> to indicate Greenwich time zone. See also <code>$TIME()</code>, <code>$TODAY()</code>, <code>$FORMATGMTIME()</code>, <code>$TIMEDIFF()</code>. Example: <code><b>%CALC{"$FORMATTIME(0, $year/$month/$day GMT)"}%</b></code> returns <code><b>1970/01/01 GMT</b></code></td>
130   </tr>
131   <tr>
132     <td><code><b>"$FORMATGMTIME(serial, text)"</b></code></td>
133     <td> Convert a serialized date into a date string in Greenwich time zone. Same variables expansion as in <code>$FORMATTIME()</code>. Example: <code><b>%CALC{"$FORMATGMTIME(1041379200, $day $mon $year)"}%</b></code> returns <code><b>01 Jan 2003</b></code></td>
134   </tr>
135   <tr>
136     <td><code><b>"$GET(name)"</b></code></td>
137     <td> 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 <code>$SET()</code> 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. See also <code>$SET()</code>. Example: <code><b>%CALC{"$GET(my_total)"}%</b></code> returns the value of the <code>my_total</code> variable </td>
138   </tr>
139   <tr>
140     <td><code><b>"$IF(condition, value if true, value if 0)"</b></code></td>
141     <td> Returns one value if a condition is met, and another value if not. The condition can be a number (where <code><b>0</b></code> means condition not met), or two numbers with a comparison operator <code><b>&lt;</b></code> (less than), <code><b>&lt;=</b></code> (less than or equal), <code><b>==</b></code> (equal), <code><b>!=</b></code> (not equal), <code><b>&gt;=</b></code> (greater than or equal), <code><b>&gt;</b></code> (greater than). Examples:%BR% <code><b>%CALC{"$IF( $T(R1:C5) &gt; 1000, Over Budget, OK)"}%</b></code> returns <code><b>Over Budget</b></code> if value in R1:C5 is over 1000, <code><b>OK</b></code> if not%BR% <code><b>%CALC{"$IF( $EXACT($T(R1:C2),), empty, $T(R1:C2))"}%</b></code> returns the content of R1:C2 or <code><b>empty</b></code> if empty%BR% <code><b>%CALC{"$SET(val, $IF( $T(R1:C2) == 0, zero, $T(R1:C2)))"}%</b></code> sets a variable conditionally </td>
142   </tr>
143   <tr>
144     <td><code><b>"$INT(formula)"</b></code></td>
145     <td> Evaluates a simple formula and rounds the result down to the nearest integer. Example: <code><b>%CALC{"$INT(10 / 4)"}%</b></code> returns <code><b>2</b></code></td>
146   </tr>
147   <tr>
148     <td><code><b>"$LEFT()"</b></code></td>
149     <td> The address range of cells to the left of the current cell </td>
150   </tr>
151   <tr>
152     <td><code><b>"$LENGTH(text)"</b></code></td>
153     <td> The length in bytes of text. Example: <code><b>%CALC{"$LENGTH(abcd)"}%</b></code> returns <code><b>4</b></code></td>
154   </tr>
155   <tr>
156     <td><code><b>"$LIST(range)"</b></code></td>
157     <td> Converts the content of a range of cells into a flat list, delimited by comma. Cells containing commas are merged into the list. Example: <code><b>%CALC{"$LIST( $LEFT() )"}%</b></code> returns <code><b>Apples, Lemons, Oranges, Kiwis</b></code> assuming the cells to the left contain <code><b>| Apples | Lemons, Oranges | Kiwis |</b></code></td>
158   </tr>
159   <tr>
160     <td><code><b>"$LISTITEM(index, list)"</b></code></td>
161     <td> Get one item of a list. Index is 1 to size of list; use a negative number to count from the end of the list. Examples:%BR% <code><b>%CALC{"$LISTITEM(2, Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>Orange</b></code> %BR% <code><b>%CALC{"$LISTITEM(-1, Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>Kiwi</b></code></td>
162   </tr>
163   <tr>
164     <td><code><b>"$LISTMAP(formula, list)"</b></code></td>
165     <td> Evaluate and update each element of a list. In the formla, <code><b>$item</b></code> indicates the element, <code><b>$index</b></code> the index of the list starting at 1. Example: <code><b>%CALC{"$LISTMAP($index: $EVAL(2 * $item), 3, 5, 7, 11)"}%</b></code> returns <code><b>1: 6, 2: 10, 3: 14, 4: 22</b></code></td>
166   </tr>
167   <tr>
168     <td><code><b>"$LISTREVERSE(list)"</b></code></td>
169     <td> The opposite order of a list. Example: <code><b>%CALC{"$LISTREVERSE(Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>Kiwi, Apple, Orange, Apple</b></code></td>
170   </tr>
171   <tr>
172     <td><code><b>"$LISTSIZE(list)"</b></code></td>
173     <td> The number of elements in a list. Example: <code><b>%CALC{"$LISTSIZE(Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>4</b></code></td>
174   </tr>
175   <tr>
176     <td><code><b>"$LISTSORT(list)"</b></code></td>
177     <td> Sorts a list in ASCII order, or numerically if all elements are numeric. Example: <code><b>%CALC{"$LISTSORT(Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>Apple, Apple, Kiwi, Orange</b></code></td>
178   </tr>
179   <tr>
180     <td><code><b>"$LISTUNIQUE(list)"</b></code></td>
181     <td> Removes all duplicates from a list. Example: <code><b>%CALC{"$LISTUNIQUE(Apple, Orange, Apple, Kiwi)"}%</b></code> returns <code><b>Apple, Orange, Kiwi</b></code></td>
182   </tr>
183   <tr>
184     <td><code><b>"$LOWER(text)"</b></code></td>
185     <td> The lower case string of a text. Example: <code><b>%CALC{"$LOWER( $T(R1:C5) )"}%</b></code> returns the lower case string of the text in cell <code><b>R1:C5</b></code></td>
186   </tr>
187   <tr>
188     <td><code><b>"$MAX(list)"</b></code></td>
189     <td> The biggest value of a list or range of cells. Example: To find the biggest number to the left of the current cell, write: <code><b>%CALC{"$MAX( $LEFT() )"}%</b></code></td>
190   </tr>
191   <tr>
192     <td><code><b>"$MEDIAN(list)"</b></code></td>
193     <td> The median of a list or range of cells. Example: <code><b>%CALC{"$MEDIAN(3, 9, 4, 5)"}%</b></code> returns <code><b>4.5</b></code></td>
194   </tr>
195   <tr>
196     <td><code><b>"$MIN(list)"</b></code></td>
197     <td> The smallest value of a list or range of cells. Example: <code><b>%CALC{"$MIN(15, 3, 28)"}%</b></code> returns <code><b>3</b></code></td>
198   </tr>
199   <tr>
200     <td><code><b>"$MOD(num, divisor)"</b></code></td>
201     <td> The reminder after dividing <code><b>num</b></code> by <code><b>divisor</b></code>. Example: <code><b>%CALC{"$MOD(7, 3)"}%</b></code> returns <code><b>1</b></code></td>
202   </tr>
203   <tr>
204     <td><code><b>"$NOP(text)"</b></code></td>
205     <td> A no-operation. Allows one to defy the order of Plugin execution. For example, it will allow preprossing to be done before <code>%SEARCH{}%</code> is evaluated. Use <code>$per</code> to escape '%'. </td>
206   </tr>
207   <tr>
208     <td><code><b>"$NOT(num)"</b></code></td>
209     <td> The reverse logic of a number. Returns 0 if <code><b>num</b></code> is not zero, 1 if zero. Example: <code><b>%CALC{"$NOT(0)"}%</b></code> returns <code><b>1</b></code></td>
210   </tr>
211   <tr>
212     <td><code><b>"$OR(list)"</b></code></td>
213     <td> The logcial OR of a list. Example: <code><b>%CALC{"$OR(1, 0, 1)"}%</b></code> returns <code><b>1</b></code></td>
214   </tr>
215   <tr>
216     <td><code><b>"$PRODUCT(list)"</b></code></td>
217     <td> The product of a list or range of cells. Example: to calculate the product of the cells to the left of the current one use <code><b>%CALC{"$PRODUCT($LEFT())"}%</b></code></td>
218   </tr>
219   <tr>
220     <td><code><b>"$PROPER(text)"</b></code></td>
221     <td> Capitalizes letters in <code><b>text</b></code> that follow any character other than a letter. Converts all other letters to lowercase letters. Examples:%BR% <code><b>%CALC{"PROPER(a small STEP)"}%</b></code> returns <code><b>A Small Step</b></code> %BR% <code><b>%CALC{"PROPER(f1 (formula-1))"}%</b></code> returns <code><b>F1 (Formula 1)</b></code></td>
222   </tr>
223   <tr>
224     <td><code><b>"$PROPERSPACE(text)"</b></code></td>
225     <td> Properly spaces out [[TWiki/WikiWords]] preceeded by white space, parenthesis, or <code><b>][</b></code>. Words listed in the DONTSPACE [[TWiki/TWikiPreferences]] variable or DONTSPACE Plugins setting are excluded. Example, assuming DONTSPACE contains McIntosh: <code><b>%CALC{"PROPERSPACE(Old MacDonald had a ServerFarm, EeEyeEeEyeOh"}%</b></code> returns <code><b>Old MacDonald had a Server Farm, Ee Eye Ee Eye Oh</b></code></td>
226   </tr>
227   <tr>
228     <td><code><b>"$RAND(max)"</b></code></td>
229     <td> Random number, evenly distributed between 0 and <code><b>max</b></code>, or 0 and 1 if max is not specified. </td>
230   </tr>
231   <tr>
232     <td><code><b>"$REPEAT(text)"</b></code></td>
233     <td> Repeat <code><b>text</b></code> a number of times. Example: <code><b>%CALC{"$REPEAT(/\, 5)"}%</b></code> returns <code><b>/\/\/\/\/\</b></code></td>
234   </tr>
235   <tr>
236     <td><code><b>"$REPLACE(text, start_num, num_chars, new_text)"</b></code></td>
237     <td> Replaces part of text string <code>text</code>, based on the starting position <code>start_num</code>, and the number of characters to replace <code>num_chars</code>. The characters are replaced with <code>new_text</code>. Starting position is 1; use a negative <code>start_num</code> to count from the end of the text. See also <code>$SUBSTITUTE()</code>, <code>$TRANSLATE()</code>. Example: %BR% <code><b>%CALC{"$REPLACE(abcdefghijk,6,5,*)"}%</b></code> returns <code><b>abcde*k</b></code></td>
238   </tr>
239   <tr>
240     <td><code><b>"$RIGHT()"</b></code></td>
241     <td> The address range of cells to the right of the current cell </td>
242   </tr>
243   <tr>
244     <td><code><b>"$ROUND(formula, digits)"</b></code></td>
245     <td> Evaluates a simple <code><b>formula</b></code> and rounds the result up or down to the number of digits if <code><b>digits</b></code> is positive; to the nearest integer if digits is missing; or to the left of the decimal point if digits is negative. Examples: %BR% <code><b>%CALC{"$ROUND(3.15, 1)"}%</b></code> returns <code><b>3.2</b></code> %BR% <code><b>%CALC{"$ROUND(3.149, 1)"}%</b></code> returns <code><b>3.1</b></code> %BR% <code><b>%CALC{"$ROUND(-2.475, 2)"}%</b></code> returns <code><b>-2.48</b></code> %BR% <code><b>%CALC{"$ROUND(34.9, -1)"}%</b></code> returns <code><b>30</b></code></td>
246   </tr>
247   <tr>
248     <td><code><b>"$ROW(offset)"</b></code></td>
249     <td> The current row number with an offset. Example: To get the number of rows excluding table heading ( first row) and summary row (last row you are in), write: <code><b>%CALC{"$ROW(-2)"}%</b></code></td>
250   </tr>
251   <tr>
252     <td><code><b>"$SEARCH(search_string, text, start_index)"</b></code></td>
253     <td> Finds one text string <code>search_string</code>, within another text string <code>text</code>, and returns the number of the starting position of <code>search_string</code>, from the first character of <code>text</code>. This search a [[TWiki/RegularExpression]] search; use <code>$FIND()</code> for non-regular expression searching. Starting position is 1; an empty string is returned if nothing is matched. Examples: %BR% <code><b>%CALC{"$SEARCH([uy], fluffy)"}%</b></code> returns <code><b>3</b></code> %BR% <code><b>%CALC{"$SEARCH([uy], fluffy, 3)"}%</b></code> returns <code><b>6</b></code> %BR% <code><b>%CALC{"$SEARCH([abc], fluffy,)"}%</b></code> returns an empty string </td>
254   </tr>
255   <tr>
256     <td><code><b>"$SET(name, value)"</b></code></td>
257     <td> Set a variable for later use. Specify the variable name (alphanumeric characters and underscores) and the value. The value may contain a formula; formulas are evaluated before the variable assignment. This function returns no output. Use <code>$GET()</code> 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. See also <code>$GET()</code>. Example: <code><b>%CALC{"$SET( my_total, $SUM($ABOVE()) )"}%</b></code> sets the <code>my_total</code> variable to the sum of all table cells located above the current cell and returns an empty string </td>
258   </tr>
259   <tr>
260     <td><code><b>"$SETM(name, formula)"</b></code></td>
261     <td> Updates 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 <code><b>+</b></code> (add), <code><b>-</b></code> (subtract), <code><b>*</b></code> (multiply), or <code><b>/</b></code> (divide) something to the variable. This function returns no output. Use <code>$GET()</code> to retrieve variables. Example: <code><b>%CALC{"$SETM( total, + $SUM($LEFT()) )"}%</b></code> adds the sum of all table cells on the left to the <code>total</code> variable, and returns an empty string </td>
262   </tr>
263   <tr>
264     <td><code><b>"$SIGN(num)"</b></code></td>
265     <td> The sign of a number. Returns -1 if <code><b>num</b></code> is negative, 0 if zero, or 1 if positive. Example: <code><b>%CALC{"$SIGN(-12.5)"}%</b></code> returns <code><b>-1</b></code></td>
266   </tr>
267   <tr>
268     <td><code><b>"$SUBSTITUTE(text, old, new, instance, option)"</b></code></td>
269     <td> Substitutes <code>new</code> text for <code>old</code> text in a <code>text</code> string. <code>instance</code> specifies which occurance of <code>old</code> you want to replace. If you specify <code>instance</code>, only that instance is replaced. Otherwise, every occurance is changed to the new text. A literal search is performed by default; a [[TWiki/RegularExpression]] search if the <code>option</code> is set to <code><b>r</b></code>. See also <code>$REPLACE()</code>, <code>$TRANSLATE()</code>. Examples: %BR% <code><b>%CALC{"$SUBSTITUTE(Good morning, morning, day)"}%</b></code> returns <code><b>Good day</b></code> %BR% <code><b>%CALC{"$SUBSTITUTE(Q2-2002,2,3)"}%</b></code> returns <code><b>Q3-3003</b></code> %BR% <code><b>%CALC{"$SUBSTITUTE(Q2-2002,2,3,3)"}%</b></code> returns <code><b>Q2-2003</b></code> %BR% <code><b>%CALC{"$SUBSTITUTE(abc123def,[0-9],9,,r)"}%</b></code> returns <code><b>abc999def</b></code></td>
270   </tr>
271   <tr>
272     <td><code><b>"$SUM(list)"</b></code></td>
273     <td> The sum of a list or range of cells. Example: To sum up column 5 excluding the title row, write: <code><b>%CALC{"$SUM( R2:C5..R$ROW(-1):C5 )"}%</b></code> in the last row; or simply <code><b>%CALC{"$SUM( $ABOVE() )"}%</b></code></td>
274   </tr>
275   <tr>
276     <td><code><b>"$SUMDAYS(list)"</b></code></td>
277     <td> 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 <code><b>h</b></code>, <code><b>hours</b></code>, <code><b>d</b></code>, <code><b>days</b></code>, <code><b>w</b></code>, <code><b>weeks</b></code> suffix. One week is assumed to have 5 working days, one day 8 hours. Example: <code><b>%CALC{"$SUMDAYS( 2w, 1, 2d, 4h )"}%</b></code> returns <code><b>13.5</b></code>, the evaluation of <code>(2*5 + 1 + 2 + 4/8)</code></td>
278   </tr>
279   <tr>
280     <td><code><b>"$SUMPRODUCT(list, list, ...)"</b></code></td>
281     <td> The scalar product on ranges of cells. Example: <code><b>%CALC{"$SUM( R2:C1..R4:C1, R2:C5..R4:C5 )"}%</b></code> evaluates and returns <code><b>($T(R2:C1) * $T(R2:C5) + $T(R3:C1) * $T(R3:C5) + $T(R4:C1) * $T(R4:C5))</b></code></td>
282   </tr>
283   <tr>
284     <td><code><b>"$T(address)"</b></code></td>
285     <td> The content of a cell. Example: <code><b>%CALC{"$T(R1:C5)"}%</b></code> returns the text in cell <code><b>R1:C5</b></code></td>
286   </tr>
287   <tr>
288     <td><code><b>"$TRANSLATE(text, from, to)"</b></code></td>
289     <td> Translate text from a set of characters to another set, one character by one. The <code>text</code> may contain commas; the last two comma separated parameters are required. For <code>from</code> and <code>to</code> parameters, you can write <code>$comma</code> to escape comma, <code>$sp</code> to escape space. See also <code>$REPLACE()</code>, <code>$SUBSTITUTE()</code>. Examples: %BR% <code><b>%CALC{"$TRANSLATE(boom,bm,cl)"}%</b></code> returns <code><b>cool</b></code> %BR% <code><b>%CALC{"$TRANSLATE(one, two,$comma,;)"}%</b></code> returns <code><b>one; two</b></code></td>
290   </tr>
291   <tr>
292     <td><code><b>"$TIME(text)"</b></code></td>
293     <td> Converts a date string into a serialized date number (seconds since the Epoch, e.g. midnight, 01 Jan 1970). Current time is taken if the date string is empty. Supported date formats: <code>31 Dec 2009</code>; <code>31 Dec 2009 GMT</code>; <code>31 Dec 09</code>; <code>31-Dec-2009</code>; <code>31/Dec/2009</code>; <code>2009/12/31</code>; <code>2009-12-31</code>; <code>2009/12/31</code>; <code>2009/12/31 23:59</code>; <code>2009/12/31 - 23:59</code>; <code>2009-12-31-23-59</code>; <code>2009/12/31 - 23:59:59</code>; <code>2009.12.31.23.59.59</code>. Date is assumed to be server time; add <code>GMT</code> to indicate Greenwich time zone. See also <code>$TODAY()</code>, <code>$FORMATTIME()</code>, <code>$FORMATGMTIME()</code>, <code>$TIMEDIFF()</code>. Example: <code><b>%CALC{"$TIME( 2003/10/14 GMT )"}%</b></code> returns <code><b>1066089600</b></code></td>
294   </tr>
295   <tr>
296     <td><code><b>"$TIMEADD( serial, value, unit )"</b></code></td>
297     <td> Add a value to a serialized date. The unit is seconds if not specified; unit can be <code>second</code>, <code>minute</code>, <code>hour</code>, <code>day</code>, <code>week</code>, <code>month</code>, <code>year</code>. Note: An approximation is used for month and year calculations. See also <code>$TIME()</code>, <code>$TIMEDIFF()</code>, <code>$TODAY()</code>, <code>$FORMATTIME()</code>. Example: <code><b>%CALC{"$TIMEADD( $TIME(), 2, week )"}%</b></code> returns the serialized date two weeks from now </td>
298   </tr>
299   <tr>
300     <td><code><b>"$TIMEDIFF( serial_1, serial_2, unit )"</b></code></td>
301     <td> Time difference between two serialized dates. The unit is seconds if not specified; unit can be specified as in <code>$TIMEADD()</code>. Note: An approximation is used for month and year calculations. Use <code>$FORMAT()</code> or <code>$INT()</code> to format real numbers. See also <code>$TIME()</code>, <code>$TIMEADD()</code>, <code>$TODAY()</code>, <code>$FORMATTIME()</code>. Example: <code><b>%CALC{"$TIMEDIFF( $TIME(), $EVAL($TIME()+90), minute )"}%</b></code> returns <code><b>1.5</b></code></td>
302   </tr>
303   <tr>
304     <td><code><b>"$TODAY()"</b></code></td>
305     <td> Get the serialized date of today at midnight GMT. The related <code>$TIME()</code> returns the serialized date of today at the current time, e.g. it includes the number of seconds since midnight GMT. See also <code>$FORMATTIME()</code>, <code>$FORMATGMTIME()</code>, <code>$TIMEDIFF()</code>. Example: <code><b>%CALC{"$TODAY()"}%</b></code> returns the number of seconds since Epoch </td>
306   </tr>
307   <tr>
308     <td><code><b>"$TRIM(text)"</b></code></td>
309     <td> Removes all spaces from text except for single spaces between words. Example: <code><b>%CALC{"$TRIM( eat  spaces  )"}%</b></code> returns <code><b>eat spaces</b></code>. </td>
310   </tr>
311   <tr>
312     <td><code><b>"$UPPER(text)"</b></code></td>
313     <td> The upper case string of a text. Example: <code><b>%CALC{"$UPPER( $T(R1:C5) )"}%</b></code> returns the upper case string of the text in cell <code><b>R1:C5</b></code></td>
314   </tr>
315   <tr>
316     <td><code><b>"$VALUE(text)"</b></code></td>
317     <td> Extracts a number from <code><b>text</b></code>. Returns <code><b>0</b></code> if not found. Examples:%BR% <code><b>%CALC{"$VALUE(US$1,200)"}%</b></code> returns <code><b>1200</b></code> %BR% <code><b>%CALC{"$VALUE(PrjNotebook1234)"}%</b></code> returns <code><b>1234</b></code> %BR% <code><b>%CALC{"$VALUE(Total: -12.5)"}%</b></code> returns <code><b>-12.5</b></code></td>
318   </tr>
319 </table>
320
321 ## <a name="Bug Tracking Example"></a> Bug Tracking Example
322
323 <table border="1" cellpadding="0" cellspacing="0">
324   <tr>
325     <th bgcolor="#99CCCC"><strong> Bug#: </strong></th>
326     <th bgcolor="#99CCCC"><strong> Priority: </strong></th>
327     <th bgcolor="#99CCCC"><strong> Subject: </strong></th>
328     <th bgcolor="#99CCCC"><strong> Status: </strong></th>
329     <th bgcolor="#99CCCC"><strong> Days to fix </strong></th>
330   </tr>
331   <tr>
332     <td> Bug:1231 </td>
333     <td> Low </td>
334     <td> File Open ... </td>
335     <td> Open </td>
336     <td align="right"> 3 </td>
337   </tr>
338   <tr>
339     <td> Bug:1232 </td>
340     <td> High </td>
341     <td> Memory Window ... </td>
342     <td> Fixed </td>
343     <td align="right"> 2 </td>
344   </tr>
345   <tr>
346     <td> Bug:1233 </td>
347     <td> Medium </td>
348     <td> Usability issue ... </td>
349     <td> Assigned </td>
350     <td align="right"> 5 </td>
351   </tr>
352   <tr>
353     <td> Bug:1234 </td>
354     <td> High </td>
355     <td> No arrange ... </td>
356     <td> Fixed </td>
357     <td align="right"> 1 </td>
358   </tr>
359   <tr>
360     <td> Total: 4 </td>
361     <td> High: 2<br /> Low: 1<br /> Medium: 1 </td>
362     <td> . </td>
363     <td> Assigned: 1<br /> Fixed: 2<br /> Open: 1 </td>
364     <td align="right"> Total: 11 </td>
365   </tr>
366 </table>
367
368 The last row is defined as:
369
370     | Total: %CALC{"$ROW(-2)"}% \
371       | %CALC{"$COUNTITEMS( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% | . \
372       | %CALC{"$COUNTITEMS( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% \
373       |  Total: %CALC{"$SUM( R2:C$COLUMN()..R$ROW(-1):C$COLUMN() )"}% |
374
375 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.
376
377 ## <a name="Plugin Settings"></a> Plugin Settings
378
379 Plugin settings are stored as preferences variables. To reference a plugin setting write <code>**%&lt;plugin&gt;\_&lt;setting&gt;%**</code>, i.e. <code>**%SPREADSHEETPLUGIN\_SHORTDESCRIPTION%**</code>
380
381 - One line description, is shown in the [[TextFormattingRules]] topic:
382   - Set SHORTDESCRIPTION = Add spreadsheet calculation like `"$SUM( $ABOVE() )"` to tables located in %WIKITOOLNAME% topics.
383
384 - Debug plugin: (See output in `data/debug.txt`)
385   - Set DEBUG = 0
386
387 - Do not handle `%CALC{}%` tag in included topic while including topic: (default: 1)
388   - Set SKIPINCLUDE = 1
389
390 - [[WikiWords|TWiki/WikiWord]] to exclude from being spaced out by the <code>**$PROPERSPACE(text)**</code> function. This comma delimited list can be overloaded by a DONTSPACE preferences variable:
391   - Set DONTSPACE = CodeWarrior, MacDonald, McIntosh, RedHat, SuSE
392
393 ## <a name="Plugin Installation Instructions"></a> Plugin Installation Instructions
394
395 **_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.
396
397 - Download the ZIP file from the %TOPIC% home
398 - Unzip <code>**SpreadSheetPlugin.zip**</code> in your twiki installation directory. Content: <table border="1" cellpadding="0" cellspacing="0">
399   <tr>
400     <th bgcolor="#99CCCC"><strong> File: </strong></th>
401     <th bgcolor="#99CCCC"><strong> Description: </strong></th>
402   </tr>
403   <tr>
404     <td><code><b>data/TWiki/%TOPIC%.txt</b></code></td>
405     <td> Plugin topic </td>
406   </tr>
407   <tr>
408     <td><code><b>data/TWiki/%TOPIC%.txt,v</b></code></td>
409     <td> Plugin topic repository </td>
410   </tr>
411   <tr>
412     <td><code><b>lib/TWiki/Plugins/%TOPIC%.pm</b></code></td>
413     <td> Plugin Perl module </td>
414   </tr>
415 </table>
416 - Test if the "Total" in the first table in this topic is correct.
417
418 ## <a name="Plugin Info"></a> Plugin Info
419
420 <table border="1" cellpadding="0" cellspacing="0">
421   <tr>
422     <td align="right"> Plugin Author: </td>
423     <td> TWiki:Main/PeterThoeny </td>
424   </tr>
425   <tr>
426     <td align="right"> Plugin Version: </td>
427     <td> 21 Mar 2004 </td>
428   </tr>
429   <tr>
430     <td align="right"> Change History: </td>
431     <td>  </td>
432   </tr>
433   <tr>
434     <td align="right"> 21 Mar 2004: </td>
435     <td> Added $LISTINDEX(); fixed call to inofficial function </td>
436   </tr>
437   <tr>
438     <td align="right"> 16 Mar 2004: </td>
439     <td> 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 </td>
440   </tr>
441   <tr>
442     <td align="right"> 08 Mar 2004: </td>
443     <td> Added $LIST() </td>
444   </tr>
445   <tr>
446     <td align="right"> 06 Mar 2004: </td>
447     <td> 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 </td>
448   </tr>
449   <tr>
450     <td align="right"> 27 Feb 2004: </td>
451     <td> Added $COUNTUNIQUE() </td>
452   </tr>
453   <tr>
454     <td align="right"> 24 Oct 2003: </td>
455     <td> Added $SET(), $GET(), $MEDIAN(); added $SUMPRODUCT(), inspired by TWiki:Main/RobertWithrow; added $SUMDAYS(), contributed by TWiki:Main/SvenDowideit </td>
456   </tr>
457   <tr>
458     <td align="right"> 21 Oct 2003: </td>
459     <td> Added support for lists <code>(1, 2, 3)</code> and lists of table ranges <code>(R1:C1..R1:C5, R3:C1..R3:C5)</code> 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 </td>
460   </tr>
461   <tr>
462     <td align="right"> 14 Oct 2003: </td>
463     <td> Added $TIME(), $TODAY(), $FORMATTIME(), $FORMATGMTIME(), $TIMEDIFF() </td>
464   </tr>
465   <tr>
466     <td align="right"> 13 Oct 2003: </td>
467     <td> Added $MULT(), contributed by TWiki:Main/GerritJanBaarda </td>
468   </tr>
469   <tr>
470     <td align="right"> 30 Jul 2003: </td>
471     <td> Added $TRANSLATE() </td>
472   </tr>
473   <tr>
474     <td align="right"> 19 Jul 2003: </td>
475     <td> Added $FIND(), $NOP(), $REPLACE(), $SEARCH(), $SUBSTITUTE(), contributed by TWiki:Main/PaulineCheung </td>
476   </tr>
477   <tr>
478     <td align="right"> 19 Apr 2003: </td>
479     <td> Added $COUNTSTR(), $EXACT(), $IF(), $ROUND(), $TRIM(); added $FORMAT(), contributed by TWiki:Main/JimStraus; support <code>%</code> modulus operator in $EVAL, $INT, and $ROUND; fixed bug in $DEF </td>
480   </tr>
481   <tr>
482     <td align="right"> 07 Jun 2002: </td>
483     <td> Added $DEF(), contributed by TWiki:Main/MartinFuzzey; allow values with HTML formatting like <code>&lt;u&gt;102&lt;/u&gt;</code>, suggested by TWiki:Main/GladeDiviney; added SKIPINCLUDE setting </td>
484   </tr>
485   <tr>
486     <td align="right"> 12 Mar 2002: </td>
487     <td> Support for multiple functions per nesting level </td>
488   </tr>
489   <tr>
490     <td align="right"> 15 Jan 2002: </td>
491     <td> Added $CHAR(), $CODE() and $LENGTH() </td>
492   </tr>
493   <tr>
494     <td align="right"> 12 Nov 2001: </td>
495     <td> Added $RIGHT() </td>
496   </tr>
497   <tr>
498     <td align="right"> 12 Aug 2001: </td>
499     <td> Fixed bug of disappearing multi-column cells </td>
500   </tr>
501   <tr>
502     <td align="right"> 19 Jul 2001: </td>
503     <td> Fixed incorrect $SUM calculation of cell with value <code>0</code></td>
504   </tr>
505   <tr>
506     <td align="right"> 14 Jul 2001: </td>
507     <td> Changed to plug &amp; play </td>
508   </tr>
509   <tr>
510     <td align="right"> 01 Jun 2001: </td>
511     <td> Fixed insecure dependencies for $MIN and $MAX </td>
512   </tr>
513   <tr>
514     <td align="right"> 16 Apr 2001: </td>
515     <td> Fixed div by 0 bug in $AVERAGE </td>
516   </tr>
517   <tr>
518     <td align="right"> 17 Mar 2001: </td>
519     <td> Initial version </td>
520   </tr>
521   <tr>
522     <td align="right"> CPAN Dependencies: </td>
523     <td> none </td>
524   </tr>
525   <tr>
526     <td align="right"> TWiki:Plugins/Benchmark: </td>
527     <td>[[TWiki/GoodStyle]] 99%, [[TWiki/FormattedSearch]] 99%, %TOPIC% 95% </td>
528   </tr>
529   <tr>
530     <td align="right"> Other Dependencies: </td>
531     <td> none </td>
532   </tr>
533   <tr>
534     <td align="right"> Perl Version: </td>
535     <td> 5.000 and up </td>
536   </tr>
537   <tr>
538     <td align="right"> Plugin Home: </td>
539     <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%</a></td>
540   </tr>
541   <tr>
542     <td align="right"> Feedback: </td>
543     <td><a href="http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev" target="_top">http://TWiki.org/cgi-bin/view/Plugins/%TOPIC%Dev</a></td>
544   </tr>
545 </table>
546
547 **_Related Topics:_** [[TWikiPreferences]], [[TWikiPlugins]]
548
549 -- TWiki:Main/PeterThoeny - 21 Mar 2004