iOfficeAI/OfficeCLI · error · ArgumentException
source must not be empty
Error message
source must not be empty
What it means
RefreshPivotCacheFromSource requires a non-empty source range spec; passing null, empty, or all-whitespace is rejected before any parsing happens. The helper cannot infer a default range, and silently no-op-ing would leave the user thinking the refresh succeeded. The check is the first guard in the method.
Source
Thrown at src/officecli/Core/PivotTableHelper.Readback.cs:372
if (axisAsDataFieldNames.Count > 0)
node.Format["axisAsDataField"] = string.Join(",", axisAsDataFieldNames);
}
}
/// <summary>
/// R10-1: refresh a pivot's cache definition + records from a new source
/// range spec ("Sheet1!A1:C4" or "A1:C4" — same sheet as the existing
/// CacheSource). Replaces CacheFields, updates WorksheetSource.Reference
/// (and Sheet if changed), rewrites the PivotTableCacheRecordsPart, and
/// resizes pivotDef.PivotFields to match the new column count. Existing
/// PivotField Axis/DataField assignments are reset because indices may no
/// longer line up — RebuildFieldAreas reapplies them after this returns.
/// </summary>
private static void RefreshPivotCacheFromSource(PivotTablePart pivotPart, string newSourceSpec,
Dictionary<string, string>? pendingFieldAreaProps = null)
{
if (string.IsNullOrWhiteSpace(newSourceSpec))
throw new ArgumentException("source must not be empty");
newSourceSpec = newSourceSpec.Trim();
if (newSourceSpec.StartsWith("["))
throw new ArgumentException(
"External workbook references are not supported in pivot source. "
+ "Use a local sheet name (e.g. Sheet1!A1:D10)");
var cachePart = pivotPart.GetPartsOfType<PivotTableCacheDefinitionPart>().FirstOrDefault()
?? throw new InvalidOperationException("Pivot table has no cache definition part");
var cacheDef = cachePart.PivotCacheDefinition
?? throw new InvalidOperationException("Pivot cache definition is missing");
var existingWsSource = cacheDef.CacheSource?.WorksheetSource
?? throw new InvalidOperationException("Pivot cache source is not a worksheet source");
// Parse the new source spec.
string newSheetName;
string newRef;
if (newSourceSpec.Contains('!'))
{View on GitHub (pinned to 1ced45e900)
Solutions
- Provide a concrete range spec: source=Sheet1!A1:D100 or source=A1:D100 (same sheet as the existing cache source)
- Validate the input is non-empty in your own layer before calling refresh
- If you do not want to refresh the source, omit the source= property entirely instead of passing empty
Example fix
// before source="" // after source="Sheet1!A1:D100"
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(sourceSpec))
throw new InvalidOperationException("Pivot source spec is required for refresh"); Type guard
static bool IsValidSourceSpec(string? s) => !string.IsNullOrWhiteSpace(s);
Try / catch
try { RefreshPivotCacheFromSource(pivotPart, sourceSpec); }
catch (ArgumentException ex) when (ex.Message == "source must not be empty")
{ /* prompt user for a source range */ } Prevention
- Always pass a concrete range to refresh
- Skip the refresh call entirely if no source is intended
- Validate input presence in your own layer
When it happens
Trigger: source= with no value; source=" " (whitespace); source passed from an unset config variable; a UI that submitted the form with the source field empty.
Common situations: Missing required input; programmatic call that forgot to populate the source argument; template/render path that omits source when it should always be present for a refresh.
Related errors
- calculatedField requires a non-empty name
- calculatedField '{name}' requires a non-empty formula
- calculatedField '{raw}' must be 'Name:=Formula' (colon-separ
- field '{name}' not found in source headers: {available}
- External workbook references are not supported in pivot sour
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/b46386224947d932.
Report an issue: GitHub.