{"record":{"id":"c78aceaeff9d445a","repo":"iOfficeAI/OfficeCLI","slug":"invalid-anchor-picanchorraw-expected-e-g-b","errorCode":null,"errorMessage":"Invalid anchor: '{picAnchorRaw}'. Expected e.g. 'B2', 'B2:E6', or one of 'oneCell'/'twoCell'/'absolute'.","messagePattern":"Invalid anchor: '(.+?)'\\. Expected e\\.g\\. 'B2', 'B2:E6', or one of 'oneCell'/'twoCell'/'absolute'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Drawings.cs","lineNumber":307,"sourceCode":"        // DrawingsPart (and possibly the media blob) was already attached,\n        // leaving an empty <xdr:wsDr/> container plus orphaned xl/media\n        // bytes in the saved file even though the add reported an error.\n        var (xlImgStream, imgPartType) = OfficeCli.Core.ImageSource.Resolve(imgPath);\n        using var xlImgDispose = xlImgStream;\n\n        var picAnchorRaw = properties.GetValueOrDefault(\"anchor\");\n        var picAnchorModeExplicit = properties.GetValueOrDefault(\"anchorMode\");\n        bool picHasRange = false;\n        int picRangeFromCol = 0, picRangeFromRow = 0, picRangeToCol = -1, picRangeToRow = -1;\n        // `anchor=` is either a cell-range (\"B2\" / \"B2:E6\") or an\n        // anchorMode token (\"oneCell\"/\"twoCell\"/\"absolute\"). Prefer the\n        // cell-range interpretation; fall back to mode-token only when\n        // the value is a recognized token. Explicit `anchorMode=` wins\n        // the mode selection regardless.\n        if (!string.IsNullOrWhiteSpace(picAnchorRaw) && !IsAnchorModeToken(picAnchorRaw))\n        {\n            if (!TryParseCellRangeAnchor(picAnchorRaw, out picRangeFromCol, out picRangeFromRow, out picRangeToCol, out picRangeToRow))\n                throw new ArgumentException($\"Invalid anchor: '{picAnchorRaw}'. Expected e.g. 'B2', 'B2:E6', or one of 'oneCell'/'twoCell'/'absolute'.\");\n            picHasRange = true;\n            if (properties.ContainsKey(\"width\") | properties.ContainsKey(\"height\")\n                | properties.ContainsKey(\"x\") | properties.ContainsKey(\"y\"))\n                Console.Error.WriteLine(\n                    \"Warning: 'x'/'y'/'width'/'height' are ignored when 'anchor' is a cell range (anchor defines the full rectangle).\");\n        }\n        var picAnchorMode = (picAnchorModeExplicit\n            ?? (picHasRange ? \"twoCell\" : picAnchorRaw)\n            ?? \"twoCell\").Trim().ToLowerInvariant();\n\n        var picDrawingsPart = picWorksheet.DrawingsPart\n            ?? picWorksheet.AddNewPart<DrawingsPart>();\n\n        if (picDrawingsPart.WorksheetDrawing == null)\n        {\n            picDrawingsPart.WorksheetDrawing = new XDR.WorksheetDrawing();\n            picDrawingsPart.WorksheetDrawing.Save();\n","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Drawings.cs#L289-L325","documentation":"Thrown by AddPicture when the 'anchor' property is present, is not a recognized anchorMode token (oneCell/twoCell/absolute), and fails to parse as a cell-range reference via TryParseCellRangeAnchor. The picture branch first checks IsAnchorModeToken; only if that returns false does it attempt cell-range parsing. This is the broadest anchor error message because pictures accept three input shapes: cell ('B2'), range ('B2:E6'), or mode token ('oneCell').","triggerScenarios":"Setting properties[\"anchor\"] to a value that is neither a recognized mode token nor a valid cell/range. Examples: 'B-2', 'top-left', 'absolute_anchor', 'B2;E6'. If the value IS a mode token, IsAnchorModeToken returns true and no error fires. If it is a cell/range that fails parsing, this error fires.","commonSituations":"User passes a descriptive word that is not one of the three recognized tokens. User uses wrong separators (semicolon, comma). User copies an anchor from a non-Excel context.","solutions":["Use a single cell like 'B2', a colon-separated range like 'B2:E6', or one of the mode tokens 'oneCell'/'twoCell'/'absolute'.","Drop anchor= and use x=/y=/width=/height= for numeric positioning.","Check for typos in mode-token names (case-insensitive, but must match exactly: oneCell, twoCell, absolute).","Ensure range separators are colons (:), not commas or semicolons."],"exampleFix":"// before\nadd /Sheet1 --type picture --src logo.png --anchor \"top-left\"\n// after\nadd /Sheet1 --type picture --src logo.png --anchor \"B2:E6\"","handlingStrategy":"validation","validationCode":"// Validate picture anchor before the add call\nif (properties.TryGetValue(\"anchor\", out var picAnchor) && !string.IsNullOrWhiteSpace(picAnchor))\n{\n    var isModeToken = picAnchor.Trim().ToLowerInvariant() is \"onecell\" or \"twocell\" or \"absolute\";\n    var isCellRange = System.Text.RegularExpressions.Regex.IsMatch(\n        picAnchor, @\"^[A-Z]+\\d+(:[A-Z]+\\d+)?$\", RegexOptions.IgnoreCase);\n    if (!isModeToken && !isCellRange)\n        throw new InvalidOperationException(\n            $\"Invalid picture anchor '{picAnchor}'. Expected a cell ('B2'), range ('B2:E6'), \" +\n            \"or mode token ('oneCell'/'twoCell'/'absolute').\");\n}","typeGuard":"static bool IsValidPictureAnchor(string? s)\n{\n    if (string.IsNullOrWhiteSpace(s)) return false;\n    var v = s.Trim().ToLowerInvariant();\n    if (v is \"onecell\" or \"twocell\" or \"absolute\") return true;\n    return System.Text.RegularExpressions.Regex.IsMatch(\n        s, @\"^[A-Z]+\\d+(:[A-Z]+\\d+)?$\", RegexOptions.IgnoreCase);\n}","tryCatchPattern":"try { handler.AddPicture(parentPath, type, position, properties); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Invalid anchor:\"))\n{\n    Console.Error.WriteLine($\"{ex.Message} Use a cell, range, or mode token.\");\n}","preventionTips":["Use A1-style cells ('B2'), colon-separated ranges ('B2:E6'), or the exact mode tokens (oneCell, twoCell, absolute).","Never use commas or semicolons as range separators.","Pre-validate anchor strings with a combined regex + mode-token check.","Fall back to x=/y=/width=/height= for numeric positioning when anchor format is uncertain."],"tags":["excel","picture","anchor","cell-reference","anchormode","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}