forked from p30928647/excelize
This closes #262, support set line width of add the shape
This commit is contained in:
parent
684603befa
commit
72d84c0cbd
2
calc.go
2
calc.go
|
@ -948,7 +948,7 @@ func (f *File) parseOperatorPrefixToken(optStack, opdStack *Stack, token efp.Tok
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// isFunctionStartToken determine if the token is function stop.
|
// isFunctionStartToken determine if the token is function start.
|
||||||
func isFunctionStartToken(token efp.Token) bool {
|
func isFunctionStartToken(token efp.Token) bool {
|
||||||
return token.TType == efp.TokenTypeFunction && token.TSubType == efp.TokenSubTypeStart
|
return token.TType == efp.TokenTypeFunction && token.TSubType == efp.TokenSubTypeStart
|
||||||
}
|
}
|
||||||
|
|
34
shape.go
34
shape.go
|
@ -32,6 +32,7 @@ func parseFormatShapeSet(formatSet string) (*formatShape, error) {
|
||||||
XScale: 1.0,
|
XScale: 1.0,
|
||||||
YScale: 1.0,
|
YScale: 1.0,
|
||||||
},
|
},
|
||||||
|
Line: formatLine{Width: 1},
|
||||||
}
|
}
|
||||||
err := json.Unmarshal([]byte(formatSet), &format)
|
err := json.Unmarshal([]byte(formatSet), &format)
|
||||||
return &format, err
|
return &format, err
|
||||||
|
@ -42,7 +43,33 @@ func parseFormatShapeSet(formatSet string) (*formatShape, error) {
|
||||||
// print settings) and properties set. For example, add text box (rect shape)
|
// print settings) and properties set. For example, add text box (rect shape)
|
||||||
// in Sheet1:
|
// in Sheet1:
|
||||||
//
|
//
|
||||||
// err := f.AddShape("Sheet1", "G6", `{"type":"rect","color":{"line":"#4286F4","fill":"#8eb9ff"},"paragraph":[{"text":"Rectangle Shape","font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"sng"}}],"width":180,"height": 90}`)
|
// err := f.AddShape("Sheet1", "G6", `{
|
||||||
|
// "type": "rect",
|
||||||
|
// "color":
|
||||||
|
// {
|
||||||
|
// "line": "#4286F4",
|
||||||
|
// "fill": "#8eb9ff"
|
||||||
|
// },
|
||||||
|
// "paragraph": [
|
||||||
|
// {
|
||||||
|
// "text": "Rectangle Shape",
|
||||||
|
// "font":
|
||||||
|
// {
|
||||||
|
// "bold": true,
|
||||||
|
// "italic": true,
|
||||||
|
// "family": "Times New Roman",
|
||||||
|
// "size": 36,
|
||||||
|
// "color": "#777777",
|
||||||
|
// "underline": "sng"
|
||||||
|
// }
|
||||||
|
// }],
|
||||||
|
// "width": 180,
|
||||||
|
// "height": 90,
|
||||||
|
// "line":
|
||||||
|
// {
|
||||||
|
// "width": 1.2
|
||||||
|
// }
|
||||||
|
// }`)
|
||||||
//
|
//
|
||||||
// The following shows the type of shape supported by excelize:
|
// The following shows the type of shape supported by excelize:
|
||||||
//
|
//
|
||||||
|
@ -378,6 +405,11 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if formatSet.Line.Width != 1 {
|
||||||
|
shape.SpPr.Ln = xlsxLineProperties{
|
||||||
|
W: f.ptToEMUs(formatSet.Line.Width),
|
||||||
|
}
|
||||||
|
}
|
||||||
if len(formatSet.Paragraph) < 1 {
|
if len(formatSet.Paragraph) < 1 {
|
||||||
formatSet.Paragraph = []formatShapeParagraph{
|
formatSet.Paragraph = []formatShapeParagraph{
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,13 +16,75 @@ func TestAddShape(t *testing.T) {
|
||||||
assert.NoError(t, f.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`))
|
assert.NoError(t, f.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`))
|
||||||
assert.NoError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`))
|
assert.NoError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`))
|
||||||
assert.NoError(t, f.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`))
|
assert.NoError(t, f.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`))
|
||||||
assert.EqualError(t, f.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"single"}}], "height": 90}`), "sheet Sheet3 is not exist")
|
assert.EqualError(t, f.AddShape("Sheet3", "H1", `{
|
||||||
|
"type": "ellipseRibbon",
|
||||||
|
"color":
|
||||||
|
{
|
||||||
|
"line": "#4286f4",
|
||||||
|
"fill": "#8eb9ff"
|
||||||
|
},
|
||||||
|
"paragraph": [
|
||||||
|
{
|
||||||
|
"font":
|
||||||
|
{
|
||||||
|
"bold": true,
|
||||||
|
"italic": true,
|
||||||
|
"family": "Times New Roman",
|
||||||
|
"size": 36,
|
||||||
|
"color": "#777777",
|
||||||
|
"underline": "single"
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"height": 90
|
||||||
|
}`), "sheet Sheet3 is not exist")
|
||||||
assert.EqualError(t, f.AddShape("Sheet3", "H1", ""), "unexpected end of JSON input")
|
assert.EqualError(t, f.AddShape("Sheet3", "H1", ""), "unexpected end of JSON input")
|
||||||
assert.EqualError(t, f.AddShape("Sheet1", "A", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
assert.EqualError(t, f.AddShape("Sheet1", "A", `{
|
||||||
|
"type": "rect",
|
||||||
|
"paragraph": [
|
||||||
|
{
|
||||||
|
"text": "Rectangle",
|
||||||
|
"font":
|
||||||
|
{
|
||||||
|
"color": "CD5C5C"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "Shape",
|
||||||
|
"font":
|
||||||
|
{
|
||||||
|
"bold": true,
|
||||||
|
"color": "2980B9"
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx")))
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx")))
|
||||||
|
|
||||||
// Test add first shape for given sheet.
|
// Test add first shape for given sheet.
|
||||||
f = NewFile()
|
f = NewFile()
|
||||||
assert.NoError(t, f.AddShape("Sheet1", "A1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"single"}}], "height": 90}`))
|
assert.NoError(t, f.AddShape("Sheet1", "A1", `{
|
||||||
|
"type": "ellipseRibbon",
|
||||||
|
"color":
|
||||||
|
{
|
||||||
|
"line": "#4286f4",
|
||||||
|
"fill": "#8eb9ff"
|
||||||
|
},
|
||||||
|
"paragraph": [
|
||||||
|
{
|
||||||
|
"font":
|
||||||
|
{
|
||||||
|
"bold": true,
|
||||||
|
"italic": true,
|
||||||
|
"family": "Times New Roman",
|
||||||
|
"size": 36,
|
||||||
|
"color": "#777777",
|
||||||
|
"underline": "single"
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
"height": 90,
|
||||||
|
"line":
|
||||||
|
{
|
||||||
|
"width": 1.2
|
||||||
|
}
|
||||||
|
}`))
|
||||||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,6 +234,13 @@ type xlsxBlipFill struct {
|
||||||
Stretch xlsxStretch `xml:"a:stretch"`
|
Stretch xlsxStretch `xml:"a:stretch"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// xlsxLineProperties specifies the width of a line in EMUs. This simple type
|
||||||
|
// has a minimum value of greater than or equal to 0. This simple type has a
|
||||||
|
// maximum value of less than or equal to 20116800.
|
||||||
|
type xlsxLineProperties struct {
|
||||||
|
W int `xml:"w,attr,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// xlsxSpPr directly maps the spPr (Shape Properties). This element specifies
|
// xlsxSpPr directly maps the spPr (Shape Properties). This element specifies
|
||||||
// the visual shape properties that can be applied to a picture. These are the
|
// the visual shape properties that can be applied to a picture. These are the
|
||||||
// same properties that are allowed to describe the visual properties of a shape
|
// same properties that are allowed to describe the visual properties of a shape
|
||||||
|
@ -242,6 +249,7 @@ type xlsxBlipFill struct {
|
||||||
type xlsxSpPr struct {
|
type xlsxSpPr struct {
|
||||||
Xfrm xlsxXfrm `xml:"a:xfrm"`
|
Xfrm xlsxXfrm `xml:"a:xfrm"`
|
||||||
PrstGeom xlsxPrstGeom `xml:"a:prstGeom"`
|
PrstGeom xlsxPrstGeom `xml:"a:prstGeom"`
|
||||||
|
Ln xlsxLineProperties `xml:"a:ln"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// xlsxPic elements encompass the definition of pictures within the DrawingML
|
// xlsxPic elements encompass the definition of pictures within the DrawingML
|
||||||
|
@ -469,6 +477,7 @@ type formatShape struct {
|
||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
Format formatPicture `json:"format"`
|
Format formatPicture `json:"format"`
|
||||||
Color formatShapeColor `json:"color"`
|
Color formatShapeColor `json:"color"`
|
||||||
|
Line formatLine `json:"line"`
|
||||||
Paragraph []formatShapeParagraph `json:"paragraph"`
|
Paragraph []formatShapeParagraph `json:"paragraph"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -485,3 +494,8 @@ type formatShapeColor struct {
|
||||||
Fill string `json:"fill"`
|
Fill string `json:"fill"`
|
||||||
Effect string `json:"effect"`
|
Effect string `json:"effect"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// formatLine directly maps the line settings of the shape.
|
||||||
|
type formatLine struct {
|
||||||
|
Width float64 `json:"width"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue