diff options
author | xuri <xuri.me@gmail.com> | 2019-05-24 09:48:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-24 09:48:14 +0800 |
commit | 623375780586cbd323dc899e17d7235ac610505f (patch) | |
tree | 25a44aa230fa674960dd5aa45b857b0a45e0c85b | |
parent | f91f548614a7182ce66d55d10ed311e9b7e08a2a (diff) | |
parent | b1c9884f6d186bd1bfb4fc1d34061856345b8530 (diff) |
Merge pull request #391 from mlh758/390-change-default-font
Add the ability to change the default font
-rw-r--r-- | comment.go | 5 | ||||
-rw-r--r-- | shape.go | 2 | ||||
-rw-r--r-- | styles.go | 40 | ||||
-rw-r--r-- | styles_test.go | 30 | ||||
-rw-r--r-- | templates.go | 2 |
5 files changed, 65 insertions, 14 deletions
@@ -239,6 +239,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { }, } } + defaultFont := f.GetDefaultFont() cmt := xlsxComment{ Ref: cell, AuthorID: 0, @@ -251,7 +252,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, + RFont: &attrValString{Val: defaultFont}, Family: &attrValInt{Val: 2}, }, T: a, @@ -262,7 +263,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, + RFont: &attrValString{Val: defaultFont}, Family: &attrValInt{Val: 2}, }, T: t, @@ -381,7 +381,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format Bold: false, Italic: false, Underline: "none", - Family: "Calibri", + Family: f.GetDefaultFont(), Size: 11, Color: "#000000", }, @@ -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 diff --git a/styles_test.go b/styles_test.go index 54321bb..decfbb9 100644 --- a/styles_test.go +++ b/styles_test.go @@ -25,7 +25,7 @@ func TestStyleFill(t *testing.T) { xl := NewFile() styleID, err := xl.NewStyle(testCase.format) if err != nil { - t.Fatalf("%v", err) + t.Fatal(err) } styles := xl.stylesReader() @@ -165,3 +165,31 @@ func TestSetConditionalFormat(t *testing.T) { assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label) } } + +func TestNewStyle(t *testing.T) { + f := NewFile() + styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777"}}`) + if err != nil { + t.Fatal(err) + } + styles := f.stylesReader() + fontID := styles.CellXfs.Xf[styleID].FontID + font := styles.Fonts.Font[fontID] + assert.Contains(t, font.Name.Val, "Berlin Sans FB Demi", "Stored font should contain font name") + assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles") +} + +func TestGetDefaultFont(t *testing.T) { + f := NewFile() + s := f.GetDefaultFont() + assert.Equal(t, s, "Calibri", "Default font should be Calibri") +} + +func TestSetDefaultFont(t *testing.T) { + f := NewFile() + f.SetDefaultFont("Ariel") + styles := f.stylesReader() + s := f.GetDefaultFont() + assert.Equal(t, s, "Ariel", "Default font should change to Ariel") + assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true) +} diff --git a/templates.go b/templates.go index 17fc8d4..923cebd 100644 --- a/templates.go +++ b/templates.go @@ -27,7 +27,7 @@ const templateContentTypes = `<Types xmlns="http://schemas.openxmlformats.org/pa const templateWorkbook = `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"><fileVersion appName="xl" lastEdited="6" lowestEdited="6" rupBuild="14420" /><workbookPr filterPrivacy="1" defaultThemeVersion="164011" /><bookViews><workbookView xWindow="0" yWindow="0" windowWidth="14805" windowHeight="8010" /></bookViews><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1" /></sheets><calcPr calcId="122211" /></workbook>` -const templateStyles = `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main"><fonts count="1" x14ac:knownFonts="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs><cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/></styleSheet>` +const templateStyles = `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main"><fonts count="1" x14ac:knownFonts="1"><font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/></font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs><cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/></styleSheet>` const templateSheet = `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="15"/><sheetData/><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>` |