# Example usages of the spreadsheet directive

You can also try this example using the standalone command:

python rstspreadsheet.py sample.rst sample.html


.. spreadsheet:: Simple spreadsheet
:eq: {2} = {0} and {1}
{3} = int(not {2})

=== === =========== =================
p   q    p and q     not (p and q)
=== === =========== =================
0   0
1   0
0   1
1   1
=== === =========== =================


.. spreadsheet:: Spreadsheet with python functions
:eq: {2} = '{0}({1})'
{3} = {2}
:setup: from math import sin, cos, exp, pi
def fac(n):
return fac(n-1) * n if n > 1 else 1
:format: 3:'%0.2f'

======== ======== ========== ======
function argument expression result
======== ======== ========== ======
fac      5
sin      pi
cos      pi
exp      1
======== ======== ========== ======


## Special variables

.. spreadsheet:: Use of the special variables
:eq: {1} = i
{2} = last
{3} = i == last
{4} = sum(col.int) + 1
{5} = cols[0].int

== == ====== =========== ================== =============
#  i  last   i == last   sum(col.int) + 1   cols[0].int
== == ====== =========== ================== =============
0
1
2
3
== == ====== =========== ================== =============


## Conditions

.. spreadsheet:: Use of condition
:eq: {1:i==0} = 3
{1:0<i<last} = col.int[-1] + 1
{1:i==last} = sum(col.int)
{2:i==last} = col.sum()
{3:i==last} = col.mean()

======== =============== ========== ===========
#        increment/sum   sum data   mean data
======== =============== ========== ===========
0                        2.1        4.1
1                        3.2        3.2
2                        4.3        2.3
sum
======== =============== ========== ===========


## Error handling

Note

To see errors in Sphinx, you will need to set keep_warnings = True in conf.py.

### Error in :setup:

.. spreadsheet::
:eq: {1} = {0}
:setup: this is error!

== ==
i  j
== ==
0
1
2
== ==


### Error in :eq:

.. spreadsheet::
:eq: {1} = this is error!
{1:this is error, too} = 1

== ==
i  j
== ==
0
== ==