Resolver Hacks contains information and code for doing fun and useful things with Resolver One. This site is created and maintained by Michael Foord, not by Resolver Systems. Please read the disclaimer before using any of the code on this site.
Follow Michael on:
Python, IronPython and all that...

Contents
Introduction
Central to Resolver is the concept that your spreadsheets are turned into Python code ('generated code') and then executed in order to generate the results.
This means that any 'user code' you add to a spreadsheet is a a central part of your spreadsheet, not a bolt on extra.
It has two important consequences:
- It is possible to use Python and .NET libraries within your spreadsheets. These are both very powerful, and anything your computer can do can be done from inside a spreadsheet.
- Your spreadsheets can be exported as code, and then integrated with other parts of your IT system.
If you want to jump straight into a simple example of creating spreadsheets with user code, then turn to Silly Colors. This uses parts of the .NET framework and the Python standard library to create a multi-coloured spreadsheet. If you would rather find out what these things are before you start using them, then read on...
Python
Writing user code means writing Python code. Fortunately Python is very easy to learn. You can start with very simple loops and functions, and gradually expand your knowledge. It is a fully fledged object oriented language though, with many large systems written entirely in Python.
For useful resources on getting started with Python, see the Useful Links page.
Python is a mature Open Source language that has been around since 1990. It started off life in the Unix world, but is also widely used on Windows.
Python gets used for an enormous range of different tasks, including:
- Games development (Civilization IV uses a lot of Python),
- Web applications (YouTube and Reddit are almost entirely written in Python)
- System administration (several Linux distributions make heavy use of Python)
- Embedded systems (including the One Laptop Per Child project)
- Science (particularly bio-informatics and language analysis)
- Engineering tasks (Seagate automate their hard drive testing with Python)
- GIS services (Python is the scripting languages for several commercial GIS systems)
- Computer graphics and animation (Sony Imageworks script their entire image processing pipeline with Python)
- Writing desktop applications (Bittorrent is one of the most famous Python desktop applications)
The Python 'philosophy' emphasizes clarity and readability of code, whilst maximising flexibility for the programmer.
IronPython
IronPython is a Microsoft implementation of the Python programming language, to run on the .NET framework. It was originally developed by Jim Hugunin (who also created Jython, Python for the Java Virtual Machine) as an experiment. It has since been taken on by Microsoft, and turned into a fully fledged member of the .NET family.
Resolver is written in IronPython, and any user code you write is IronPython (which is really just the Microsoft flavour of Python). IronPython code can take advantage of many of the great number of Python libraries that there are available. You can also use the .NET framework directly within IronPython code.
The Python Standard Library
The most important Python libraries you can use are what is known as the 'Python Standard Library'. This is a wide set of modules for tackling common programming tasks, and has earned Python the reputation of coming 'batteries included'.
Resolver includes a copy of the Python Standard Library, so you can import and use these functions and classes within user code.
Not all of the standard library works with IronPython, some it relies on C code (whereas IronPython is written in C#) or uses platform dependent features that aren't available on IronPython. However, the IronPython team have put a great deal of effort into making sure that as much of the standard library as possible does work.
Where there are bits that don't work, there may be a wrapper around a .NET library to provide an identical equivalent and there will certainly be a .NET library that does a similar job anyway.
The .NET Framework
IronPython runs on top of the .NET framework. The .NET framework is a programming 'ssytem' created by Microsoft for writing everything from desktop applications, programmers libraries, web applications to web services. It is the most common framework for writing modern business applications. The user interface for Resolver is written using the Windows Forms library from .NET.
The .NET framework consists of two parts:
- The CLR: The Common Language Runtime, which actually runs your programmes and includes a fast JIT (Just in Time Compiler), built in security features and a lot more besides.
- The Framework Classes: Like the Python Standard Library, the .NET framework comes with its own large assortment of tools for tackling programming tasks.
As you can use the .NET classes easily within IronPython code, IronPython is doubly blessed with libraries and modules to use. The IronPython Cookbook contains many good examples of using .NET classes from IronPython.
Resolver Library
Of course, in order to write user code that does something useful, you need to be able to manipulate the spreadsheet. The way Resolver does this is by giving you access to the spreadsheet and its components as 'objects' that you can do things with.
All the Resolver spreadsheet objects are defined in Python files, contained in the Library directory in your Resolver installation. The usual place for this is C:\Program Files\Resolver\Library, unless you have installed Resolver somewhere odd! We refer to these files as 'the Library code'.
The major spreadsheet objects defined in the Library code are:
Workbook
This represents the whole spreadsheet, and as well as giving you access to all the worksheets it provides methods for adding new ones.
Worksheet
This represents individual worksheets in your spreadsheet. It gives you access to the rows, columns and cells that it contains, and allows you to set attributes ('traits') for the whole worksheet.
Cell
This represents individual cells in a spreadsheet. You can ask cells for their value, or set the value, and also set all the traits (like BackColor, Bold and friends).
Row
This represents a single row in a worksheet. It provides access to all the cells in the row, as well as allowing you to set attributes for the whole row.
Column
See Row, but substitute the word column instead...
CellRange
Cell ranges provide a view into a specified (rectangular) area of a worksheet. They allow you to perform operations with just a part of a worksheet, including formatting them. They are a nice way of working with (including presenting) tables of data that are smaller than a whole worksheet.
If you are curious, you can look at the source code for these files to get an idea of how things work. However, if you change anything and it breaks then you are on your own [1]!
There is lots more in the library of course, including useful functions, image worksheets (worksheets that display an image instead of containing cells) and so on. The other articles and examples on this site will hopefully give you ideas about some of the things you can do with them.
Developing Your Own Spreadsheet Code
As you create functions and classes for use with your spreadsheets, it can be unwieldy to include the code within each spreadsheet. Things are much more manageable if they live in separate Python files (modules).
Resolver adds the directory containing the current spreadsheet to the 'path'. This means that it can import from modules contained in the same directory as the spreadsheet, which makes it easier to distribute spreadsheets with the Python files that they depend on.
If you create modules that you use in several spreadsheets, the Library directory is currently the best place to put them. That means that you can import from them into your spreadsheets, with code like the following:
from Library.MyModule import MyFunction, MyClass
Eventually we will provide a user directory for putting storing your Resolver modules (at which point I have to remember to update this document).
| [1] | Re-installing Resolver should restore as much normality as a spreadsheet program is able to bestow. (I.e. it will remove your changes, but leave your personal problems intact.) |
Last edited Fri Feb 15 13:45:04 2008.

IronPython in Action