iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {fcfSheetName}

Error message

Sheet not found: {fcfSheetName}

What it means

Thrown by AddFormulaCf when the first segment of parentPath does not resolve to a worksheet. The formula-based (expression) conditional format needs the target worksheet to attach the rule and to compute priority via NextCfPriority(GetSheet(...)).

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Cf.cs:435

            SequenceOfReferences = new ListValue<StringValue>(
                isSqref.Split(' ').Select(s => new StringValue(s)))
        };

        var isWsElement = GetSheet(isWorksheet);
        InsertConditionalFormatting(isWsElement, isCf);

        SaveWorksheet(isWorksheet);
        var isCfCount = isWsElement.Elements<ConditionalFormatting>().Count();
        return $"/{isSheetName}/cf[{isCfCount}]";
    }

    private string AddFormulaCf(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var fcfSegments = parentPath.TrimStart('/').Split('/', 2);
        var fcfSheetName = fcfSegments[0];
        var fcfWorksheet = FindWorksheet(fcfSheetName)
            ?? throw new ArgumentException($"Sheet not found: {fcfSheetName}");

        // CONSISTENCY(cf-sqref): three-level fallback matches dataBar/colorScale branches.
        // R22-2: path-tail range is the fallback before the hardcoded default.
        var fcfPathRange = fcfSegments.Length > 1 && !string.IsNullOrEmpty(fcfSegments[1]) ? fcfSegments[1] : "A1:A10";
        var fcfSqref = ValidateSqref(properties.GetValueOrDefault("sqref") ?? properties.GetValueOrDefault("range") ?? properties.GetValueOrDefault("ref", fcfPathRange), "ref");
        // CONSISTENCY(cf-value-alias): the help schema documents value/
        // formula1 as aliases of formula, and the cellIs branch already
        // accepts them — the formula branch alone rejected the alias.
        var fcfFormula = properties.GetValueOrDefault("formula")
            ?? properties.GetValueOrDefault("formula1")
            ?? properties.GetValueOrDefault("value")
            ?? throw new ArgumentException("Formula-based conditional formatting requires 'formula' property (e.g. formula=$A1>100)");
        // The <x:formula> element is A1-only — an R1C1-style reference makes
        // real Excel refuse the file (0x800A03EC) while schema validation
        // stays green. Same guard cell formulas already get.
        ValidateNoR1C1Reference(fcfFormula);
        ValidateFormulaLength(fcfFormula, "conditional-format formula");

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use the exact worksheet name as the first path segment.
  2. Create the worksheet before adding the formula CF rule.
  3. Re-list sheets to confirm current names before the call.

Example fix

// before: 'Report' sheet does not exist
add /Report/A1:A10 formulacf formula=$A1>100
// after
add-sheet Report
add /Report/A1:A10 formulacf formula=$A1>100
Defensive patterns

Strategy: validation

Validate before calling

var sheet = parentPath.TrimStart('/').Split('/', 2)[0];
if (FindWorksheet(sheet) is null)
    throw new ArgumentException($"Sheet '{sheet}' not found.");

Type guard

static bool SheetExists(string? sheetName, IEnumerable<string> known)
    => sheetName is not null && known.Contains(sheetName, StringComparer.OrdinalIgnoreCase);

Try / catch

try { return Add(path, "formulacf", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* correct sheet name, retry */ throw; }

Prevention

When it happens

Trigger: Calling Add with parentPath '/<sheet>/...' and type=formula (or cf type=formula/expression, or formulacf) where <sheet> is not found. The lookup is case-insensitive but an absent or renamed sheet fails.

Common situations: Stale sheet name from a prior session; path constructed dynamically from user input without validating the sheet exists; workbook created from a template with localized default sheet names.

Related errors


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