{"record":{"id":"5e5ed7f554470ffb","repo":"iOfficeAI/OfficeCLI","slug":"datafield-onebasedidx-showas-index-out-of-range","errorCode":null,"errorMessage":"dataField{oneBasedIdx}.showAs: index out of range (1..{dfCount} data field(s) defined)","messagePattern":"dataField(.+?)\\.showAs: index out of range \\(1\\.\\.(.+?) data field\\(s\\) defined\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Set.cs","lineNumber":360,"sourceCode":"                    // R15-4: accept `dataField{N}.showAs=<token>` as the\n                    // write-side counterpart of the Get readback key. N is\n                    // 1-indexed over the current DataFields list; map to\n                    // the positional `showdataas` list so RebuildFieldAreas\n                    // can apply the transform through its existing showAs\n                    // override path. Consistency with the Get readback\n                    // symmetry rule: users copy a key from Get and Set it\n                    // back without learning a second vocabulary.\n                    var lkDf = key.ToLowerInvariant();\n                    if (lkDf.StartsWith(\"datafield\") && lkDf.EndsWith(\".showas\"))\n                    {\n                        var idxStr = lkDf.Substring(\"datafield\".Length,\n                            lkDf.Length - \"datafield\".Length - \".showas\".Length);\n                        if (int.TryParse(idxStr, out var oneBasedIdx) && oneBasedIdx >= 1)\n                        {\n                            var existingDf = pivotDef.DataFields?.Elements<DataField>().ToList();\n                            var dfCount = existingDf?.Count ?? 0;\n                            if (oneBasedIdx > dfCount)\n                                throw new ArgumentException(\n                                    $\"dataField{oneBasedIdx}.showAs: index out of range \" +\n                                    $\"(1..{dfCount} data field(s) defined)\");\n\n                            // Build / extend the positional showdataas list\n                            // so slot oneBasedIdx-1 carries the new token,\n                            // leaving earlier slots empty (RebuildFieldAreas\n                            // treats empty slot as \"keep current\").\n                            fieldAreaProps.TryGetValue(\"showdataas\", out var existingShow);\n                            var slots = existingShow?.Split(',').Select(s => s.Trim()).ToList()\n                                        ?? new List<string>();\n                            while (slots.Count < oneBasedIdx) slots.Add(\"\");\n                            slots[oneBasedIdx - 1] = value;\n                            fieldAreaProps[\"showdataas\"] = string.Join(\",\", slots);\n\n                            // Force RebuildFieldAreas to run even without\n                            // any rows/cols/values/filters in this call.\n                            if (!fieldAreaProps.ContainsKey(\"rows\") && !fieldAreaProps.ContainsKey(\"cols\")\n                                && !fieldAreaProps.ContainsKey(\"values\") && !fieldAreaProps.ContainsKey(\"filters\")","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Set.cs#L342-L378","documentation":"Thrown in SetPivotTableProperties when the key dataField{N}.showAs=<token> references a 1-based data field index N that exceeds the number of DataFields currently defined on the pivot. The code parses N from between 'dataField' and '.showAs', validates N >= 1, then checks N against the actual DataFields count. This is the write-side counterpart of the Get readback key, letting users copy a key from Get and Set it back.","triggerScenarios":"Calling Set pivot dataField5.showAs=percentOfTotal when the pivot only has 3 data fields defined (N must be in 1..3). Or referencing an index after a values= change that reduced the data field count.","commonSituations":"Copying a dataField key from a Get dump of a different/older pivot that had more value fields; stale assumptions about how many values are defined; off-by-one confusion between 0-based and 1-based indexing.","solutions":["Run Get pivot first to see how many DataFields are currently defined and their 1-based indices.","Use an index within the range 1..count returned by the DataFields list.","If you need more data fields, add them via values= before referencing the higher index.","Remember the index is 1-based (dataField1 is the first, not dataField0)."],"exampleFix":"// before — only 2 data fields exist\nSet pivot dataField3.showAs=percentOfTotal\n// after — use a valid index, or add the value first\nSet pivot values=Revenue,Cost dataField2.showAs=percentOfTotal","handlingStrategy":"validation","validationCode":"// Before setting dataField{N}.showAs, check the count\nint dfCount = pivotDef.DataFields?.Elements<DataField>().Count() ?? 0;\nif (oneBasedIdx < 1 || oneBasedIdx > dfCount)\n    throw new ArgumentException($\"dataField index {oneBasedIdx} out of range (1..{dfCount}).\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run Get pivot to see the current DataFields list and valid indices.","Remember data field indices are 1-based (dataField1 is the first).","If you need more data fields, add them via values= first.","After removing value fields, update any dataField{N}.showAs references that pointed past the new count."],"tags":["pivot","excel","showdataas","data-field","index","set-property"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}