Rust inside Excel

I have created a couple of new packages inside crates.io and GitHub. The first, xladd, is a library intended to make it easy to develop Excel addins using Rust. Internally, it uses the Excel4/12 API as described in the Excel SDK. The second addin, xladd-util, is an example of how to use xladd to create an addin. However, it does perform a few useful functions, such as gluing together ranges of cells.

Both libraries have been placed on GitHub with an open-source MIT license. Code writers, extenders or reviewers all very welcome. Especially if you are an expert in Rust macros, I think there are some interesting things that could be done to simplify things.

In GitHub, look at https://github.com/MarcusRainbow. On Crates.io, look at https://crates.io/crates/xladd and https://crates.io/crates/xladd-util

13 Likes

Not clear how to use them, any simple startup example!

Hi Hasan,

Did you look at the code inside xladd-util? Rather limited at the moment I'm afraid. I plan to add a lot more examples.

Marcus

I did, but did not understand what d you mean!

Hi Hasan,
One thing about my library is that I do not hide the details of how Excel addins work from you. The registration and function calls are basically the same as you would call in C or C++ -- the only difference is that they are Rust.

This means that to get anywhere you really need to read the documentation in the Excel SDK. For instance, you could start at https://docs.microsoft.com/en-us/office/client-developer/excel/calling-into-excel-from-the-dll-or-xll

So far, I have only exposed the Excel12 API. The Excel4 API is only needed for really old versions of Excel. Also, I have only exposed the pascal-based API, as Rust does not handle varargs very naturally. What this means is that the only call into Excel that I support is:

int pascal Excel12v(int xlfn, LPXLOPER12 operRes, int count, LPXLOPER12 opers);

The basic principle is that you can only call into Excel if Excel called you. When your addin is loaded, Excel calls a function xlAutoOpen (see https://docs.microsoft.com/en-us/office/client-developer/excel/xlautoopen )

From xlAutoOpen, you can register your own functions to be invoked from inside a spreadsheet, or in response to events such as keystrokes or mouse clicks. This means that your addin must implement xlAutoOpen. See xladd-util for an example of how to do this.

There are other functions that Excel invokes, such as xlAutoFree, when Excel wants to free data that you allocated, but the xladd framework implements these for you. The only one it cannot implement is xlAutoOpen.

If there are specific things that are unclear about the documentation or the API, please let me know, and I'll add documentation for them.

Good luck!

Marcus

2 Likes