Support set font style of shape text. Relate issue #38.

This commit is contained in:
Ri Xu 2017-05-16 20:42:01 +08:00
parent cf97118bfe
commit 70f6328150
7 changed files with 96 additions and 61 deletions

View File

@ -252,7 +252,7 @@ func (f *File) addChart(formatSet *formatChart) {
Rich: &cRich{ Rich: &cRich{
P: aP{ P: aP{
PPr: &aPPr{ PPr: &aPPr{
DefRPr: aDefRPr{ DefRPr: aRPr{
Kern: 1200, Kern: 1200,
Strike: "noStrike", Strike: "noStrike",
U: "none", U: "none",
@ -292,7 +292,7 @@ func (f *File) addChart(formatSet *formatChart) {
TxPr: cTxPr{ TxPr: cTxPr{
P: aP{ P: aP{
PPr: &aPPr{ PPr: &aPPr{
DefRPr: aDefRPr{ DefRPr: aRPr{
Kern: 1200, Kern: 1200,
U: "none", U: "none",
Sz: 14000, Sz: 14000,
@ -812,7 +812,7 @@ func (f *File) drawPlotAreaTxPr() *cTxPr {
}, },
P: aP{ P: aP{
PPr: &aPPr{ PPr: &aPPr{
DefRPr: aDefRPr{ DefRPr: aRPr{
Sz: 900, Sz: 900,
B: false, B: false,
I: false, I: false,

View File

@ -158,7 +158,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
} }
comments := xlsxComments{ comments := xlsxComments{
Authors: []xlsxAuthor{ Authors: []xlsxAuthor{
xlsxAuthor{ {
Author: formatSet.Author, Author: formatSet.Author,
}, },
}, },
@ -168,7 +168,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
AuthorID: 0, AuthorID: 0,
Text: xlsxText{ Text: xlsxText{
R: []xlsxR{ R: []xlsxR{
xlsxR{ {
RPr: &xlsxRPr{ RPr: &xlsxRPr{
B: " ", B: " ",
Sz: &attrValInt{Val: 9}, Sz: &attrValInt{Val: 9},
@ -180,7 +180,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
}, },
T: a, T: a,
}, },
xlsxR{ {
RPr: &xlsxRPr{ RPr: &xlsxRPr{
Sz: &attrValInt{Val: 9}, Sz: &attrValInt{Val: 9},
Color: &xlsxColor{ Color: &xlsxColor{

View File

@ -517,7 +517,7 @@ func TestAddShape(t *testing.T) {
t.Log(err) t.Log(err)
} }
xlsx.AddShape("Sheet1", "A30", `{"type":"rect","text":"Rectangle Shape"}`) xlsx.AddShape("Sheet1", "A30", `{"type":"rect","text":"Rectangle Shape"}`)
xlsx.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "height": 90}`) xlsx.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"single"}, "height": 90}`)
err = xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)

View File

@ -22,6 +22,14 @@ func parseFormatShapeSet(formatSet string) *formatShape {
XScale: 1.0, XScale: 1.0,
YScale: 1.0, YScale: 1.0,
}, },
Font: formatFont{
Bold: false,
Italic: false,
Underline: "none",
Family: "Calibri",
Size: 11,
Color: "#000000",
},
Text: " ", Text: " ",
} }
json.Unmarshal([]byte(formatSet), &format) json.Unmarshal([]byte(formatSet), &format)
@ -33,7 +41,7 @@ func parseFormatShapeSet(formatSet string) *formatShape {
// print settings) and properties set. For example, add text box (rect shape) in // print settings) and properties set. For example, add text box (rect shape) in
// Sheet1: // Sheet1:
// //
// xlsx.AddShape("Sheet1", "G6", `{"type":"rect", "text":"Rectangle Shape", "color":{"line":"#4286F4","fill":"#8eb9ff"}, "width": 180, "height": 90}`) // xlsx.AddShape("Sheet1", "G6", `{"type":"rect", "text":"Rectangle Shape", "color":{"line":"#4286F4","fill":"#8eb9ff"}, "font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"sng"}, "width": 180, "height": 90}`)
// //
// The following shows the type of chart supported by excelize: // The following shows the type of chart supported by excelize:
// //
@ -225,6 +233,27 @@ func parseFormatShapeSet(formatSet string) *formatShape {
// wedgeRectCallout (Callout Wedge Rectangle Shape) // wedgeRectCallout (Callout Wedge Rectangle Shape)
// wedgeRoundRectCallout (Callout Wedge Round Rectangle Shape) // wedgeRoundRectCallout (Callout Wedge Round Rectangle Shape)
// //
// The following shows the type of text underline supported by excelize:
//
// none
// words
// sng
// dbl
// heavy
// dotted
// dottedHeavy
// dash
// dashHeavy
// dashLong
// dashLongHeavy
// dotDash
// dotDashHeavy
// dotDotDash
// dotDotDashHeavy
// wavy
// wavyHeavy
// wavyDbl
//
func (f *File) AddShape(sheet, cell, format string) { func (f *File) AddShape(sheet, cell, format string) {
formatSet := parseFormatShapeSet(format) formatSet := parseFormatShapeSet(format)
// Read sheet data. // Read sheet data.
@ -250,6 +279,12 @@ func (f *File) AddShape(sheet, cell, format string) {
// addDrawingShape // addDrawingShape
func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *formatShape) { func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *formatShape) {
textUnderlineType := map[string]bool{"none": true, "words": true, "sng": true, "dbl": true, "heavy": true, "dotted": true, "dottedHeavy": true, "dash": true, "dashHeavy": true, "dashLong": true, "dashLongHeavy": true, "dotDash": true, "dotDashHeavy": true, "dotDotDash": true, "dotDotDashHeavy": true, "wavy": true, "wavyHeavy": true, "wavyDbl": true}
u := formatSet.Font.Underline
_, ok := textUnderlineType[u]
if !ok {
u = "none"
}
cell = strings.ToUpper(cell) cell = strings.ToUpper(cell)
fromCol := string(strings.Map(letterOnlyMapF, cell)) fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
@ -262,7 +297,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
content.A = NameSpaceDrawingML content.A = NameSpaceDrawingML
content.Xdr = NameSpaceDrawingMLSpreadSheet content.Xdr = NameSpaceDrawingMLSpreadSheet
cNvPrID := 1 cNvPrID := 1
_, ok := f.XLSX[drawingXML] _, ok = f.XLSX[drawingXML]
if ok { // Append Model if ok { // Append Model
decodeWsDr := decodeWsDr{} decodeWsDr := decodeWsDr{}
xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr) xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
@ -331,9 +366,18 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
P: &aP{ P: &aP{
R: &aR{ R: &aR{
RPr: aRPr{ RPr: aRPr{
I: formatSet.Font.Italic,
B: formatSet.Font.Bold,
Lang: "en-US", Lang: "en-US",
AltLang: "en-US", AltLang: "en-US",
Sz: 1100, U: u,
Sz: formatSet.Font.Size * 100,
Latin: &aLatin{Typeface: formatSet.Font.Family},
SolidFill: &aSolidFill{
SrgbClr: &attrValString{
Val: strings.Replace(strings.ToUpper(formatSet.Font.Color), "#", "", -1),
},
},
}, },
T: formatSet.Text, T: formatSet.Text,
}, },

View File

@ -108,44 +108,15 @@ type aP struct {
// formatting, since they are directly applied to the paragraph and supersede // formatting, since they are directly applied to the paragraph and supersede
// any formatting from styles. // any formatting from styles.
type aPPr struct { type aPPr struct {
DefRPr aDefRPr `xml:"a:defRPr"` DefRPr aRPr `xml:"a:defRPr"`
}
// aDefRPr directly maps the a:defRPr element. This element contains all
// default run level text properties for the text runs within a containing
// paragraph. These properties are to be used when overriding properties have
// not been defined within the rPr element.
type aDefRPr struct {
AltLang string `xml:"altLang,attr,omitempty"`
B bool `xml:"b,attr"`
Baseline int `xml:"baseline,attr"`
Bmk string `xml:"bmk,attr,omitempty"`
Cap string `xml:"cap,attr,omitempty"`
Dirty bool `xml:"dirty,attr,omitempty"`
Err bool `xml:"err,attr,omitempty"`
I bool `xml:"i,attr"`
Kern int `xml:"kern,attr"`
Kumimoji bool `xml:"kumimoji,attr,omitempty"`
Lang string `xml:"lang,attr,omitempty"`
NoProof bool `xml:"noProof,attr,omitempty"`
NormalizeH bool `xml:"normalizeH,attr,omitempty"`
SmtClean bool `xml:"smtClean,attr,omitempty"`
SmtID uint64 `xml:"smtId,attr,omitempty"`
Spc int `xml:"spc,attr"`
Strike string `xml:"strike,attr,omitempty"`
Sz int `xml:"sz,attr"`
U string `xml:"u,attr,omitempty"`
SolidFill *aSolidFill `xml:"a:solidFill"`
Latin *aLatin `xml:"a:latin"`
Ea *aEa `xml:"a:ea"`
Cs *aCs `xml:"a:cs"`
} }
// aSolidFill (Solid Fill) directly maps the solidFill element. This element // aSolidFill (Solid Fill) directly maps the solidFill element. This element
// specifies a solid color fill. The shape is filled entirely with the specified // specifies a solid color fill. The shape is filled entirely with the specified
// color. // color.
type aSolidFill struct { type aSolidFill struct {
SchemeClr *aSchemeClr `xml:"a:schemeClr,omitempty"` SchemeClr *aSchemeClr `xml:"a:schemeClr"`
SrgbClr *attrValString `xml:"a:srgbClr"`
} }
// aSchemeClr (Scheme Color) directly maps the a:schemeClr element. This // aSchemeClr (Scheme Color) directly maps the a:schemeClr element. This
@ -206,9 +177,29 @@ type aR struct {
// properties are defined as direct formatting, since they are directly applied // properties are defined as direct formatting, since they are directly applied
// to the run and supersede any formatting from styles. // to the run and supersede any formatting from styles.
type aRPr struct { type aRPr struct {
Lang string `xml:"lang,attr,omitempty"` AltLang string `xml:"altLang,attr,omitempty"`
AltLang string `xml:"altLang,attr,omitempty"` B bool `xml:"b,attr"`
Sz int `xml:"sz,attr,omitempty"` Baseline int `xml:"baseline,attr"`
Bmk string `xml:"bmk,attr,omitempty"`
Cap string `xml:"cap,attr,omitempty"`
Dirty bool `xml:"dirty,attr,omitempty"`
Err bool `xml:"err,attr,omitempty"`
I bool `xml:"i,attr"`
Kern int `xml:"kern,attr"`
Kumimoji bool `xml:"kumimoji,attr,omitempty"`
Lang string `xml:"lang,attr,omitempty"`
NoProof bool `xml:"noProof,attr,omitempty"`
NormalizeH bool `xml:"normalizeH,attr,omitempty"`
SmtClean bool `xml:"smtClean,attr,omitempty"`
SmtID uint64 `xml:"smtId,attr,omitempty"`
Spc int `xml:"spc,attr"`
Strike string `xml:"strike,attr,omitempty"`
Sz int `xml:"sz,attr,omitempty"`
U string `xml:"u,attr,omitempty"`
SolidFill *aSolidFill `xml:"a:solidFill"`
Latin *aLatin `xml:"a:latin"`
Ea *aEa `xml:"a:ea"`
Cs *aCs `xml:"a:cs"`
} }
// cSpPr (Shape Properties) directly maps the c:spPr element. This element // cSpPr (Shape Properties) directly maps the c:spPr element. This element
@ -579,14 +570,10 @@ type formatChart struct {
// formatChartLegend directly maps the format settings of the chart legend. // formatChartLegend directly maps the format settings of the chart legend.
type formatChartLegend struct { type formatChartLegend struct {
None bool `json:"none"` None bool `json:"none"`
DeleteSeries []int `json:"delete_series"` DeleteSeries []int `json:"delete_series"`
Font struct { Font formatFont `json:"font"`
Size int `json:"size"` Layout struct {
Blod bool `json:"blod"`
Italic bool `json:"italic"`
} `json:"font"`
Layout struct {
X float64 `json:"x"` X float64 `json:"x"`
Y float64 `json:"y"` Y float64 `json:"y"`
Width float64 `json:"width"` Width float64 `json:"width"`

View File

@ -349,6 +349,7 @@ type formatShape struct {
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Format formatPicture `json:"format"` Format formatPicture `json:"format"`
Font formatFont `json:"font"`
Text string `json:"text"` Text string `json:"text"`
Color formatShapeColor `json:"color"` Color formatShapeColor `json:"color"`
} }

View File

@ -282,6 +282,16 @@ type xlsxStyleColors struct {
Color string `xml:",innerxml"` Color string `xml:",innerxml"`
} }
// formatFont directly maps the styles settings of the fonts.
type formatFont struct {
Bold bool `json:"bold"`
Italic bool `json:"italic"`
Underline string `json:"underline"`
Family string `json:"family"`
Size int `json:"size"`
Color string `json:"color"`
}
// formatCellStyle directly maps the styles settings of the borders. // formatCellStyle directly maps the styles settings of the borders.
type formatCellStyle struct { type formatCellStyle struct {
Border []struct { Border []struct {
@ -295,14 +305,7 @@ type formatCellStyle struct {
Color []string `json:"color"` Color []string `json:"color"`
Shading int `json:"shading"` Shading int `json:"shading"`
} `json:"fill"` } `json:"fill"`
Font *struct { Font *formatFont `json:"font"`
Bold bool `json:"bold"`
Italic bool `json:"italic"`
Underline string `json:"underline"`
Family string `json:"family"`
Size int `json:"size"`
Color string `json:"color"`
} `json:"font"`
Alignment *struct { Alignment *struct {
Horizontal string `json:"horizontal"` Horizontal string `json:"horizontal"`
Indent int `json:"indent"` Indent int `json:"indent"`