From 7bc43301dabaef70b785133caf4fb4f1f978e089 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Mon, 19 Jun 2017 11:18:58 +0800 Subject: [PATCH] - Support insert new lines into shape, relate issue #38, note that the format set parameter of function `AddShape()` changed; - go test and go doc updated --- excelize_test.go | 6 ++-- shape.go | 81 ++++++++++++++++++++++++++++-------------------- xmlDrawing.go | 23 +++++++++----- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/excelize_test.go b/excelize_test.go index 93b6f91..77413ed 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -551,8 +551,10 @@ func TestAddShape(t *testing.T) { if err != nil { t.Log(err) } - xlsx.AddShape("Sheet1", "A30", `{"type":"rect","text":"Rectangle Shape"}`) - 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}`) + xlsx.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`) + xlsx.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`) + xlsx.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`) + xlsx.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"single"}}], "height": 90}`) err = xlsx.Save() if err != nil { t.Log(err) diff --git a/shape.go b/shape.go index 2923763..99ab291 100644 --- a/shape.go +++ b/shape.go @@ -22,15 +22,6 @@ func parseFormatShapeSet(formatSet string) *formatShape { XScale: 1.0, YScale: 1.0, }, - Font: formatFont{ - Bold: false, - Italic: false, - Underline: "none", - Family: "Calibri", - Size: 11, - Color: "#000000", - }, - Text: " ", } json.Unmarshal([]byte(formatSet), &format) return &format @@ -41,7 +32,7 @@ func parseFormatShapeSet(formatSet string) *formatShape { // print settings) and properties set. For example, add text box (rect shape) in // Sheet1: // -// 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}`) +// xlsx.AddShape("Sheet1", "G6", `{"type":"rect","color":{"line":"#4286F4","fill":"#8eb9ff"},"paragraph":[{"text":"Rectangle Shape","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: // @@ -281,11 +272,6 @@ func (f *File) AddShape(sheet, cell, format string) { // drawingXMLand format sets. 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) fromCol := string(strings.Map(letterOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) @@ -346,29 +332,56 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format RtlCol: false, Anchor: "t", }, - P: &aP{ - R: &aR{ - RPr: aRPr{ - I: formatSet.Font.Italic, - B: formatSet.Font.Bold, - Lang: "en-US", - AltLang: "en-US", - 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), - }, + }, + } + if len(formatSet.Paragraph) < 1 { + formatSet.Paragraph = []formatShapeParagraph{ + { + Font: formatFont{ + Bold: false, + Italic: false, + Underline: "none", + Family: "Calibri", + Size: 11, + Color: "#000000", + }, + Text: " ", + }, + } + } + for _, p := range formatSet.Paragraph { + u := p.Font.Underline + _, ok := textUnderlineType[u] + if !ok { + u = "none" + } + text := p.Text + if text == "" { + text = " " + } + paragraph := &aP{ + R: &aR{ + RPr: aRPr{ + I: p.Font.Italic, + B: p.Font.Bold, + Lang: "en-US", + AltLang: "en-US", + U: u, + Sz: p.Font.Size * 100, + Latin: &aLatin{Typeface: p.Font.Family}, + SolidFill: &aSolidFill{ + SrgbClr: &attrValString{ + Val: strings.Replace(strings.ToUpper(p.Font.Color), "#", "", -1), }, }, - T: formatSet.Text, - }, - EndParaRPr: &aEndParaRPr{ - Lang: "en-US", }, + T: text, }, - }, + EndParaRPr: &aEndParaRPr{ + Lang: "en-US", + }, + } + shape.TxBody.P = append(shape.TxBody.P, paragraph) } twoCellAnchor.Sp = &shape twoCellAnchor.ClientData = &xdrClientData{ diff --git a/xmlDrawing.go b/xmlDrawing.go index bf263a5..e61fe6a 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -330,7 +330,7 @@ type aFontRef struct { // paragraphs multiple runs of text. type xdrTxBody struct { BodyPr *aBodyPr `xml:"a:bodyPr"` - P *aP `xml:"a:p"` + P []*aP `xml:"a:p"` } // formatPicture directly maps the format settings of the picture. @@ -346,15 +346,22 @@ type formatPicture struct { // formatShape directly maps the format settings of the shape. type formatShape struct { - Type string `json:"type"` - Width int `json:"width"` - Height int `json:"height"` - Format formatPicture `json:"format"` - Font formatFont `json:"font"` - Text string `json:"text"` - Color formatShapeColor `json:"color"` + Type string `json:"type"` + Width int `json:"width"` + Height int `json:"height"` + Format formatPicture `json:"format"` + Color formatShapeColor `json:"color"` + Paragraph []formatShapeParagraph `json:"paragraph"` } +// formatShapeParagraph directly maps the format settings of the paragraph in +// the shape. +type formatShapeParagraph struct { + Font formatFont `json:"font"` + Text string `json:"text"` +} + +// formatShapeColor directly maps the color settings of the shape. type formatShapeColor struct { Line string `json:"line"` Fill string `json:"fill"`