Python Examples
Basic functions
import xllify
@xllify.fn("xllipy.Hello")
def hello(name: str) -> str:
return f"Hello, {name}!"
@xllify.fn("xllipy.Add")
def add(a: float, b: float) -> float:
return a + bSlow operations
All Python functions run asynchronously, so slow operations don't freeze Excel:
@xllify.fn("xllipy.SlowCalc")
def slow_calc(seconds: float) -> str:
import time
time.sleep(seconds)
return f"Done after {seconds}s"Working with ranges
@xllify.fn("xllipy.MaxInRange")
def max_in_range(data: list) -> float:
max_val = float('-inf')
for row in data:
for cell in row:
if isinstance(cell, (int, float)):
max_val = max(max_val, cell)
return max_valUsage: =xllipy.MaxInRange(A1:Z100)
Black-Scholes option pricing
from math import log, sqrt, exp, erf
@xllify.fn("xllipy.BSCall", category="Finance")
def black_scholes_call(s: float, k: float, t: float, r: float, sigma: float) -> float:
"""Black-Scholes call option price"""
if t <= 0:
return max(s - k, 0)
d1 = (log(s / k) + (r + 0.5 * sigma ** 2) * t) / (sigma * sqrt(t))
d2 = d1 - sigma * sqrt(t)
def norm_cdf(x):
return 0.5 * (1 + erf(x / sqrt(2)))
return s * norm_cdf(d1) - k * exp(-r * t) * norm_cdf(d2)Usage: =xllipy.BSCall(100, 95, 0.25, 0.05, 0.2)
HTTP requests
@xllify.fn("xllipy.FetchPrice")
def fetch_price(symbol: str) -> float:
import requests
resp = requests.get(f"https://api.example.com/price/{symbol}")
return resp.json()["price"]Error handling
@xllify.fn("xllipy.SafeDivide")
def safe_divide(a: float, b: float) -> float:
if b == 0:
raise ValueError("Cannot divide by zero")
return a / bSystem info
@xllify.fn("xllipy.GetInfo")
def get_info() -> str:
import sys
import platform
return f"Python {sys.version.split()[0]} on {platform.system()}"Pandas integration
import pandas as pd
@xllify.fn("xllipy.AnalyzeData")
def analyze_data(data: list) -> str:
df = pd.DataFrame(data[1:], columns=data[0]) # First row is headers
return f"Mean: {df['value'].mean():.2f}"Last updated