qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets

Home Page:https://xuri.me/excelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty columns within the source data range of the pivot table, Excel cannot be opened after setting the pivot table

ShowerBandV opened this issue · comments

Description
There are empty columns within the source data range of the pivot table. After setting the pivot table, Excel cannot be opened. After decompressing the Excel file, it was found that the empty cacheField field in pivotCacheDefinition.xml was the cause. Reading the code reveals that it is empty because pc.CacheFields.CacheField.Name directly reads the cell value as empty. Should pc.CacheFields.CacheField.Name be given a default value when the cell value is empty, for example, in the source data of A1~E20, column C is a blank column, and pcCacheFieldsCacheFieldName defaults to column C instead of an empty string

func (f *File) getTableFieldsOrder(opts *PivotTableOptions) ([]string, error) {
	var order []string
	if err := f.getPivotTableDataRange(opts); err != nil {
		return order, err
	}
	dataSheet, coordinates, err := f.adjustRange(opts.pivotDataRange)
	if err != nil {
		return order, newPivotTableDataRangeError(err.Error())
	}
	for col := coordinates[0]; col <= coordinates[2]; col++ {
                //here
		coordinate, _ := CoordinatesToCellName(col, coordinates[1])
		name, err := f.GetCellValue(dataSheet, coordinate)
		if err != nil {
			return order, err
		}
		order = append(order, name)
	}
	return order, nil
}
commented

Thanks for your issue. Excel does't allow column header cell is empty in pivot table data range. I added check in the AddPivotTable function, now it will be return an error for this. Please upgrade to the master branch, and this patch will be released in the next version.