From f91f548614a7182ce66d55d10ed311e9b7e08a2a Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 17 May 2019 22:58:12 +0800 Subject: [PATCH] Resolve #404, get sheet map by target rels. --- sheet.go | 16 +++++++++++----- styles.go | 9 +++------ xmlStyles.go | 13 ++++--------- xmlWorkbook.go | 5 ++--- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/sheet.go b/sheet.go index b22592d..e873118 100644 --- a/sheet.go +++ b/sheet.go @@ -369,12 +369,18 @@ func (f *File) GetSheetMap() map[int]string { return sheetMap } -// getSheetMap provides a function to get worksheet name and XML file path map of -// XLSX. +// getSheetMap provides a function to get worksheet name and XML file path map +// of XLSX. func (f *File) getSheetMap() map[string]string { - maps := make(map[string]string) - for idx, name := range f.GetSheetMap() { - maps[name] = "xl/worksheets/sheet" + strconv.Itoa(idx) + ".xml" + content := f.workbookReader() + rels := f.workbookRelsReader() + maps := map[string]string{} + for _, v := range content.Sheets.Sheet { + for _, rel := range rels.Relationships { + if rel.ID == v.ID { + maps[v.Name] = fmt.Sprintf("xl/%s", rel.Target) + } + } } return maps } diff --git a/styles.go b/styles.go index d6d267d..fc8a290 100644 --- a/styles.go +++ b/styles.go @@ -1895,11 +1895,8 @@ func (f *File) NewStyle(style string) (int, error) { numFmtID := setNumFmt(s, fs) if fs.Font != nil { - font, _ := xml.Marshal(setFont(fs)) s.Fonts.Count++ - s.Fonts.Font = append(s.Fonts.Font, &xlsxFont{ - Font: string(font[6 : len(font)-7]), - }) + s.Fonts.Font = append(s.Fonts.Font, setFont(fs)) fontID = s.Fonts.Count - 1 } @@ -1950,7 +1947,7 @@ func (f *File) NewConditionalStyle(style string) (int, error) { // setFont provides a function to add font style by given cell format // settings. -func setFont(formatStyle *formatStyle) *font { +func setFont(formatStyle *formatStyle) *xlsxFont { fontUnderlineType := map[string]string{"single": "single", "double": "double"} if formatStyle.Font.Size < 1 { formatStyle.Font.Size = 11 @@ -1958,7 +1955,7 @@ func setFont(formatStyle *formatStyle) *font { if formatStyle.Font.Color == "" { formatStyle.Font.Color = "#000000" } - f := font{ + f := xlsxFont{ B: formatStyle.Font.Bold, I: formatStyle.Font.Italic, Sz: &attrValInt{Val: formatStyle.Font.Size}, diff --git a/xmlStyles.go b/xmlStyles.go index fc53f77..5c198e7 100644 --- a/xmlStyles.go +++ b/xmlStyles.go @@ -82,8 +82,9 @@ type xlsxFonts struct { Font []*xlsxFont `xml:"font"` } -// font directly maps the font element. -type font struct { +// xlsxFont directly maps the font element. This element defines the +// properties for one of the fonts used in this workbook. +type xlsxFont struct { Name *attrValString `xml:"name"` Charset *attrValInt `xml:"charset"` Family *attrValInt `xml:"family"` @@ -100,12 +101,6 @@ type font struct { Scheme *attrValString `xml:"scheme"` } -// xlsxFont directly maps the font element. This element defines the properties -// for one of the fonts used in this workbook. -type xlsxFont struct { - Font string `xml:",innerxml"` -} - // xlsxFills directly maps the fills element. This element defines the cell // fills portion of the Styles part, consisting of a sequence of fill records. A // cell fill consists of a background color, foreground color, and pattern to be @@ -262,7 +257,7 @@ type xlsxDxf struct { // dxf directly maps the dxf element. type dxf struct { - Font *font `xml:"font"` + Font *xlsxFont `xml:"font"` NumFmt *xlsxNumFmt `xml:"numFmt"` Fill *xlsxFill `xml:"fill"` Alignment *xlsxAlignment `xml:"alignment"` diff --git a/xmlWorkbook.go b/xmlWorkbook.go index ad66f42..90a1427 100644 --- a/xmlWorkbook.go +++ b/xmlWorkbook.go @@ -146,9 +146,8 @@ type xlsxSheets struct { Sheet []xlsxSheet `xml:"sheet"` } -// xlsxSheet directly maps the sheet element from the namespace -// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have -// not checked it for completeness - it does as much as I need. +// xlsxSheet defines a sheet in this workbook. Sheet data is stored in a +// separate part. type xlsxSheet struct { Name string `xml:"name,attr,omitempty"` SheetID int `xml:"sheetId,attr,omitempty"`