#2425·excelize

Pivot tables: colFields "data" field always after user specified colFields

Author: kekovubicahomeCreated Sep 21, 2026Updated Sep 22, 2026

Description

  1. In Go scope : file - pivotTable.go
go
// addPivotColFields create pivot column fields by given pivot table
// definition and option.
func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
	if len(opts.Columns) == 0 {
		if len(opts.Data) <= 1 {
			return nil
		}
		pt.ColFields = &xlsxColFields{}
		// in order to create pivot table in case there is no input from Columns
		pt.ColFields.Count = 1
		pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
			X: -2,
		})
		return nil
	}

	pt.ColFields = &xlsxColFields{}

	// col fields
	colFieldsIndex, err := f.getPivotFieldsIndex(opts.Columns, opts)
	if err != nil {
		return err
	}
	for _, fieldIdx := range colFieldsIndex {
		pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
			X: fieldIdx, // here is user defined colFields always before DATA colField (-2) and no opportunity to change order
		})
	}

	// in order to create pivot in case there is many Columns and Data
	if len(opts.Data) > 1 {
		pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
			X: -2, // here is -2 always after user defined fields and no opportunity to change order 
		})
	} 

	// count col fields
	pt.ColFields.Count = len(pt.ColFields.Field)
	return err
}

In excelize.PivotTableOptions no way to change order of such colField as Data(-2)

xml
  <colFields count="2">
     <field x="15"></field>
        <field x="-2"></field> // this always after my defined column with 15 idx
    </colFields>

Could u please, dear friend add some option to change order of such fields some way ?

Additional context

No response

Validations

  • Check that there isn't already an issue that requests the same feature to avoid creating a duplicate.