Categories

The Excel Gambit

In the spirt of shoving Lisp where Lisp should-not-go I have jammed Gambit Scheme into Excel.

If you’re interested in trying it out you should go and get the files RIGHT NOW at GitHub. You just need to add the compiled .XLL into your add-ins and use the formula =GAMBITC(“(+ 1 1)”) in a cell of your choosing.

Please note this is a proof-of-concept. You probably shouldn’t use it for anything important, most importantly because it probably leaks like a sieve. You should also know I only really tested it on Excel 2003. It should load into Excel 2007 but I didn’t try that yet.

Why? Because it’s there, and I’m here

Why did I do it? Well the rationale behind doing this was that I wanted to write an app for Excel in Scheme. I want to do that because I think it would be vastly superior to using .NET or VBA or COM. Having spent a few hours on it I think I could definitely achieve what I set out to do but whether I’ll actually go this route in the end I’m undecided about. More on that later.

Mature like a good cheese

The Excel API, as it turns out, is … well … mature. Now I like old things quite a lot. I’m old, and I like me just fine. One consequence of age is that we get a little extra baggage on the way. This is true of Excel too.

Now, IMHO one of the more challenging aspects of programming a long-lived project is version management. Especially version management of an API. Broken backwards/forwards compatibility of those APIs could be serious impediments to a new Excel’s acceptance. Unsurprisingly then Excel has a number of APIs that have all undergone various enhancements over the years. To me though the switch from Excel 2003 to Excel 2007 was the most significant, requiring the most additional cognitive load to work with. For now, and for simplicity, I have chosen to totally deny the existence of Excel 2007. But I’m coming back to it, oh yes I am.

I Eat Pain For Breakfast

I had read a bit online about the Excel C API and found it all hugely confusing. So I bought a book thinking that that would make it clearer. Although the author does attempt to explain how you might write an Excel add-in he does it in a way that makes programming from it hard. For example, the information I need is spread all-over the book. Answering a single question about Excel’s behaviour with respect to calling the core Excel4/Excel12 function necessitates flicking between the index and more than 10 different page sections.

The reason for this is probably that Excel is really a very versatile tool which is why a lot of the world’s business (large and small :)) seem to operate their database, accounts and customer details from it. So there’s not really one track through the development process. There’s literally hundreds, limited only by your imagination!

Having said that the book in combination with the Excel 2007 SDK was sufficient to pick through the rubble and build something workable. Maybe I’ll try and produce some guides/tutorials of my own to make the topic clearer. Maybe.

On the Gambit side there’s not much more documentation than what’s on the manual HTML page to help. Like the Excel book it is also very densely populated with information. IMHO its main failing is that it could really do with having more documentation of the C macro functions. Did I ever mention that I hate C macros? Well in Gambit these undocumented C macros are effectively what Gambit Scheme is written in and you kind-of need a fairly thorough explanation of how it all fits together to be able to make an effective glue with them. I will be coming back to this later when I try and construct lists from cell ranges.

Finally I decided to do this all in MinGW rather than the more usual VC++. Whilst going this route did cost me a bit of time I’d much rather use the GCC toolchain because I understand it better.

Where-to-now

Now that I have the interpreter in Excel I will probably work on tidying it up for Excel 2007. More importantly though, I want to see if I can exploit Excel 2007’s all important multi-threading capability.

If that all works out I might use it to make a custom add-in. I will develop the app from the REPL in Emacs using Gambit Scheme. When it’s done I’ll compile it up into a standalone XLL with Gambit. That’s the plan anyway.

Let me know if you like it, or can see a use for it. Encouragement is always good.

2 replies on “The Excel Gambit”

Sergeysays:

Just reading the book you mentioned. All examples in vc, but I want to use gcc. Is it possible? What is your experience with xll and gcc?

Yes, it’s possible. XLLs with GCC is fine. It just takes a bit more configuration. If you look at the Makefile I used for this project you’ll see that I use some specific LD_FLAGS and LIBS. As the bare minimum I think you will need to have:

 LD_FLAGS=-s -shared -Wl,--kill-at LIBS=-lxlcall32 -lmingwex -lgambc -lws2_32