How CallFromExcel works

CallFromExcel's Excel add-in is written in C#, using Excel-DNA. Its worksheet functions are registered with Excel through the Excel SDK, and VBA calls the same code through the object created by CreateObject("CallFromExcel.ComServer"). The Julia ribbon tab is part of the same add-in, and its buttons use the same code too. The add-in communicates with Julia over a local HTTP connection:

  1. JuliaLaunch starts a Julia process (as a normal Windows process, or under WSL) running a small HTTP server (CallFromExcel.start_server()) on a free local port, and writes that port number to a file so the add-in can find it. If a session for the current Excel process already appears to be running, JuliaLaunch checks that it's actually responding before reusing it, rather than starting a duplicate.
  2. JuliaEval POSTs the Julia expression as plain text to the server's /eval endpoint. The Julia function srv_eval_inner (in comms.jl) evaluates it with Meta.parseall/eval and returns the result - encoded in CallFromExcel's own wire format - in the HTTP response body.
  3. JuliaCall and JuliaCallVBA instead encode the function name and its arguments (marshalled per the rules described in Marshalling) into that same wire format and POST it to the /call endpoint. srv_call_inner decodes it and invokes the named function directly, returning the result the same way. This avoids Meta.parseing a literal expression, which is slow for calls passing large arrays. JuliaCallNamedArgs and JuliaCallNamedArgsVBA encode their arguments the same way but POST them to the /callkw endpoint, where Julia splits them at the ";" into positional and named arguments.
  4. The add-in decodes the response body into worksheet values or VBA variables. A call that fails in Julia - the code throws, or its result can't be encoded - comes back marked as a failure rather than as a value, so it can't be confused with a string result. The add-in shows it on a worksheet as a string such as #MethodError: ... Julia REPL has more details and stacktrace!, and raises it as an error in VBA.

Other points to note:

  • The wire format distinguishes the types VBA does (e.g. Long from Double), and supports 2-D arrays (a worksheet range) and Excel error values. JSON, the obvious alternative, has only nested 1-D arrays and a much thinner type system. Arrays of numbers are sent in bulk, without per-element type or length information, and Julia ranges such as 1:1000000 are sent as just their first value, step and length.
  • Each JuliaEval/JuliaCall is a synchronous HTTP request/response, over connections kept open between calls. If Julia isn't there to answer (e.g. after =JuliaEval("exit()")), the call fails with a message saying so.
  • Object handles hook into Excel's RTD (real-time data) mechanism, through Excel-DNA's ExcelAsyncUtil.Observe. A JuliaCallHandle formula subscribes to an RTD topic keyed on the function name and its arguments, so the same formula implies the same object. Only the first formula to ask for a topic makes Julia build and store the object (CallFromExcel.handle_create) and return its handle. Later recalculations, and other formulas with the same function and arguments, get the same handle without calling Julia. When no formula uses a topic any more, Excel tells the add-in, which tells Julia to free the object. Any string argument matching the handle pattern is assumed to be a handle: it is sent to Julia as a reference, which Julia replaces with the stored object, or reports as not found. The key also includes a generation number, increased when JuliaLaunch finds a new Julia session, so that every handle formula subscribes again and rebuilds its object.
  • Running Julia inside Excel's own process, via Julia embedding, would avoid the HTTP round trip altogether. But Julia could then only be restarted by restarting Excel and there would be no separate Julia REPL to watch or debug in.

Viewing the code

The Excel add-in is written in C#, using Excel-DNA; its source is in the dotnet-addin folder of the repository. Julia source code is always visible on your PC, and the @functionloc macro is an easy way to locate the code of any function you're interested in.