Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/officecli/Core/PivotTableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4720,13 +4720,20 @@ private static OpenXmlElement BuildMultiRowItems(
int count = 0;
foreach (var (outer, inners) in groups)
{
// Outer subtotal row: <i><x v="outerIdx"/></i>
// Outer subtotal row: <i><x v="outerIdx"/><x v="defaultInner"/></i>
// ECMA-376 §18.10.1.44: r=0 requires fieldCount (2) <x> children.
// Pad with the inner field's default item index.
var outerEntry = new RowItem();
var outerPivIdx = outerOrder[outer];
if (outerPivIdx == 0)
outerEntry.AppendChild(new MemberPropertyIndex());
else
outerEntry.AppendChild(new MemberPropertyIndex { Val = outerPivIdx });
var innerDefaultIdx = innerOrder.Count;
if (innerDefaultIdx == 0)
outerEntry.AppendChild(new MemberPropertyIndex());
else
outerEntry.AppendChild(new MemberPropertyIndex { Val = innerDefaultIdx });
container.AppendChild(outerEntry);
count++;

Expand Down Expand Up @@ -5036,6 +5043,20 @@ void Walk(AxisNode node)
if (idx == 0) item.AppendChild(new MemberPropertyIndex());
else item.AppendChild(new MemberPropertyIndex { Val = idx });
}
// ECMA-376 §18.10.1.44: each <i> must have exactly
// (fieldCount - r) <x> children. Subtotal entries have
// path.Length < fieldIndices.Count, so pad with the "default"
// item index for each remaining deeper field. The default item
// is appended after all value items by AppendFieldItems, so its
// 0-based index equals the unique value count for that level.
// Leaf entries already satisfy the requirement (path.Length ==
// fieldIndices.Count), so the loop is a no-op for them.
for (int i = path.Length; i < fieldIndices.Count; i++)
{
int defaultIdx = perLevelOrder[i].Count;
if (defaultIdx == 0) item.AppendChild(new MemberPropertyIndex());
else item.AppendChild(new MemberPropertyIndex { Val = defaultIdx });
}
// For col-axis leaves with K>1, append one extra <x/> for the
// first data field (index 0 = bare <x/>). The K-1 subsequent
// entries below handle the remaining data fields.
Expand Down