avoid duplicate filter database in workbook defined name

This commit is contained in:
yuemanxilou 2020-05-15 14:03:02 +08:00 committed by GitHub
parent 9baa1bbc98
commit c815e4b84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 11 deletions

View File

@ -281,28 +281,38 @@ func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
formatSet, _ := parseAutoFilterSet(format)
var cellStart, cellEnd string
cellStart, err = CoordinatesToCellName(hcol, hrow)
if err != nil {
if cellStart, err = CoordinatesToCellName(hcol, hrow); err != nil {
return err
}
cellEnd, err = CoordinatesToCellName(vcol, vrow)
if err != nil {
if cellEnd, err = CoordinatesToCellName(vcol, vrow); err != nil {
return err
}
ref := cellStart + ":" + cellEnd
ref, filterDB := cellStart+":"+cellEnd, "_xlnm._FilterDatabase"
wb := f.workbookReader()
sheetID := f.GetSheetIndex(sheet)
filterRange := fmt.Sprintf("%s!%s", sheet, ref)
d := xlsxDefinedName{
Name: "_xlnm._FilterDatabase",
Name: filterDB,
Hidden: true,
LocalSheetID: intPtr(f.GetSheetIndex(sheet)),
Data: fmt.Sprintf("%s!%s", sheet, ref),
LocalSheetID: intPtr(sheetID),
Data: filterRange,
}
if wb.DefinedNames != nil {
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d)
} else {
if wb.DefinedNames == nil {
wb.DefinedNames = &xlsxDefinedNames{
DefinedName: []xlsxDefinedName{d},
}
} else {
var definedNameExists bool
for idx := range wb.DefinedNames.DefinedName {
definedName := wb.DefinedNames.DefinedName[idx]
if definedName.Name == filterDB && *definedName.LocalSheetID == sheetID && definedName.Hidden {
wb.DefinedNames.DefinedName[idx].Data = filterRange
definedNameExists = true
}
}
if !definedNameExists {
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d)
}
}
refRange := vcol - hcol
return f.autoFilter(sheet, ref, refRange, hcol, formatSet)