Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit e96f423

Browse files
authored
Merge pull request #839 from Sajito/fix/global-defined-names
Change LocalSheetID to pointer of int
2 parents 233ab0f + 077caea commit e96f423

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func autoFilterDefinedName(sheet *Sheet, sheetIndex int) (*xlsxDefinedName, erro
326326
cellIDStringWithFixed(sheet.AutoFilter.BottomRightCell),
327327
),
328328
Name: "_xlnm._FilterDatabase",
329-
LocalSheetID: sheetIndex - 1,
329+
LocalSheetID: iPtr(sheetIndex - 1),
330330
Hidden: true,
331331
}, nil
332332
}

file_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,27 @@ func TestFile(t *testing.T) {
989989
c.Assert(parts["xl/worksheets/sheet1.xml"], qt.Contains, `<autoFilter ref="A1:D"></autoFilter>`)
990990
})
991991

992+
csRunO(c, "TestSaveFileWithGlobalDefinedNames", func(c *qt.C, option FileOption) {
993+
f := NewFile(option)
994+
f.AddDefinedName(DefinedName{
995+
Name: "global",
996+
Data: "MySheet!$A$1",
997+
})
998+
f.AddDefinedName(DefinedName{
999+
Name: "local",
1000+
Data: "MySheet!$A$1",
1001+
LocalSheetID: iPtr(0),
1002+
})
1003+
1004+
sheet, _ := f.AddSheet("MySheet")
1005+
row1 := sheet.AddRow()
1006+
row1.AddCell().SetValue("Cell value")
1007+
1008+
parts, err := f.MakeStreamParts()
1009+
c.Assert(err, qt.IsNil)
1010+
c.Assert(parts["xl/workbook.xml"], qt.Contains, `<definedNames><definedName name="global">MySheet!$A$1</definedName><definedName name="local" localSheetId="0">MySheet!$A$1</definedName></definedNames>`)
1011+
})
1012+
9921013
// We can save a File as a valid XLSX file at a given path.
9931014
csRunO(c, "TestSaveFileWithHyperlinks", func(c *qt.C, option FileOption) {
9941015
tmpPath, err := os.MkdirTemp("", "testsavefilewithhyperlinks")

utility.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ func bPtr(b bool) *bool {
1616
func u8Ptr(u uint8) *uint8 {
1717
return &u
1818
}
19+
20+
func iPtr(i int) *int {
21+
return &i
22+
}

xmlWorkbook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type xlsxDefinedName struct {
141141
Help string `xml:"help,attr,omitempty"`
142142
ShortcutKey string `xml:"shortcutKey,attr,omitempty"`
143143
StatusBar string `xml:"statusBar,attr,omitempty"`
144-
LocalSheetID int `xml:"localSheetId,attr"`
144+
LocalSheetID *int `xml:"localSheetId,attr"`
145145
FunctionGroupID int `xml:"functionGroupId,attr,omitempty"`
146146
Function bool `xml:"function,attr,omitempty"`
147147
Hidden bool `xml:"hidden,attr,omitempty"`

xmlWorkbook_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func TestUnmarshallWorkbookXML(t *testing.T) {
4848
<definedName name="monitors" comment="this is the comment"
4949
description="give cells a name"
5050
localSheetId="0">Sheet1!$A$1533</definedName>
51+
<definedName name="global" comment="this is the comment"
52+
description="a global defined name">Sheet1!$A$1533</definedName>
5153
</definedNames>
5254
<calcPr calcId="125725"/>
5355
</workbook>`)
@@ -72,14 +74,21 @@ func TestUnmarshallWorkbookXML(t *testing.T) {
7274
c.Assert(sheet.Name, qt.Equals, "Sheet1")
7375
c.Assert(sheet.SheetId, qt.Equals, "1")
7476
c.Assert(sheet.State, qt.Equals, "visible")
75-
c.Assert(workbook.DefinedNames.DefinedName, qt.HasLen, 1)
77+
c.Assert(workbook.DefinedNames.DefinedName, qt.HasLen, 2)
7678
dname := workbook.DefinedNames.DefinedName[0]
7779
c.Assert(dname.Data, qt.Equals, "Sheet1!$A$1533")
78-
c.Assert(dname.LocalSheetID, qt.Equals, 0)
80+
c.Assert(*dname.LocalSheetID, qt.Equals, 0)
7981
c.Assert(dname.Name, qt.Equals, "monitors")
8082
c.Assert(dname.Comment, qt.Equals, "this is the comment")
8183
c.Assert(dname.Description, qt.Equals, "give cells a name")
8284
c.Assert(workbook.CalcPr.CalcId, qt.Equals, "125725")
85+
dname2 := workbook.DefinedNames.DefinedName[1]
86+
c.Assert(dname2.Data, qt.Equals, "Sheet1!$A$1533")
87+
c.Assert(dname2.LocalSheetID, qt.Equals, (*int)(nil))
88+
c.Assert(dname2.Name, qt.Equals, "global")
89+
c.Assert(dname2.Comment, qt.Equals, "this is the comment")
90+
c.Assert(dname2.Description, qt.Equals, "a global defined name")
91+
c.Assert(workbook.CalcPr.CalcId, qt.Equals, "125725")
8392
}
8493

8594
// Test we can marshall a Workbook to xml

0 commit comments

Comments
 (0)