none
[openafs-wiki.git] / TWiki / SpreadSheetPlugin.mdwn
index 2ee232a..90e1ade 100644 (file)
@@ -1,6 +1,6 @@
 # <a name="TWiki Spreadsheet Plugin"></a> TWiki Spreadsheet Plugin
 
-This Plugin adds speadsheet capabilities to %WIKITOOLNAME% topics. Formulas like <code>**%CALC\{"$INT(7/3)"\}%**</code> are evaluated at page view time. They can be placed in table cells and outside of tables.
+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.
 
 Example:
 
@@ -39,8 +39,9 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
 
 ## <a name="Syntax Rules"></a> Syntax Rules
 
-- The formula in the <code>**%CALC\{"formula"\}%**</code> variable can contain built-in functions
-- Built-in function are of format <code>**$FUNCNAME(parameter)%**</code>
+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.
+
+- Built-in function are of format <code>**$FUNCNAME(parameter)**</code>
 - Functions may be nested, e.g. <code>**%CALC\{"$SUM( R2:C$COLUMN(0)..R$ROW(-1):C$COLUMN(0) )"\}%**</code>
 - 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
@@ -104,10 +105,6 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
     <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>
   </tr>
   <tr>
-    <td><code><b>"$COUNTUNIQUE(list)"</b></code></td>
-    <td> Counts unique items in a list, separated by comma and/or space. Example: <code><b>%CALC{"$COUNTUNIQUE( $ABOVE() )"}%</b></code> returns <code><b>Alice: 2, Mike: 1, Tom: 2</b></code> assuming the cells above the current cell contain <code><b>Alice</b></code> and <code><b>Tom, Mike</b></code> and <code><b>Alice, Tom</b></code></td>
-  </tr>
-  <tr>
     <td><code><b>"$DEF(list)"</b></code></td>
     <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>
   </tr>
@@ -128,24 +125,24 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
     <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>
   </tr>
   <tr>
-    <td><code><b>"$FORMATTIME( serial, text )"</b></code></td>
-    <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>
+    <td><code><b>"$FORMATTIME(serial, text)"</b></code></td>
+    <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>
   </tr>
   <tr>
-    <td><code><b>"$FORMATGMTIME( serial, text )"</b></code></td>
-    <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>
+    <td><code><b>"$FORMATGMTIME(serial, text)"</b></code></td>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$GET(name)"</b></code></td>
-    <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>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$IF(condition, value if true, value if 0)"</b></code></td>
-    <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 (notice that you cannot set a variable conditionally inside an $IF() since formulae in the "value if true" and "value if 0" are both evaluated before the condition is applied) </td>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$INT(formula)"</b></code></td>
-    <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>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$LEFT()"</b></code></td>
@@ -156,6 +153,34 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
     <td> The length in bytes of text. Example: <code><b>%CALC{"$LENGTH(abcd)"}%</b></code> returns <code><b>4</b></code></td>
   </tr>
   <tr>
+    <td><code><b>"$LIST(range)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTITEM(index, list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTMAP(formula, list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTREVERSE(list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTSIZE(list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTSORT(list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
+    <td><code><b>"$LISTUNIQUE(list)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
     <td><code><b>"$LOWER(text)"</b></code></td>
     <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>
   </tr>
@@ -197,7 +222,7 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
   </tr>
   <tr>
     <td><code><b>"$PROPERSPACE(text)"</b></code></td>
-    <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(McIntosh likes WikiWord links like WebHome and [[WebHome][WebHome]])"}%</b></code> returns <code><b>McIntosh likes Wiki Word links like Web Home and [[Main/WebHome]]</b></code></td>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$RAND(max)"</b></code></td>
@@ -205,7 +230,7 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
   </tr>
   <tr>
     <td><code><b>"$REPEAT(text)"</b></code></td>
-    <td> Repeat <code><b>text</b></code> a number of times. Example: <code><b>%CALC{"$REPEAT(Hi! , 3)"}%</b></code> returns <code><b>Hi! Hi! Hi!</b></code></td>
+    <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>
   </tr>
   <tr>
     <td><code><b>"$REPLACE(text, start_num, num_chars, new_text)"</b></code></td>
@@ -232,6 +257,10 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
     <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>
   </tr>
   <tr>
+    <td><code><b>"$SETM(name, formula)"</b></code></td>
+    <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>
+  </tr>
+  <tr>
     <td><code><b>"$SIGN(num)"</b></code></td>
     <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>
   </tr>
@@ -285,7 +314,7 @@ The formula next to "Total" is <code>**%CALC\{"$SUM( $ABOVE() )"\}%**</code>. <b
   </tr>
   <tr>
     <td><code><b>"$VALUE(text)"</b></code></td>
-    <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>
+    <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>
   </tr>
 </table>
 
@@ -359,7 +388,7 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
   - Set SKIPINCLUDE = 1
 
 - [[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:
-  - Set DONTSPACE = CodeWarrior, McIntosh, RedHat, SuSE
+  - Set DONTSPACE = CodeWarrior, MacDonald, McIntosh, RedHat, SuSE
 
 ## <a name="Plugin Installation Instructions"></a> Plugin Installation Instructions
 
@@ -395,13 +424,25 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
   </tr>
   <tr>
     <td align="right"> Plugin Version: </td>
-    <td> 06 Mar 2004 </td>
+    <td> 21 Mar 2004 </td>
   </tr>
   <tr>
     <td align="right"> Change History: </td>
     <td>  </td>
   </tr>
   <tr>
+    <td align="right"> 21 Mar 2004: </td>
+    <td> Added $LISTINDEX(); fixed call to inofficial function </td>
+  </tr>
+  <tr>
+    <td align="right"> 16 Mar 2004: </td>
+    <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>
+  </tr>
+  <tr>
+    <td align="right"> 08 Mar 2004: </td>
+    <td> Added $LIST() </td>
+  </tr>
+  <tr>
     <td align="right"> 06 Mar 2004: </td>
     <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>
   </tr>
@@ -505,4 +546,4 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
 
 **_Related Topics:_** [[TWikiPreferences]], [[TWikiPlugins]]
 
--- TWiki:Main/PeterThoeny - 06 Mar 2004
+-- TWiki:Main/PeterThoeny - 21 Mar 2004