Luau Examples
This page demonstrates some simple functions. It also introduces xllify's async Luau support.
Some of the examples are contrived and already implemented by Excel, so see them merely as illustrations.
Basic maths
xllify.ExcelFunction({
name = "ADD",
category = "Math"
}, function(x, y)
return x + y
end)
xllify.ExcelFunction({
name = "MULTIPLY",
category = "Math"
}, function(x, y)
return x * y
end)String operations
xllify.ExcelFunction({
name = "UPPER"
}, function(text)
return string.upper(text)
end)
xllify.ExcelFunction({
name = "WORDCOUNT"
}, function(text)
local count = 0
for word in string.gmatch(text, "%S+") do
count = count + 1
end
return count
end)Working with ranges
xllify.ExcelFunction({
name = "SUMRANGE",
description = "Sum all numbers in range"
}, function(data)
local sum = 0
for i = 1, #data do
for j = 1, #data[i] do
if type(data[i][j]) == "number" then
sum = sum + data[i][j]
end
end
end
return sum
end)Usage: =SUMRANGE(A1:C10)
Date functions
xllify.ExcelFunction({
name = "TIMESTAMP"
}, function()
return os.date("%Y-%m-%d %H:%M:%S")
end)
xllify.ExcelFunction({
name = "ISLEAPYEAR"
}, function(year)
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end)Async example
xllify.ExcelFunction({
name = "SLOWCALC",
execution_type = "async"
}, function(seconds)
-- Simulate delay
async.sleep(seconds) # will yield until seconds elapses!
return "Done after " .. seconds .. " seconds"
end)Multiple scripts
xllify.ExcelFunction({
name = "SQUARE"
}, function(x)
return x * x
end)
xllify.ExcelFunction({
name = "CUBE"
}, function(x)
return x * x * x
end)xllify.ExcelFunction({
name = "REVERSE"
}, function(text)
return string.reverse(text)
end)Build:
xllify MyAddin.xll math.luau text.luauAll functions will be available in Excel.
Combining Luau and Python
You can mix Luau and Python in one XLL for the best of both worlds.
xllify.ExcelFunction({
name = "FASTSUM",
description = "SUM not good enough for you?"
}, function(data)
local sum = 0
for i = 1, #data do
for j = 1, #data[i] do
if type(data[i][j]) == "number" then
sum = sum + data[i][j]
end
end
end
return sum
end)import xllify
import requests
@xllify.fn("xllipy.FetchPrice")
def fetch_price(symbol: str) -> float:
"""Fetch stock price from API"""
resp = requests.get(f"https://api.example.com/price/{symbol}")
return resp.json()["price"]Build:
xllify HybridAddin.xll fast_math.luau api_calls.py \
--py-entrypoint api_calls \
--py-requirements requirements.txtLoad HybridAddin.xll into Excel:
=FASTSUM(A1:Z100) → Ultra-fast Luau calculation
=xllipy.FetchPrice("AAPL") → Python API call with requests libraryLast updated