Skip to content

Functions

Excel worksheet functions.

F builds a call for any function name, so nothing here limits which functions you can use::

F.SUM(rng("B3:B10"))
F.XLOOKUP(key, lookup_range, return_range)
F.SOME_ADDIN_FUNCTION(a, b)

Functions added in Excel 2010 or later are stored in the file with an _xlfn. prefix; written bare they show as #NAME? when the workbook is opened. That rewriting happens automatically, so you always write the plain name.

Two checks run at construction, both aimed at mistakes that would otherwise survive as far as opening the workbook:

  • Arity. Names in :data:_ARITY are checked against their argument count and raise :class:TypeError on the line that built them. Names outside the table are not checked at all, so add-in and future functions keep working.
  • Spelling. An unrecognised name close to a known one warns (:class:UnknownFunctionWarning) rather than raising, since an arbitrary name is legitimate. check=False silences both.

Func

Bases: Expr

A worksheet function call.

None renders as an omitted argument, so Func("VLOOKUP", a, b, c, None) gives VLOOKUP(a,b,c,).

Source code in src/pycountant/excel/functions.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
class Func(Expr):
    """A worksheet function call.

    ``None`` renders as an omitted argument, so ``Func("VLOOKUP", a, b, c, None)``
    gives ``VLOOKUP(a,b,c,)``.
    """

    name: str
    args: tuple

    def __new__(cls, name: str, *args, check: bool = True) -> Func:
        if check:
            _check_name(name, len(args))
        parts = tuple(lit(a) for a in args)
        text = f"{stored_name(name)}({','.join(p.text for p in parts)})"
        obj = cls._make(text, P_ATOM)
        obj.name, obj.args = name, parts
        return obj

    def shift(self, rows: int = 0, cols: int = 0) -> Func:
        # Already validated when first built -- and shifting cannot change arity.
        return Func(self.name, *(a.shift(rows, cols) for a in self.args), check=False)

UnknownFunctionWarning

Bases: UserWarning

An unrecognised function name that looks like a misspelling of a known one.

Source code in src/pycountant/excel/functions.py
35
36
class UnknownFunctionWarning(UserWarning):
    """An unrecognised function name that looks like a misspelling of a known one."""

stored_name(name)

The name as it must appear inside the xlsx file.

Source code in src/pycountant/excel/functions.py
204
205
206
207
208
209
210
211
def stored_name(name: str) -> str:
    """The name as it must appear inside the xlsx file."""
    upper = name.upper()
    if upper in _XLWS:
        return "_xlfn._xlws." + upper
    if upper in _XLFN:
        return "_xlfn." + upper
    return name