PHP and Javascript implementations of a simple markdown parser
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

spreadsheet.md 7.1KB

Spreadsheet Expressions

Spreadsheet expressions are an optional feature not enabled in the default parser configurations. To enable it, include spreadsheet.js / spreadsheet.php and create a Markdown instance with a new MDSpreadsheetReader() included in its reader array.

Expressions

As with many spreadsheet applications, a cell that begins with = is treated as a formula.

Cell References

Values in other cells can be referenced with addresses. It consists of a column letter and an optional row number. When the row number is omitted the row is assumed to be the same as where the expression is located.

Only body rows of a table are included in calculations and can be referenced. Header rows are entirely ignored.

| Address | Description | | -- | -- | | =A1 | Value from the leftmost column on the first row | | =C2 | Value from the third column from the left on the second row | | =A | Value from the leftmost column on the same row as the expression | | =AB1 | Value from the 28th column from the left. Letters proceed A to Z for columns 1 to 26, then AA, AB, AC… for 27, 28, 29…. Limited to 2 letters (676 columns), which is way more than could ever reasonably fit in a markdown table. |

Rows or column references can be fixed for autofilled formulas (discussed below) by using a $ before the column letters or row number.

| Address | Description | | -- | -- | | =$A1 | First column and row. If transposed, the row may change but the column will not. | | =A$1 | First column and row. If transposed, the column may change but the row will not. | | =$A$1 | First column and row even if transposed. |

Cell Ranges

Some functions accept a range of values which can be referenced with an address range. This consists of two addresses joined with a colon (:) and will include all the cells in the rectangle between them.

Examples.

| Range | Description | | -- | -- | | A1:C9 | All the cells in columns A, B, and C from rows 1 through 9, inclusive. | | A:C | All the cells in columns A, B, and C from all rows. |

Cell ranges can be used in functions like SUM, AVERAGE, MAX, and others that operate on arbitrary numbers of values.

If the cell range exceeds the number of rows or columns in a table, the out-of-bounds addresses will be skipped.

Omitting the row number is a low maintenance way of referring to all the cells in a column without having to update the lower bound as rows are added.

If a cell has a formula with a range that includes its own cell, the interpeter will automatically exclude it. E.g. this is valid:

| Name  | Qty       |
| ----- | --------- |
| A     | 2         |
| B     | 3         |
| C     | 1         |
| TOTAL | =SUM(B:B) |

The sum references column B and is also in column B but will not cause a circular reference. It will produce a result of 6.

Operators

| Operator | Description | | -- | -- | | x + y | Addition | | x - y | Subtraction | | x * y | Multiplication | | x / y | Division | | -x | Unary minus. Negates an argument. | | x & y | Concatenate. Produces a text value. | | x < y | Less than. Produces a Boolean value. | | x <= y | Less than or equal. Produces a Boolean value. | | x > y | Greater than. Produces a Boolean value. | | x >= y | Greater than or equal. Produces a Boolean value. | | x == y | Equal. Produces a Boolean value. | | x != y | Not equal. Produces a Boolean value. | | !x | Logical not. Produces a Boolean value. |

Parentheses

Calculations can be grouped in parentheses to affect evaluation order.

| Example | Evaluation order | | -- | -- | | =3*4+1*2 | 1. =12+1*2 (multiply 3 and 4)
2. =12+2 (multiply 1 and 2)
3. =14 (add 12 and 2) | | =3*(4+1)*2 | 1. =3*5*2 (add 4 and 1)
2. =15*2 (multiply 3 and 5)
3. =30 (multiply 15 and 2) |

Functions

| Function | Description | | -- | -- | | ABS(x) | Absolute value | | AND(x, y,, z) | Boolean AND of 1 or more arguments | | AVERAGE(x, y,, z) | Statistical mean of 1 or more arguments. Non-numeric values are ignored. | | CEILING(x) | Rounds a value up | | EXP(x) | Raises e to the power of x | | FLOOR(x) | Rounds a value down | | IF(test, trueval, falseval) | Returns trueval if test evaluates to TRUE, otherwise returns falseval. | IFS(test1, val1, test2, val2,, fallbackval) | Performs multiple if tests. If test1 is TRUE, returns val1. If test2 is TRUE, returns val2. Etc. If no tests are TRUE, the final fallbackval is returned. Takes an odd number of arguments of 3 or more. | | ISBLANK(x) | True if the value or cell is blank | | LN(x) | Natural logarithm | | LOG(x,[ base]) | Logarithm with a given base, or 10 if base omitted | | LOWER(x) | Lowercase of a text value | | MAX(x, y,, z) | Maximum of 1 or more values | | MIN(x, y,, z) | Minimum of 1 or more values | | MOD(x, y) | Modulo division | | NOT(x) | Boolean NOT | | OR(x, y,, z) | Boolean OR of 1 or more arguments | | POWER(x, y) | Raises x to the y exponent | | ROUND(x, [digits]) | Rounds a number to the nearest integer. If digits is provided, rounds to that number of digits after the decimal place. Negative digits will round to the nearest 10, 100, etc. | | SQRT(x) | Square root | | SUBSTITUTE(text, pattern, replacement) | Replaces all occurrences of pattern in text with replacement | | SUM(x, y,, z) | Sum of 1 or more numeric arguments. Non-numeric values are ignored. | | UPPER(x) | Upercase of a text value | | XOR(x, y,, z) | Boolean XOR of 1 or more arguments |

Non-Formula Values

Values that are not formulas with recognized values (e.g. numbers, dollar amounts) will be interpreted and reformatted.

Literal Text

To force a value to behave like text, prefix it with a '. E.g. '0001 will be treated as regular text, not a number, and will not be reformatted. A text value beginning with an equal sign can be prevented from being interpreted as a formula in the same way. '=A. The leading apostrophe will be stripped when rendering the table cell. For a literal leading apostrophe, prefix with two apostrophes. ''Kay

Errors

If an expression cannot be evaluated, the cell will show an error symbol, such as #REF, #SYNTAX, or #ERROR. A more detailed message is in the title attribute and can be seen in a tooltip by mousing over it.

Styling

The parser tries to be smart about differentiating operators from markdown syntax, but if an operator character (such as *) is mistaken for formatting, escape it with \\*.

A limited set of styling can be applied to formula results if it consists of matching punctuation before and after. For example, **=SUM(A:A)** will render a sum in bold. Up to three such formats can be combined, e.g. ==~~__=SUM(A:A)__~~==. Blank cells that are autofilled by a formula in the column cannot currently be styled in this way.