{"record":{"id":"758871009fefbd15","repo":"iOfficeAI/OfficeCLI","slug":"source-sheet-not-found-sourcesheetname","errorCode":null,"errorMessage":"Source sheet not found: {sourceSheetName}","messagePattern":"Source sheet not found: (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":1716,"sourceCode":"        if (resolved.HasValue)\n        {\n            sourceSheetName = resolved.Value.sheet;\n            sourceRef = resolved.Value.rangeRef;\n        }\n        else if (sourceSpec.Contains('!'))\n        {\n            var srcParts = sourceSpec.Split('!', 2);\n            sourceSheetName = srcParts[0].Trim().Trim('\\'', '\"').Trim();\n            sourceRef = srcParts[1].Trim();\n        }\n        else\n        {\n            sourceSheetName = ptSheetName;\n            sourceRef = sourceSpec;\n        }\n\n        var sourceWorksheet = FindWorksheet(sourceSheetName)\n            ?? throw new ArgumentException($\"Source sheet not found: {sourceSheetName}\");\n\n        var ptPosition = (properties.GetValueOrDefault(\"position\", \"\")\n            ?? properties.GetValueOrDefault(\"pos\", \"\"))\n            ?.Replace(\"$\", \"\"); // CONSISTENCY(dollar-strip): parity with source ref handling\n        if (string.IsNullOrEmpty(ptPosition))\n        {\n            // Auto-position: place after the source data range\n            var rangeEnd = sourceRef.Split(':').Last();\n            var colEndMatch = System.Text.RegularExpressions.Regex.Match(rangeEnd, @\"([A-Za-z]+)\");\n            var nextCol = colEndMatch.Success ? IndexToColumnName(ColumnNameToIndex(colEndMatch.Value.ToUpperInvariant()) + 2) : \"H\";\n            ptPosition = $\"{nextCol}1\";\n        }\n\n        // R26-1: validate that the pivot output fits within sheet dimensions\n        // before writing any cache/pivot parts. A position near the sheet edge\n        // can produce an end-location beyond XFD1048576, which causes a\n        // partial-write: cache parts are already saved when the render stage\n        // discovers the overflow and throws, leaving a corrupt zip.","sourceCodeStart":1698,"sourceCodeEnd":1734,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L1698-L1734","documentation":"Thrown by AddPivotTable when the sheet named in the source spec (the part before '!', or the host sheet when no '!' is present) does not resolve via FindWorksheet. The pivot cache needs a real source worksheet, so an unknown source sheet name is rejected before building the cache. Note structured-table refs (Table1[#All]) and defined names are resolved earlier and fall through to this literal parse only if unresolved.","triggerScenarios":"Calling Add('/Report/pivottable', ...) with source=MissingSheet!A1:D10 where MissingSheet does not exist, or a same-sheet source where the host sheet itself is wrong.","commonSituations":"Source sheet renamed/deleted, typo in the sheet name, or pointing at a sheet in a different workbook after the external-ref guard.","solutions":["Verify the source sheet exists by listing the workbook's sheets.","For same-sheet sources, ensure the host sheet (parent path) actually contains the data.","If the source is a table or defined name, confirm it resolves before the literal parse path."],"exampleFix":"// before\nadd /Report/pivottable --prop source=Data2!A1:D100\n// after (correct sheet name)\nadd /Report/pivottable --prop source=Data!A1:D100","handlingStrategy":"validation","validationCode":"// Resolve and verify the source sheet exists before Add.\nvar src = (props.GetValueOrDefault(\"source\") ?? props.GetValueOrDefault(\"src\")).Trim();\nvar sourceSheet = src.Contains('!') ? src.Split('!', 2)[0].Trim().Trim('\\'', '\"').Trim() : parentSheet;\nif (handler.FindWorksheet(sourceSheet) is null)\n    throw new InvalidOperationException($\"Source sheet not found: {sourceSheet}\");","typeGuard":null,"tryCatchPattern":"try { handler.Add(parentPath, \"pivottable\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Source sheet not found\"))\n{ /* correct the source sheet name and retry */ }","preventionTips":["Verify the source sheet name against the workbook before adding the pivot.","For same-sheet sources, confirm the host sheet (parent path) holds the data.","If using a table/defined-name source, confirm it resolves before relying on the literal parse."],"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"}