{"record":{"id":"6b584648f97ebc88","repo":"iOfficeAI/OfficeCLI","slug":"invalid-margin-value-value-use-1in-2cm-10m","errorCode":null,"errorMessage":"Invalid margin value: '{value}' (use 1in, 2cm, 10mm, 72pt, or bare inches)","messagePattern":"Invalid margin value: '(.+?)' \\(use 1in, 2cm, 10mm, 72pt, or bare inches\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.cs","lineNumber":93,"sourceCode":"        }\n        if (v.EndsWith(\"cm\"))\n        {\n            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);\n            return num / 2.54;\n        }\n        if (v.EndsWith(\"mm\"))\n        {\n            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);\n            return num / 25.4;\n        }\n        if (v.EndsWith(\"pt\"))\n        {\n            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);\n            return num / 72.0;\n        }\n        // Bare number = inches\n        if (!double.TryParse(v, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out num))\n            throw new ArgumentException($\"Invalid margin value: '{value}' (use 1in, 2cm, 10mm, 72pt, or bare inches)\");\n        return num;\n    }\n\n    // Build an <xdr:pic> element with an initial Transform2D, applying any\n    // user-supplied rotation/flip props. Keeps the Add.cs path readable.\n    // CONSISTENCY(scheme-color): Map a scheme-color name\n    // (\"accent1\"-\"accent6\", \"lt1\"/\"dk1\", \"lt2\"/\"dk2\", \"bg1\"/\"tx1\", \"bg2\"/\"tx2\",\n    // \"hlink\", \"folHlink\") to the OOXML theme index used by TabColor.Theme,\n    // color.Theme on fonts, etc. Returns null for non-scheme inputs — callers\n    // then fall back to srgbClr (hex) handling.\n    internal static uint? ExcelSchemeColorNameToThemeIndex(string s) =>\n        s?.Trim().ToLowerInvariant() switch\n        {\n            \"lt1\" or \"bg1\" or \"light1\" or \"background1\" => 0u,\n            \"dk1\" or \"tx1\" or \"dark1\" or \"text1\" => 1u,\n            \"lt2\" or \"bg2\" or \"light2\" or \"background2\" => 2u,\n            \"dk2\" or \"tx2\" or \"dark2\" or \"text2\" => 3u,\n            \"accent1\" => 4u,","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.cs#L75-L111","documentation":"Thrown by ExcelHandler.ParseMarginInches when a print-margin value does not end in a recognized unit suffix (in/cm/mm/pt) and is not parseable as a bare decimal number (inches). It is a guard at the Set/add path before the value is written into a PageMargins element, which itself only stores inches.","triggerScenarios":"Calling set on a sheet/workbook path with --prop margin.left= (or top/right/bottom/header/footer) where the value is null, contains an unsupported unit (px, pc, em, %), uses a comma decimal separator, or is otherwise non-numeric (e.g. 'margin.left=', 'margin.left=2px', 'margin.left=1,5cm').","commonSituations":"Locale pitfalls (a user in a comma-decimal locale types '2,5cm'), AI assistants defaulting to CSS units ('px','em'), trailing whitespace inside an empty string token, and copy-paste of '12.7mm' where 'mm' is supported but a typo like '12.7mn' is not.","solutions":["Use one of the supported forms: a bare number (inches), or a value suffixed with 'in', 'cm', 'mm', or 'pt' (e.g. margin.left=0.5in, margin.left=1.27cm, margin.left=10mm, margin.left=36pt).","If you need pixels, convert first: pixels/96 inches (e.g. 96px = 1in) since 'px' is NOT accepted by this margin helper.","Ensure the value uses a dot decimal separator regardless of locale (invariant culture parsing).","Trim stray whitespace and confirm the value is non-empty before the call."],"exampleFix":"// before\nset /Sheet1 --prop margin.left=2px\nset /Sheet1 --prop margin.left=1,5cm\n\n// after (px is not supported -> convert to inches; use dot decimal)\nset /Sheet1 --prop margin.left=0.0208in   // 2px / 96\nset /Sheet1 --prop margin.left=1.5cm","handlingStrategy":"validation","validationCode":"static readonly Regex MarginRe = new(@\"^\\s*(\\d+(\\.\\d+)?|\\.\\d+)\\s*(in|cm|mm|pt)?\\s*$\", RegexOptions.IgnoreCase);\nstatic bool IsValidMargin(string v) => !string.IsNullOrWhiteSpace(v) && MarginRe.IsMatch(v);\n// NOTE: 'px','em','rem','%' are NOT accepted by ParseMarginInches.","typeGuard":null,"tryCatchPattern":"try { var inches = ExcelHandler.ParseMarginInches(value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Invalid margin\"))\n{ /* surface to user with the accepted-unit list */ }","preventionTips":["Validate margin strings with a regex that only allows in/cm/mm/pt or a bare number before calling Set.","Always use the invariant (dot) decimal separator regardless of locale.","Convert pixel values to inches (px/96) yourself; do not pass 'px' to a margin prop."],"tags":["excel","validation","units","ooxml","page-margins"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}