Your own worksheet functions
Instead of =JuliaCall("birthday", A1), a Julia function can be a worksheet function of its own, =jl.birthday(A1). Mark it with @excel:
"""
birthday(people, days=365)
The probability that, in a group of people, at least two share a birthday.
# Arguments
- `people`: the number of people in the group.
- `days`: the number of days in the year.
"""
@excel broadcast=true function birthday(people, days=365)
1 - prod((days - k) / days for k in 0:people-1)
endThe docstring does double duty: as well as documenting the function in Julia, it gives Excel its help. As you type the formula, a tooltip shows the function's arguments, its description (the docstring's text after the signature, however long) and a description of the argument you're on (from the # Arguments list). The Insert Function (fx) dialog shows them too. There's nothing to register by hand and no separate file to keep in step: change the docstring, re-include the file, and Excel's help changes with it.

Include the file - with JuliaInclude, or the ribbon's Include button - and =jl.birthday(23) works in any cell of any open workbook, returning 0.507: in a group of just 23 people, it's more likely than not that two share a birthday. broadcast=true makes it work on ranges too: with the numbers 1 to 60 in A1:A60, =jl.birthday(A1:A60) returns a column of 60 probabilities, ready to chart. Typing =jl. lists all such functions. Functions you didn't write, from Julia itself or a package, can be worksheet functions too: see Exposing existing functions.
@excel and expose_to_excel take these options, except the last two, which only expose_to_excel takes:
| Option | Description |
|---|---|
name | The Excel name. Default "jl." followed by the Julia name. The prefix keeps your functions apart from Excel's own (when names clash, Excel's built-in function wins) and from cell references: abc123 is a cell, jl.abc123 isn't. A name without it, such as name="BIRTHDAY", is allowed. A name Excel can't use is refused with an error saying why: one with characters Excel doesn't allow (push!), one Excel would read as a cell reference, one beginning with Julia (reserved for the add-in's functions), or one differing only in case from a name already in use, since Excel ignores case. |
description, argdescriptions | Descriptions of the function and of each argument, for the tooltip shown as you type and the Insert Function dialog. By default they come from the docstring: the text after the signature, up to the first heading such as # Arguments or # Examples, and the # Arguments list. The tooltip shows them in full, keeping paragraphs and list items, but leaving out code blocks, admonitions such as !!! note, footnotes, and Markdown marks such as backticks; simple LaTeX maths such as x^p \pmod m becomes plain text, x^p (mod m). The Insert Function dialog, which Excel limits to 255 characters, shows only the description's first paragraph and the start of each argument's description. |
broadcast | Which arguments, given a range, are broadcast over - see below. Default false, none. |
handle | true to return a handle to the result, which stays in Julia, rather than the result itself - see below. Default false. |
volatile | true to recalculate at every calculation, as Excel's NOW and RAND do - see below. Default false. |
method | expose_to_excel only. For a function with several methods, the one whose arguments and help Excel shows; @excel uses the method it's attached to. Best given as the method that values from Excel reach, as in method=which(quantile, (Vector{Float64}, Float64)) - see Which method. Default: a guess, reported in the REPL. |
nargs | expose_to_excel only. A shorter way to choose a method: the one with this many arguments, as in nargs=1. Can't be combined with method. |
See also @excel and expose_to_excel in the Julia API.
Exposing existing functions
@excel is for functions you write. Any other Julia function - from Julia itself, a standard library or a package - can be made a worksheet function, without changing or wrapping it, with expose_to_excel. Its help as you type comes from its docstring, as with @excel. This can be done from a worksheet:
=JuliaEval("using LinearAlgebra; expose_to_excel(pinv; nargs=1)")and then =jl.pinv(A1:B2) returns the pseudo-inverse of a matrix, even of a singular one, for which MINVERSE gives #NUM!. To have functions available whenever Julia starts, put the calls, with the using lines they need, in a file you include.
In the demo below, three functions from Julia and its standard library become worksheet functions, one JuliaEval formula each:
bitstringshows the 64 bits Excel stores for a number.broadcast=truemakes it take a range, giving one result per cell. A16 holds=0.1+0.2and A17 holds0.3. Both display as 0.3, but their last bits differ: the sum is the floating-point number just above 0.3.reversehas many methods, for arrays as well as strings.method=which(reverse, (String,))picks the one for a string, so the tooltip shows that method's argument and docstring, andbroadcast=truereverses each cell in turn. Because the method was chosen explicitly, A10 doesn't say "chosen from".shuffle, from the Random standard library, is exposed withname="SHUFFLE", so it needs nojl.prefix. Excel has no function of that name to clash with. Its tooltip comes from the docstring like the others.volatile=truemakes it reshuffle at every calculation, as Excel'sRANDandRANDARRAYdo: pressing F9 at the end gives a new order each time.The annotations in brown text ("Formula at...") are to make what's happening in the demo clearer. They won't appear when you try CallFromExcel for yourself.
Replay the GIF by refreshing your browser (F5).

Some examples, each to be run in Julia, with using LinearAlgebra for cond and eigvals, and using Statistics (with the Statistics package installed) for quantile:
| Julia | In Excel |
|---|---|
expose_to_excel(bitstring) | =jl.bitstring(0.1) shows the 64 bits Excel stores for 0.1, which isn't exactly 0.1. |
expose_to_excel(bitstring; broadcast=true) | =jl.bitstring(A1:A10) does the same for each number in A1:A10. |
expose_to_excel(cond) | =jl.cond(A1:B2) gives the condition number of a matrix and =jl.cond(A1:B2, 1) its condition number in the 1-norm, as cond's second argument is optional. |
expose_to_excel(eigvals; nargs=1) | =jl.eigvals(A1:C3) gives the eigenvalues of a matrix. nargs=1 picks the method with one argument, rather than eigvals(A, B), the generalized eigenvalues of two matrices. |
expose_to_excel(quantile; method=which(quantile, (Vector{Float64}, Float64))) | =jl.quantile(A1:A100, 0.95) gives the 95th percentile of the data. method picks the method that a column of numbers and a number reach, rather than quantile(f, v, p). |
expose_to_excel(evalpoly; broadcast=true) | =jl.evalpoly(A1:A20, B1:B3) evaluates the polynomial with coefficients B1:B3 at each value in A1:A20, giving a column of 20 results. The coefficients aren't broadcast, being declared p::AbstractVector. |
expose_to_excel(hypot; name="HYPOT") | =HYPOT(3, 4) returns 5, under a name of your choosing. |
Functions that take only whole numbers, such as binomial or Dates.monthname, fail with a MethodError when exposed, as numbers from cells arrive in Julia as Float64. See Numbers from a worksheet arrive as Float64 for ways round that.
Points to note
Optional arguments
Arguments with default values (days=365) are optional: leave them out at the end of a call, as in =jl.birthday(23), and Julia uses the defaults; give them to override the defaults, as in =jl.birthday(23, 366). Leaving out a required argument (=jl.birthday()), or one before an argument that is given (=jl.birthday(, 366)), is an error naming it. Keyword arguments aren't available as worksheet arguments; use JuliaCallNamedArgs for those.
Which method
Which method runs is up to Julia's dispatch, from the arguments the formula gives. But Excel shows one method's arguments and description, needs to know which arguments can be left out, and broadcast=true goes by one method's declared argument types. @excel follows the method it's attached to. For a function with several methods, expose_to_excel makes an informed guess - preferring methods that can take what a worksheet passes, then the most general - and says which it chose. =JuliaEval("expose_to_excel(bitstring)") shows, in its cell,
jl.bitstring follows bitstring(x::T) where T @ Base intfuncs.jl:1095, chosen from 2 methodswhere "chosen from" marks a guess (it's left out when the function has one method, or when you choose it), and the same appears in the REPL. The guess is usually right; the method shown, or the tooltip's argument names, show when it isn't. To choose:
method=mfollows the methodm. The surest way is to ask Julia'swhichfor the method that the values from Excel will reach, giving the types they arrive as:In Excel Arrives in Julia as a number Float64text StringTRUEorFALSEBoola column a Vector, such asVector{Float64}a row or a block a Matrix, such asMatrix{Float64}a range of mixed values a Vector{Any}orMatrix{Any}as in
expose_to_excel(quantile; method=which(quantile, (Vector{Float64}, Float64))), for=jl.quantile(A1:A100, 0.95). See Marshalling for the details.nargs=nfollows the method withnarguments, counting those with default values, and the best by the same guess if there are several, as inexpose_to_excel(eigvals; nargs=1).
method is the safer choice in a file included whenever Julia starts: a package update that adds methods could change the guess, and with it the argument names, the help and what broadcast=true broadcasts.
See Exposing existing functions for what these give. With expose_to_excel, an argument is optional if a method without it takes the same first arguments, going by their names: those with default values, as with @excel, and those of other methods, as in cond(x) alongside cond(x, p), so that =jl.cond(A1:B2) and =jl.cond(A1:B2, 1) both work.
Broadcasting
In Julia, broadcasting makes a function written for single values, f(x, y), work element by element on arrays, written f.(x, y). The broadcast option of @excel and expose_to_excel does this for ranges, in one call from Excel, and the result spills:
broadcast=false(the default): no argument is broadcast, and the function gets each range whole, as withJuliaCall(see Marshalling).broadcast=true: every argument is broadcast, except those declared with an array type, such asw::AbstractVectororm::Matrix{Float64}, which are passed whole.broadcast=(:x, :y), orbroadcast=:xfor one: only the arguments named.- This is how many of Excel's own functions behave. Given a range where they expect a single value, they return a spilled array:
=SQRT(A1:A10)gives ten square roots, and=A1:A3*B1:C1, a column times a row, gives a 3 by 2 table. Arguments that expect a range, such asSUM's, take it whole, as arguments declared as arrays do here. One difference: where ranges don't fit together, Excel pads the result with#N/A, while a broadcast Julia function gives an error naming the two arguments.
Ranges broadcast together must have the same number of rows, or one of them a single row, and likewise for columns; otherwise the error names the two arguments. A broadcast function that returns an array for each element gives an error, as the result can't be shown in one range.
Example
birthday above is marked broadcast=true, so =jl.birthday(A1:A60) returns a column of 60 probabilities, and with 365 and 366 in B1:C1, =jl.birthday(A1:A60, B1:C1) - a column against a row - a 60 by 2 table. Without broadcast=true, birthday would receive A1:A60 as a vector, and fail.
Returning a handle
For a function whose result belongs in Julia rather than on the sheet - a fitted model, a large matrix, a dictionary - use handle=true, and the worksheet function returns a handle to it, as JuliaCallHandle does. Handles' rules apply: the same arguments give the same object, built once, and it's freed when no formula uses it. With broadcasting, the handle is to the whole broadcast result. Excel's description of the function ends "Returns a handle."
Example
With
@excel handle=true squarearray(n) = reshape(1:Int(n)^2, Int(n), Int(n))=jl.squarearray(1000) in A1 returns a handle such as jl:ReshapedArray#1@k3x9, and =JuliaCall("sum", A1) sums the million elements without them ever reaching the sheet. To see the result itself, pass the handle to identity: =JuliaCall("identity", jl.squarearray(3)) returns the 3 by 3 matrix.
Volatile functions
Excel recalculates a cell when its inputs change, so a function that reads something Excel can't see - a Julia global set by JuliaSetVar or JuliaEval, a file, a database, the clock, random numbers - can show an out-of-date result, and a function with no arguments is recalculated only by Ctrl+Alt+F9. With volatile=true, as in @excel volatile=true latest_price(ticker) = ..., Excel recalculates it at every calculation, as it does NOW and RAND. Use it sparingly: every calculation (every edit, in automatic calculation) calls Julia for each cell using the function, and recalculates the cells that depend on them. volatile=true can't be combined with handle=true, as a handle keeps its object while the arguments are unchanged.
When functions are registered
After JuliaLaunch, JuliaInclude and JuliaEval, whether in a worksheet formula or called from VBA, and the ribbon's Launch Julia and Include buttons, the add-in registers any functions that have been exposed since it last did, once Excel is next idle (for VBA, after the macro ends). Functions exposed from the Julia REPL are registered by the ribbon's Register Functions button, or from VBA with Application.Run "JuliaRegisterFunctions", which registers them straight away. Registered functions stay registered until Excel closes: a function no longer exposed in a new Julia session gives an error saying so.
Formulas typed too soon show #NAME?
A formula that uses a function before it is registered shows #NAME?, and goes on doing so after registration, until it is re-entered (select it, press F2, then Enter). This doesn't affect saved workbooks: reopened before Julia is running, their cells keep their saved values, and recalculate correctly once the functions are registered (in manual calculation mode, at the next recalculation). VBA code that writes such formulas should run Application.Run "JuliaRegisterFunctions" first.
Changing a function
Change its code and re-include the file: the worksheet function calls whatever the Julia function now does, with no need to register again. (Cells using it aren't recalculated by the change. Use the ribbon's Rebuild Objects and Recalculate All, or press Ctrl+Alt+F9 - except for cells with handles, including those of functions exposed with handle=true, which Ctrl+Alt+F9 doesn't rebuild.) Changing its arguments needs it to be exposed again, which re-including a file with @excel does.
From VBA
Call the Julia function itself, as in jl.JuliaCallVBA("birthday", 23) (see Calling from VBA), rather than the worksheet function through Application.Run("jl.birthday", 23). That avoids a detour through Excel, allows results a worksheet can't hold (such as a dictionary), and makes the code independent of whether the function has been registered. Use the Julia name, not the Excel name, and leave out optional arguments as usual. The @excel options don't apply. To broadcast, add a . to the name, as in jl.JuliaCallVBA("birthday.", sizes), which broadcasts over every argument. For a function with handle=true, jl.JuliaCallHandle("squarearray", 1000) returns a handle that lasts until jl.JuliaHandleRelease frees it, whereas through Application.Run the handle is freed straight away.