From 077caea4e922a79e3bd7741b4f26d5e85df37954 Mon Sep 17 00:00:00 2001 From: Gigino Chianese Date: Tue, 3 Dec 2024 10:47:38 +0100 Subject: [PATCH] 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. --- file.go | 2 +- file_test.go | 21 +++++++++++++++++++++ utility.go | 4 ++++ xmlWorkbook.go | 2 +- xmlWorkbook_test.go | 13 +++++++++++-- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/file.go b/file.go index b09449ab..b9c9e294 100644 --- a/file.go +++ b/file.go @@ -326,7 +326,7 @@ func autoFilterDefinedName(sheet *Sheet, sheetIndex int) (*xlsxDefinedName, erro cellIDStringWithFixed(sheet.AutoFilter.BottomRightCell), ), Name: "_xlnm._FilterDatabase", - LocalSheetID: sheetIndex - 1, + LocalSheetID: iPtr(sheetIndex - 1), Hidden: true, }, nil } diff --git a/file_test.go b/file_test.go index e749f8a8..97a6a16c 100644 --- a/file_test.go +++ b/file_test.go @@ -967,6 +967,27 @@ func TestFile(t *testing.T) { c.Assert(parts["xl/worksheets/sheet1.xml"], qt.Contains, ``) }) + csRunO(c, "TestSaveFileWithGlobalDefinedNames", func(c *qt.C, option FileOption) { + f := NewFile(option) + f.AddDefinedName(DefinedName{ + Name: "global", + Data: "MySheet!$A$1", + }) + f.AddDefinedName(DefinedName{ + Name: "local", + Data: "MySheet!$A$1", + LocalSheetID: iPtr(0), + }) + + sheet, _ := f.AddSheet("MySheet") + row1 := sheet.AddRow() + row1.AddCell().SetValue("Cell value") + + parts, err := f.MakeStreamParts() + c.Assert(err, qt.IsNil) + c.Assert(parts["xl/workbook.xml"], qt.Contains, `MySheet!$A$1MySheet!$A$1`) + }) + // We can save a File as a valid XLSX file at a given path. csRunO(c, "TestSaveFileWithHyperlinks", func(c *qt.C, option FileOption) { tmpPath, err := os.MkdirTemp("", "testsavefilewithhyperlinks") diff --git a/utility.go b/utility.go index 11c9b0bf..3e7209c4 100644 --- a/utility.go +++ b/utility.go @@ -16,3 +16,7 @@ func bPtr(b bool) *bool { func u8Ptr(u uint8) *uint8 { return &u } + +func iPtr(i int) *int { + return &i +} diff --git a/xmlWorkbook.go b/xmlWorkbook.go index 2949f8b9..b4fd4d61 100644 --- a/xmlWorkbook.go +++ b/xmlWorkbook.go @@ -141,7 +141,7 @@ type xlsxDefinedName struct { Help string `xml:"help,attr,omitempty"` ShortcutKey string `xml:"shortcutKey,attr,omitempty"` StatusBar string `xml:"statusBar,attr,omitempty"` - LocalSheetID int `xml:"localSheetId,attr"` + LocalSheetID *int `xml:"localSheetId,attr"` FunctionGroupID int `xml:"functionGroupId,attr,omitempty"` Function bool `xml:"function,attr,omitempty"` Hidden bool `xml:"hidden,attr,omitempty"` diff --git a/xmlWorkbook_test.go b/xmlWorkbook_test.go index defe5fc9..c905098f 100644 --- a/xmlWorkbook_test.go +++ b/xmlWorkbook_test.go @@ -48,6 +48,8 @@ func TestUnmarshallWorkbookXML(t *testing.T) { Sheet1!$A$1533 + Sheet1!$A$1533 `) @@ -72,14 +74,21 @@ func TestUnmarshallWorkbookXML(t *testing.T) { c.Assert(sheet.Name, qt.Equals, "Sheet1") c.Assert(sheet.SheetId, qt.Equals, "1") c.Assert(sheet.State, qt.Equals, "visible") - c.Assert(workbook.DefinedNames.DefinedName, qt.HasLen, 1) + c.Assert(workbook.DefinedNames.DefinedName, qt.HasLen, 2) dname := workbook.DefinedNames.DefinedName[0] c.Assert(dname.Data, qt.Equals, "Sheet1!$A$1533") - c.Assert(dname.LocalSheetID, qt.Equals, 0) + c.Assert(*dname.LocalSheetID, qt.Equals, 0) c.Assert(dname.Name, qt.Equals, "monitors") c.Assert(dname.Comment, qt.Equals, "this is the comment") c.Assert(dname.Description, qt.Equals, "give cells a name") c.Assert(workbook.CalcPr.CalcId, qt.Equals, "125725") + dname2 := workbook.DefinedNames.DefinedName[1] + c.Assert(dname2.Data, qt.Equals, "Sheet1!$A$1533") + c.Assert(dname2.LocalSheetID, qt.Equals, (*int)(nil)) + c.Assert(dname2.Name, qt.Equals, "global") + c.Assert(dname2.Comment, qt.Equals, "this is the comment") + c.Assert(dname2.Description, qt.Equals, "a global defined name") + c.Assert(workbook.CalcPr.CalcId, qt.Equals, "125725") } // Test we can marshall a Workbook to xml