This fixes #1645 and fixes #1655

- Breaking changes, change the data type for the `HeaderFooterOptions` structure fields `AlignWithMargins` and `ScaleWithDoc` as a pointer
- Fixed panic on `AutoFilter` by adding nil pointer guard for local sheet ID
- Allow dot character in the defined name, table name, or pivot table name
- Update the unit tests
This commit is contained in:
xuri 2023-09-09 13:51:00 +08:00
parent a0a7d5cdbb
commit 49706c9018
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
4 changed files with 14 additions and 7 deletions

View File

@ -266,7 +266,7 @@ func TestSetHeaderFooter(t *testing.T) {
func TestDefinedName(t *testing.T) {
f := NewFile()
assert.NoError(t, f.SetDefinedName(&DefinedName{
Name: "Amount",
Name: "Amount.",
RefersTo: "Sheet1!$A$2:$D$5",
Comment: "defined name comment",
Scope: "Sheet1",

View File

@ -449,8 +449,11 @@ func (f *File) AutoFilter(sheet, rangeRef string, opts []AutoFilterOptions) erro
} else {
var definedNameExists bool
for idx := range wb.DefinedNames.DefinedName {
definedName := wb.DefinedNames.DefinedName[idx]
if definedName.Name == builtInDefinedNames[2] && *definedName.LocalSheetID == sheetID && definedName.Hidden {
definedName, localSheetID := wb.DefinedNames.DefinedName[idx], 0
if definedName.LocalSheetID != nil {
localSheetID = *definedName.LocalSheetID
}
if definedName.Name == builtInDefinedNames[2] && localSheetID == sheetID && definedName.Hidden {
wb.DefinedNames.DefinedName[idx].Data = filterRange
definedNameExists = true
}

View File

@ -156,6 +156,10 @@ func TestAutoFilter(t *testing.T) {
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.AutoFilter("Sheet1", "D4:B1", nil), "XML syntax error on line 1: invalid UTF-8")
// Test add auto filter with empty local sheet ID
f = NewFile()
f.WorkBook = &xlsxWorkbook{DefinedNames: &xlsxDefinedNames{DefinedName: []xlsxDefinedName{{Name: builtInDefinedNames[2], Hidden: true}}}}
assert.NoError(t, f.AutoFilter("Sheet1", "A1:B1", nil))
}
func TestAutoFilterError(t *testing.T) {

View File

@ -81,8 +81,8 @@ type xlsxHeaderFooter struct {
XMLName xml.Name `xml:"headerFooter"`
DifferentOddEven bool `xml:"differentOddEven,attr,omitempty"`
DifferentFirst bool `xml:"differentFirst,attr,omitempty"`
ScaleWithDoc bool `xml:"scaleWithDoc,attr,omitempty"`
AlignWithMargins bool `xml:"alignWithMargins,attr,omitempty"`
ScaleWithDoc *bool `xml:"scaleWithDoc,attr"`
AlignWithMargins *bool `xml:"alignWithMargins,attr"`
OddHeader string `xml:"oddHeader,omitempty"`
OddFooter string `xml:"oddFooter,omitempty"`
EvenHeader string `xml:"evenHeader,omitempty"`
@ -963,10 +963,10 @@ type SheetProtectionOptions struct {
// HeaderFooterOptions directly maps the settings of header and footer.
type HeaderFooterOptions struct {
AlignWithMargins bool
AlignWithMargins *bool
DifferentFirst bool
DifferentOddEven bool
ScaleWithDoc bool
ScaleWithDoc *bool
OddHeader string
OddFooter string
EvenHeader string