iOfficeAI/OfficeCLI · error · ArgumentException

Unknown totals-row function '{tok}'. Valid: sum, average, co

Error message

Unknown totals-row function '{tok}'. Valid: sum, average, count, countNums, max, min, stdDev, var, none, custom.

What it means

Thrown by MapTotalsRowFunction when a per-column totalsRowFunction token matches none of the switch arms. The caller (AddTable path, Add.Tables.cs:1565) splits properties["totalsRowFunction"] on commas and lowercases each token before mapping. Note the message advertises 'countNums' but the switch is case-insensitive and also accepts aliases NOT in the message: 'avg', 'countnumbers', 'maximum', 'minimum', 'stdev', 'variance', 'label', and empty string. Unknown aggregations like 'median' or 'counta' are not supported by the OOXML totals-row enum.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Cell.cs:37

    // Map a table-column totals-row function token to its OOXML enum and the
    // SUBTOTAL function code Excel uses. Unknown tokens throw — the earlier
    // SUM fallback silently changed the aggregation the user asked for
    // (silent-accept enum-miss family). Every token the dump emitter can
    // produce (TotalsRowFunction InnerText, lowercased) is enumerated below,
    // so dump→batch replay never hits the throw.
    internal static (TotalsRowFunctionValues, int) MapTotalsRowFunction(string tok) => tok switch
    {
        "sum" => (TotalsRowFunctionValues.Sum, 109),
        "average" or "avg" => (TotalsRowFunctionValues.Average, 101),
        "count" => (TotalsRowFunctionValues.Count, 103),
        "countnums" or "countnumbers" => (TotalsRowFunctionValues.CountNumbers, 102),
        "max" or "maximum" => (TotalsRowFunctionValues.Maximum, 104),
        "min" or "minimum" => (TotalsRowFunctionValues.Minimum, 105),
        "stddev" or "stdev" => (TotalsRowFunctionValues.StandardDeviation, 107),
        "var" or "variance" => (TotalsRowFunctionValues.Variance, 110),
        "none" or "label" or "" => (TotalsRowFunctionValues.None, 0),
        "custom" => (TotalsRowFunctionValues.Custom, 109),
        _ => throw new ArgumentException(
            $"Unknown totals-row function '{tok}'. Valid: sum, average, count, countNums, max, min, stdDev, var, none, custom.")
    };

    private string GetCellDisplayValue(Cell cell, Core.FormulaEvaluator? evaluator = null)
    {
        if (cell.DataType?.Value == CellValues.InlineString)
        {
            return cell.InlineString?.InnerText ?? "";
        }

        var value = cell.CellValue?.Text ?? "";

        if (cell.DataType?.Value == CellValues.SharedString)
        {
            var sst = _doc.WorkbookPart?.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
            if (sst?.SharedStringTable != null && int.TryParse(value, out int idx))
            {
                var item = sst.SharedStringTable.Elements<SharedStringItem>().ElementAtOrDefault(idx);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a supported token (sum, average/avg, count, countNums/countnumbers, max/maximum, min/minimum, stdDev/stdev, var/variance, none/label, custom).
  2. Leave the token empty for non-first columns to get the default (Sum); for column 0 empty means label.
  3. For unsupported aggregations, set totalsRowFunction=custom and write the SUBTOTAL formula into the totals cell directly.
  4. Validate each token against the allowed set before calling AddTable.

Example fix

// before
props["totalsRowFunction"] = "label,sum,median"; // 'median' -> throw

// after
props["totalsRowFunction"] = "label,sum,average";
// or, for a custom aggregation on col 2:
props["totalsRowFunction"] = "none,sum,custom";
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> ValidTotals = new(StringComparer.OrdinalIgnoreCase)
{ "sum","average","avg","count","countnums","countnumbers","max","maximum",
  "min","minimum","stddev","stdev","var","variance","none","label","","custom" };
var bad = tokens.Where(t => !ValidTotals.Contains(t)).ToList();
if (bad.Any()) throw new ArgumentException($"Unsupported totals tokens: {string.Join(",",bad)}");

Type guard

static bool IsValidTotalsToken(string t) =>
    (new[] { "sum","average","avg","count","countnums","countnumbers",
      "max","maximum","min","minimum","stddev","stdev","var","variance",
      "none","label","","custom" }).Contains(t, StringComparer.OrdinalIgnoreCase);

Prevention

When it happens

Trigger: totalsRowFunction="sum,median,average" (median unsupported); totalsRowFunction="counta"; totalsRowFunction="subtotal"; totalsRowFunction="total".

Common situations: User typed an Excel UI label or a formula name instead of the enum token; copied 'Count Numbers' literally; expected an aggregation the OOXML totals row does not offer.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/19b058ab08e5189c. Report an issue: GitHub.