{"record":{"id":"a652a9f15b0b758e","repo":"iOfficeAI/OfficeCLI","slug":"pivottable-index-ptidx-out-of-range-1-pivotpar","errorCode":null,"errorMessage":"PivotTable index {ptIdx} out of range (1-{pivotParts.Count})","messagePattern":"PivotTable index (.+?) out of range \\(1-(.+?)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Query.cs","lineNumber":756,"sourceCode":"                var seriesIdx = int.Parse(chartMatch.Groups[2].Value);\n                var seriesChildren = chartNode.Children.Where(c => c.Type == \"series\").ToList();\n                if (seriesIdx < 1 || seriesIdx > seriesChildren.Count)\n                    throw new ArgumentException($\"Series {seriesIdx} not found (total: {seriesChildren.Count})\");\n                var seriesNode = seriesChildren[seriesIdx - 1];\n                seriesNode.Path = path;\n                return seriesNode;\n            }\n            return chartNode;\n        }\n\n        // Pivot table path: /Sheet1/pivottable[N]\n        var pivotMatch = Regex.Match(cellRef, @\"^pivottable\\[(\\d+)\\]$\", RegexOptions.IgnoreCase);\n        if (pivotMatch.Success)\n        {\n            var ptIdx = int.Parse(pivotMatch.Groups[1].Value);\n            var pivotParts = worksheet.PivotTableParts.ToList();\n            if (ptIdx < 1 || ptIdx > pivotParts.Count)\n                throw new ArgumentException($\"PivotTable index {ptIdx} out of range (1-{pivotParts.Count})\");\n\n            var pivotPart = pivotParts[ptIdx - 1];\n            var ptNode = new DocumentNode { Path = path, Type = \"pivottable\" };\n            if (pivotPart.PivotTableDefinition != null)\n                PivotTableHelper.ReadPivotTableProperties(pivotPart.PivotTableDefinition, ptNode, pivotPart);\n            return ptNode;\n        }\n\n        // Slicer path: /Sheet1/slicer[N]\n        var slicerMatch = Regex.Match(cellRef, @\"^slicer\\[(\\d+)\\]$\", RegexOptions.IgnoreCase);\n        if (slicerMatch.Success)\n        {\n            var slIdx = int.Parse(slicerMatch.Groups[1].Value);\n            if (!TryFindSlicerByIndex(worksheet, slIdx, out var slicerElem, out var slicerCache) || slicerElem == null)\n                throw new ArgumentException($\"slicer[{slIdx}] not found on sheet '{sheetNameFromPath}'\");\n            var slNode = new DocumentNode { Path = path, Type = \"slicer\" };\n            ReadSlicerProperties(slicerElem, slicerCache, slNode);\n            return slNode;","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Query.cs#L738-L774","documentation":"Thrown for /Sheet/pivottable[N] when N exceeds worksheet.PivotTableParts.Count (1-based). Each worksheet owns its own pivot-table parts; the valid per-sheet range is shown in the message.","triggerScenarios":"handler.Get(\"/Sheet1/pivottable[2]\") on a sheet with one (or zero) pivot tables. pivottable[0].","commonSituations":"Hard-coded pivot index on a sheet whose pivots were removed. Wrong sheet. Zero-based indexing.","solutions":["Use a 1-based index in [1, pivotCount].","Verify the sheet has pivot tables before indexing.","try/catch(ArgumentException) and read the (1-N) range from the message."],"exampleFix":"// before\nvar pt = handler.Get(\"/Sheet1/pivottable[2]\"); // throws if <2 pivots\n\n// after\ntry { var pt = handler.Get(\"/Sheet1/pivottable[2]\"); }\ncatch (ArgumentException) { /* pivot index invalid */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"static int? ElementIndex(string cellRef, string element)\n{\n    var m = Regex.Match(cellRef, $@\"^{Regex.Escape(element)}\\[(\\d+)$\", RegexOptions.IgnoreCase);\n    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;\n}","tryCatchPattern":"try { return handler.Get(\"/Sheet1/pivottable[2]\"); }\ncatch (ArgumentException ex) { /* ex.Message carries the valid (1-N) range */ return null; }","preventionTips":["Pivot indices are 1-based and per-sheet — there is no index 0.","Confirm the sheet has pivot tables before indexing.","Parse the (1-N) range from the exception message."],"tags":["excel","pivottable","path-index","argument-exception"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}