iOfficeAI/OfficeCLI · warning · ArgumentException

Invalid path '{path}': trailing '/' is not allowed.

Error message

Invalid path '{path}': trailing '/' is not allowed.

What it means

Thrown by NormalizeExcelPath when the path has length > 1, is not the single-character '/', and ends with '/'. Excel paths must not have a trailing slash — it implies an empty final segment and previously exposed raw OOXML local names.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs:107

    /// Sheet1!A:A → /Sheet1/col[A]   (whole column)
    /// Paths already starting with '/' are returned unchanged.
    /// </summary>
    // Excel-quoted sheet name: 'My Data (2024)'!A1 — strip one pair of single
    // quotes and un-double embedded quotes ('It''s'!A1 → It's). Excel REQUIRES
    // the quoted form for names with spaces/punctuation, so refs pasted from
    // formulas arrive quoted. Unquoted names pass through unchanged.
    internal static string UnquoteSheetName(string s)
        => s.Length >= 2 && s[0] == '\'' && s[^1] == '\''
            ? s[1..^1].Replace("''", "'")
            : s;

    internal string NormalizeExcelPath(string path)
    {
        // Reject malformed segment separators that previously slipped past
        // the regex matchers and exposed raw OOXML local names. DOCX already
        // rejects these; bring XLSX up to parity.
        if (path.Length > 1 && path != "/" && path.EndsWith("/"))
            throw new ArgumentException($"Invalid path '{path}': trailing '/' is not allowed.");
        if (path.StartsWith("//"))
            throw new ArgumentException($"Invalid path '{path}': leading '//' is not allowed.");
        if (path.Contains("//"))
            throw new ArgumentException($"Invalid path '{path}': empty path segment ('//') is not allowed.");
        // Handle "/Sheet1!A1" — strip leading '/' when '!' is present so native
        // notation is parsed correctly. EXCEPT when the first slash segment
        // names an existing sheet: '!' is legal inside a sheet NAME (Excel
        // forbids only : \ / ? * [ ]), and reinterpreting "/Q1!Results" as
        // bang notation made such a sheet permanently unaddressable.
        if (path.StartsWith('/') && path.Contains('!'))
        {
            var seg0 = path[1..];
            var seg0Slash = seg0.IndexOf('/');
            var first = seg0Slash < 0 ? seg0 : seg0[..seg0Slash];
            if (!GetWorksheets().Any(w => w.Name.Equals(first, StringComparison.OrdinalIgnoreCase)))
                path = path[1..];
        }
        if (path.Equals("/workbook", StringComparison.OrdinalIgnoreCase)) return "/";

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Strip a single trailing '/' from non-root paths before sending (see validationCode).
  2. Use the documented path shape: leading '/', segments separated by '/', no trailing '/' (except root '/').
  3. For root, send exactly '/'.

Example fix

// before
string path = "/Sheet1/A1/";
// after
string path = "/Sheet1/A1";
if (path.Length > 1 && path.EndsWith('/')) path = path[..^1];
Defensive patterns

Strategy: validation

Validate before calling

static string NormalizePath(string p)
{
    if (string.IsNullOrEmpty(p)) return p;
    while (p.Length > 1 && p != "/" && p.EndsWith('/')) p = p[..^1];
    return p;
}

Type guard

null

Try / catch

try { NormalizeExcelPath(path); }
catch (ArgumentException ex) when (ex.Message.Contains("trailing '/'"))
{ path = path.TrimEnd('/'); if (path.Length == 0) path = "/"; }

Prevention

When it happens

Trigger: Calling any path-based Excel API with '/Sheet1/', '/Sheet1/A1/', '/Sheet1/table[1]/', or any path whose last char is '/'. The root '/' alone is allowed.

Common situations: Concatenating a trailing '/' in URL-style path building; normalizing paths with a generic trim that adds a trailing separator; user input from a CLI that accepts a trailing slash.

Related errors


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