Skip to content

Commit f91f548

Browse files
committed
Resolve qax-os#404, get sheet map by target rels.
1 parent 7e77e14 commit f91f548

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

sheet.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,18 @@ func (f *File) GetSheetMap() map[int]string {
369369
return sheetMap
370370
}
371371

372-
// getSheetMap provides a function to get worksheet name and XML file path map of
373-
// XLSX.
372+
// getSheetMap provides a function to get worksheet name and XML file path map
373+
// of XLSX.
374374
func (f *File) getSheetMap() map[string]string {
375-
maps := make(map[string]string)
376-
for idx, name := range f.GetSheetMap() {
377-
maps[name] = "xl/worksheets/sheet" + strconv.Itoa(idx) + ".xml"
375+
content := f.workbookReader()
376+
rels := f.workbookRelsReader()
377+
maps := map[string]string{}
378+
for _, v := range content.Sheets.Sheet {
379+
for _, rel := range rels.Relationships {
380+
if rel.ID == v.ID {
381+
maps[v.Name] = fmt.Sprintf("xl/%s", rel.Target)
382+
}
383+
}
378384
}
379385
return maps
380386
}

styles.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,11 +1895,8 @@ func (f *File) NewStyle(style string) (int, error) {
18951895
numFmtID := setNumFmt(s, fs)
18961896

18971897
if fs.Font != nil {
1898-
font, _ := xml.Marshal(setFont(fs))
18991898
s.Fonts.Count++
1900-
s.Fonts.Font = append(s.Fonts.Font, &xlsxFont{
1901-
Font: string(font[6 : len(font)-7]),
1902-
})
1899+
s.Fonts.Font = append(s.Fonts.Font, setFont(fs))
19031900
fontID = s.Fonts.Count - 1
19041901
}
19051902

@@ -1950,15 +1947,15 @@ func (f *File) NewConditionalStyle(style string) (int, error) {
19501947

19511948
// setFont provides a function to add font style by given cell format
19521949
// settings.
1953-
func setFont(formatStyle *formatStyle) *font {
1950+
func setFont(formatStyle *formatStyle) *xlsxFont {
19541951
fontUnderlineType := map[string]string{"single": "single", "double": "double"}
19551952
if formatStyle.Font.Size < 1 {
19561953
formatStyle.Font.Size = 11
19571954
}
19581955
if formatStyle.Font.Color == "" {
19591956
formatStyle.Font.Color = "#000000"
19601957
}
1961-
f := font{
1958+
f := xlsxFont{
19621959
B: formatStyle.Font.Bold,
19631960
I: formatStyle.Font.Italic,
19641961
Sz: &attrValInt{Val: formatStyle.Font.Size},

xmlStyles.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ type xlsxFonts struct {
8282
Font []*xlsxFont `xml:"font"`
8383
}
8484

85-
// font directly maps the font element.
86-
type font struct {
85+
// xlsxFont directly maps the font element. This element defines the
86+
// properties for one of the fonts used in this workbook.
87+
type xlsxFont struct {
8788
Name *attrValString `xml:"name"`
8889
Charset *attrValInt `xml:"charset"`
8990
Family *attrValInt `xml:"family"`
@@ -100,12 +101,6 @@ type font struct {
100101
Scheme *attrValString `xml:"scheme"`
101102
}
102103

103-
// xlsxFont directly maps the font element. This element defines the properties
104-
// for one of the fonts used in this workbook.
105-
type xlsxFont struct {
106-
Font string `xml:",innerxml"`
107-
}
108-
109104
// xlsxFills directly maps the fills element. This element defines the cell
110105
// fills portion of the Styles part, consisting of a sequence of fill records. A
111106
// cell fill consists of a background color, foreground color, and pattern to be
@@ -262,7 +257,7 @@ type xlsxDxf struct {
262257

263258
// dxf directly maps the dxf element.
264259
type dxf struct {
265-
Font *font `xml:"font"`
260+
Font *xlsxFont `xml:"font"`
266261
NumFmt *xlsxNumFmt `xml:"numFmt"`
267262
Fill *xlsxFill `xml:"fill"`
268263
Alignment *xlsxAlignment `xml:"alignment"`

xmlWorkbook.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ type xlsxSheets struct {
146146
Sheet []xlsxSheet `xml:"sheet"`
147147
}
148148

149-
// xlsxSheet directly maps the sheet element from the namespace
150-
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
151-
// not checked it for completeness - it does as much as I need.
149+
// xlsxSheet defines a sheet in this workbook. Sheet data is stored in a
150+
// separate part.
152151
type xlsxSheet struct {
153152
Name string `xml:"name,attr,omitempty"`
154153
SheetID int `xml:"sheetId,attr,omitempty"`

0 commit comments

Comments
 (0)