iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {sheetName}

Error message

Sheet not found: {sheetName}

What it means

Thrown by ExcelHandler.GetDumpCfRuleNodes when the supplied sheetName does not resolve to any worksheet via FindWorksheet. This dump entry point enumerates conditional-formatting rules per sheet, so it requires an existing sheet.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.Cf.cs:246

            if (rule.TimePeriod?.HasValue == true) cfNode.Format["period"] = rule.TimePeriod.InnerText;
            if (rule.FormatId?.Value != null) cfNode.Format["dxfId"] = rule.FormatId.Value;
        }

        // Resolve dxfId to actual fill/font colors from the stylesheet
        if (rule.FormatId?.Value != null)
            PopulateCfNodeFromDxf(cfNode, (int)rule.FormatId.Value);
    }

    /// <summary>
    /// One node per conditional-formatting RULE across all cf elements on a
    /// sheet (a &lt;conditionalFormatting&gt; may hold several &lt;cfRule&gt;
    /// children). The dump emitter iterates this so every rule round-trips;
    /// counting cf ELEMENTS alone dropped the 2nd+ rule of a shared element.
    /// </summary>
    public List<DocumentNode> GetDumpCfRuleNodes(string sheetName)
    {
        var worksheet = FindWorksheet(sheetName)
            ?? throw new System.ArgumentException($"Sheet not found: {sheetName}");
        var result = new List<DocumentNode>();
        foreach (var cf in GetSheet(worksheet).Elements<ConditionalFormatting>())
        {
            var refStr = cf.SequenceOfReferences?.InnerText ?? "";
            foreach (var rule in cf.Elements<ConditionalFormattingRule>())
            {
                var node = new DocumentNode { Type = "conditionalFormatting" };
                node.Format["ref"] = refStr;
                PopulateCfNodeFromRule(worksheet, rule, node);
                result.Add(node);
            }
        }
        return result;
    }
}

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List sheets first via Get('/') and pass the exact name returned.
  2. Create the sheet before dumping its conditional formats.
  3. If iterating by index, resolve the name through the same Get('/') listing rather than guessing.

Example fix

// before
var rules = excel.GetDumpCfRuleNodes("SheetX");   // does not exist

// after
var sheets = excel.Get("/").Children.Select(c => c.Preview);
var name   = sheets.First(n => n.Equals("sheetx", StringComparison.OrdinalIgnoreCase));
var rules  = excel.GetDumpCfRuleNodes(name);
Defensive patterns

Strategy: validation

Validate before calling

if (excel.FindWorksheet(sheetName) == null)
    throw new ArgumentException($"Sheet '{sheetName}' does not exist.");

Prevention

When it happens

Trigger: Calling GetDumpCfRuleNodes with a typo'd, missing, or stale sheet name; calling it before any sheet exists in the workbook.

Common situations: A dump/export tool that iterates a stale list of sheet names; a renamed sheet referenced by its old label; an LLM hallucinating a sheet name.

Related errors


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