diff options
author | Harris <mike.harris@cerner.com> | 2019-04-25 11:24:25 -0500 |
---|---|---|
committer | Harris <mike.harris@cerner.com> | 2019-05-23 17:01:30 -0500 |
commit | b1c9884f6d186bd1bfb4fc1d34061856345b8530 (patch) | |
tree | 25a44aa230fa674960dd5aa45b857b0a45e0c85b /styles.go | |
parent | f91f548614a7182ce66d55d10ed311e9b7e08a2a (diff) |
Add the ability to change the default font
Closes #390
Diffstat (limited to 'styles.go')
-rw-r--r-- | styles.go | 40 |
1 files changed, 31 insertions, 9 deletions
@@ -1896,7 +1896,7 @@ func (f *File) NewStyle(style string) (int, error) { if fs.Font != nil { s.Fonts.Count++ - s.Fonts.Font = append(s.Fonts.Font, setFont(fs)) + s.Fonts.Font = append(s.Fonts.Font, f.setFont(fs)) fontID = s.Fonts.Count - 1 } @@ -1932,7 +1932,7 @@ func (f *File) NewConditionalStyle(style string) (int, error) { Border: setBorders(fs), } if fs.Font != nil { - dxf.Font = setFont(fs) + dxf.Font = f.setFont(fs) } dxfStr, _ := xml.Marshal(dxf) if s.Dxfs == nil { @@ -1945,9 +1945,32 @@ func (f *File) NewConditionalStyle(style string) (int, error) { return s.Dxfs.Count - 1, nil } +// GetDefaultFont provides the default font name currently set in the workbook +// Documents generated by excelize start with Calibri +func (f *File) GetDefaultFont() string { + font := f.readDefaultFont() + return font.Name.Val +} + +// SetDefaultFont changes the default font in the workbook +func (f *File) SetDefaultFont(fontName string) { + font := f.readDefaultFont() + font.Name.Val = fontName + s := f.stylesReader() + s.Fonts.Font[0] = font + custom := true + s.CellStyles.CellStyle[0].CustomBuiltIn = &custom +} + +// readDefaultFont provides an unmarshalled font value +func (f *File) readDefaultFont() *xlsxFont { + s := f.stylesReader() + return s.Fonts.Font[0] +} + // setFont provides a function to add font style by given cell format // settings. -func setFont(formatStyle *formatStyle) *xlsxFont { +func (f *File) setFont(formatStyle *formatStyle) *xlsxFont { fontUnderlineType := map[string]string{"single": "single", "double": "double"} if formatStyle.Font.Size < 1 { formatStyle.Font.Size = 11 @@ -1955,7 +1978,7 @@ func setFont(formatStyle *formatStyle) *xlsxFont { if formatStyle.Font.Color == "" { formatStyle.Font.Color = "#000000" } - f := xlsxFont{ + fnt := xlsxFont{ B: formatStyle.Font.Bold, I: formatStyle.Font.Italic, Sz: &attrValInt{Val: formatStyle.Font.Size}, @@ -1963,15 +1986,14 @@ func setFont(formatStyle *formatStyle) *xlsxFont { Name: &attrValString{Val: formatStyle.Font.Family}, Family: &attrValInt{Val: 2}, } - if f.Name.Val == "" { - f.Name.Val = "Calibri" - f.Scheme = &attrValString{Val: "minor"} + if fnt.Name.Val == "" { + fnt.Name.Val = f.GetDefaultFont() } val, ok := fontUnderlineType[formatStyle.Font.Underline] if ok { - f.U = &attrValString{Val: val} + fnt.U = &attrValString{Val: val} } - return &f + return &fnt } // setNumFmt provides a function to check if number format code in the range |