{"record":{"id":"3e71a5a7e895ee88","repo":"iOfficeAI/OfficeCLI","slug":"sheet-not-found-ptsheetname","errorCode":null,"errorMessage":"Sheet not found: {ptSheetName}","messagePattern":"Sheet not found: (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":1661,"sourceCode":"        {\n            tableParts = new TableParts();\n            tblWs.AppendChild(tableParts);\n        }\n        tableParts.AppendChild(new TablePart { Id = tblWorksheet.GetIdOfPart(tableDefPart) });\n        tableParts.Count = (uint)tableParts.Elements<TablePart>().Count();\n        SaveWorksheet(tblWorksheet);\n\n        var tblIdx = PathIndex.FromArrayIndex(tblWorksheet.TableDefinitionParts.ToList().IndexOf(tableDefPart));\n        return $\"/{tblSheetName}/table[{tblIdx}]\";\n    }\n\n    private string AddPivotTable(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var index = position?.Index;\n        var ptSegments = parentPath.TrimStart('/').Split('/', 2);\n        var ptSheetName = ptSegments[0];\n        var ptWorksheet = FindWorksheet(ptSheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {ptSheetName}\");\n\n        // Source: \"Sheet1!A1:D100\" or \"A1:D100\" (same sheet)\n        var sourceSpec = properties.GetValueOrDefault(\"source\", \"\")\n            ?? properties.GetValueOrDefault(\"src\", \"\")\n            ?? throw new ArgumentException(\"pivottable requires 'source' property (e.g. source=Sheet1!A1:D100)\");\n        if (string.IsNullOrEmpty(sourceSpec))\n            throw new ArgumentException(\"pivottable requires 'source' property (e.g. source=Sheet1!A1:D100)\");\n\n        // R8-7: incidental whitespace around the source spec or its\n        // components (\" Sheet1 ! A1:D10 \") is a common paste-from-docs\n        // artefact. Trim the whole string and both sides of the '!'\n        // split so the downstream sheet/range lookup sees clean values.\n        sourceSpec = sourceSpec.Trim();\n\n        // R8-3: external workbook refs such as [other.xlsx]Sheet1!A1:D10\n        // used to fall through to FindWorksheet and surface as the\n        // misleading \"Source sheet not found: [other.xlsx]Sheet1\".\n        // Detect the '[' prefix up front and throw a clear error so","sourceCodeStart":1643,"sourceCodeEnd":1679,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L1643-L1679","documentation":"Thrown by AddPivotTable when the first segment of the parent path (the sheet that will host the pivot) does not resolve via FindWorksheet. A pivot table must be anchored to a concrete worksheet, so an unknown host sheet name is rejected up front rather than producing an orphan pivot cache.","triggerScenarios":"Calling Add('/MissingSheet/pivottable', ...) where no worksheet named MissingSheet exists, including case, whitespace, or renamed-sheet mismatches.","commonSituations":"Sheet was deleted/renamed, name with spaces unquoted, or using a sheet index where a name is required.","solutions":["Verify the host sheet exists by listing the workbook's sheets.","Quote sheet names with spaces exactly as stored.","Create the host sheet first with --type sheet if it does not exist."],"exampleFix":"// before\nadd /Report/pivottable --prop source=Data!A1:D100\n// after (create host sheet first)\nadd /sheet --prop name=Report\nadd /Report/pivottable --prop source=Data!A1:D100","handlingStrategy":"validation","validationCode":"// Confirm the host sheet exists before adding a pivot table to it.\nvar sheetName = parentPath.TrimStart('/').Split('/', 2)[0];\nif (handler.FindWorksheet(sheetName) is null)\n    throw new InvalidOperationException($\"Cannot add pivot: host sheet '{sheetName}' not found.\");","typeGuard":null,"tryCatchPattern":"try { handler.Add(parentPath, \"pivottable\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Sheet not found\"))\n{ /* list sheets; create the host sheet if needed, then retry */ }","preventionTips":["Create the host sheet explicitly in setup scripts before adding pivots.","Cache the sheet list when scripting many pivot adds.","Quote sheet names with spaces exactly as stored."],"tags":["excel","pivot-table","validation","sheet-lookup"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}