Skip to content

Feature/data column first - #2443

Open
kekovubicahome wants to merge 2 commits into
qax-os:masterfrom
kekovubicahome:feature/data_column_first
Open

kekovubicahome wants to merge 2 commits into
qax-os:masterfrom
kekovubicahome:feature/data_column_first

Conversation

@kekovubicahome

Copy link
Copy Markdown

Changes

pivotTable.go - Add pivot table column field position option (DataColumnFirst)

New Feature: DataColumnFirst option for PivotTableOptions

Added a new DataColumnFirst boolean field to the PivotTableOptions struct (pivotTable.go:50). This option controls the position of the system "Values" (Data) column on the pivot table column axis when multiple data fields are present.

Behavior:

  • DataColumnFirst: true - The Values column () is placed BEFORE user-defined column fields in the element
  • DataColumnFirst: false (default) - The Values column is placed AFTER user-defined column fields (maintains backward compatibility)

Changes:

  1. pivotTable.go (lines 997-1024) - Modified addPivotColFields() function to implement position logic:

    • When DataColumnFirst is true, inserts the Values field (x: -2) before user column fields
    • When DataColumnFirst is false (default), maintains existing behavior of appending after user columns
    • Only applies when multiple data fields exist (len(opts.Data) > 1)
  2. pivotTable.go (lines 1469-1480) - Enhanced extractPivotTableFields() function:

    • Added detection logic to read the Values field position from XML during pivot table extraction
    • Sets DataColumnFirst: true when field -2 appears at the beginning of pt.ColFields.Field
    • Maintains DataColumnFirst: false when field -2 appears after user fields
  3. pivotTable_test.go (lines 692-1050) - Comprehensive test coverage:

    • Extended TestAddPivotColFields() with scenarios for both DataColumnFirst configurations
    • Verifies correct XML field ordering in generated pivot table files
    • Includes round-trip testing to ensure option persists through GetPivotTables()
    • Extended TestExtractPivotTableFields() with detection logic verification
    • Tests both forward (write) and reverse (read) scenarios

Use Case Example

f := excelize.NewFile()
assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Revenue"}))
// ... populate data ...

// Pivot with Values column before user columns
assert.NoError(t, f.AddPivotTable(&excelize.PivotTableOptions{
    DataRange:       "Sheet1!A1:D31",
    PivotTableRange: "Sheet1!G4:M30",
    Rows:            []excelize.PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
    Columns:         []excelize.PivotTableField{{Data: "Type", DefaultSubtotal: true}},
    Data: []excelize.PivotTableField{
        {Data: "Revenue", Subtotal: "Sum", Name: "Sum of Revenue"},
        {Data: "Revenue", Subtotal: "Average", Name: "Average of Revenue"},
    },
    DataColumnFirst: true, // Values column before Type column
    RowGrandTotals:  true,
    ColGrandTotals:  true,
    ShowDrill:       true,
    ShowRowHeaders:  true,
    ShowColHeaders:  true,
    ShowLastColumn:  true,
}))

Generated XML with DataColumnFirst: true

<colFields count="2">
    <field x="-2"/>
    <field x="0"/>
</colFields>

Generated XML with DataColumnFirst: false (default)

<colFields count="2">
    <field x="0"/>
    <field x="-2"/>
</colFields>

Backward Compatibility

This change is fully backward compatible. The default value of DataColumnFirst is false, which preserves the existing behavior. Existing code that doesn't specify this option will continue to work without any modifications.

Testing

All tests pass:

  • Existing pivot table tests continue to pass
  • New tests verify both write (AddPivotTable) and read (GetPivotTables) scenarios
  • Edge cases handled (no columns, single data field, multiple data fields)
  • Field position validation and round-trip persistence verified

Benefits

  1. Layout Control - Users can now control the visual layout of pivot tables with multiple data fields
  2. Backward Compatibility - Default behavior unchanged, no breaking changes
  3. Extensible - Foundation for future positioning options
  4. Well-Tested - Comprehensive test coverage ensures reliability

@xuri xuri added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants