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

Commit 077caea

Browse files
committed
Change LocalSheetID to pointer of int
Defined names can apply globally (i.e. for every sheet in the document). Global defined names have no LocalSheetID attached to them. LocalSheetID=0 means that the defined name only applies to the first sheet. By changing the type to `*int` the information will be preserved correctly.
1 parent 6c6b048 commit 077caea

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
@@ -967,6 +967,27 @@ func TestFile(t *testing.T) {
967967
c.Assert(parts["xl/worksheets/sheet1.xml"], qt.Contains, `<autoFilter ref="A1:D"></autoFilter>`)
968968
})
969969

970+
csRunO(c, "TestSaveFileWithGlobalDefinedNames", func(c *qt.C, option FileOption) {
971+
f := NewFile(option)
972+
f.AddDefinedName(DefinedName{
973+
Name: "global",
974+
Data: "MySheet!$A$1",
975+
})
976+
f.AddDefinedName(DefinedName{
977+
Name: "local",
978+
Data: "MySheet!$A$1",
979+
LocalSheetID: iPtr(0),
980+
})
981+
982+
sheet, _ := f.AddSheet("MySheet")
983+
row1 := sheet.AddRow()
984+
row1.AddCell().SetValue("Cell value")
985+
986+
parts, err := f.MakeStreamParts()
987+
c.Assert(err, qt.IsNil)
988+
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>`)
989+
})
990+
970991
// We can save a File as a valid XLSX file at a given path.
971992
csRunO(c, "TestSaveFileWithHyperlinks", func(c *qt.C, option FileOption) {
972993
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)