Skip to content

Commit 72d84c0

Browse files
committed
This closes qax-os#262, support set line width of add the shape
1 parent 684603b commit 72d84c0

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed

calc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ func (f *File) parseOperatorPrefixToken(optStack, opdStack *Stack, token efp.Tok
948948
return
949949
}
950950

951-
// isFunctionStartToken determine if the token is function stop.
951+
// isFunctionStartToken determine if the token is function start.
952952
func isFunctionStartToken(token efp.Token) bool {
953953
return token.TType == efp.TokenTypeFunction && token.TSubType == efp.TokenSubTypeStart
954954
}

shape.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func parseFormatShapeSet(formatSet string) (*formatShape, error) {
3232
XScale: 1.0,
3333
YScale: 1.0,
3434
},
35+
Line: formatLine{Width: 1},
3536
}
3637
err := json.Unmarshal([]byte(formatSet), &format)
3738
return &format, err
@@ -42,7 +43,33 @@ func parseFormatShapeSet(formatSet string) (*formatShape, error) {
4243
// print settings) and properties set. For example, add text box (rect shape)
4344
// in Sheet1:
4445
//
45-
// 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}`)
46+
// err := f.AddShape("Sheet1", "G6", `{
47+
// "type": "rect",
48+
// "color":
49+
// {
50+
// "line": "#4286F4",
51+
// "fill": "#8eb9ff"
52+
// },
53+
// "paragraph": [
54+
// {
55+
// "text": "Rectangle Shape",
56+
// "font":
57+
// {
58+
// "bold": true,
59+
// "italic": true,
60+
// "family": "Times New Roman",
61+
// "size": 36,
62+
// "color": "#777777",
63+
// "underline": "sng"
64+
// }
65+
// }],
66+
// "width": 180,
67+
// "height": 90,
68+
// "line":
69+
// {
70+
// "width": 1.2
71+
// }
72+
// }`)
4673
//
4774
// The following shows the type of shape supported by excelize:
4875
//
@@ -378,6 +405,11 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
378405
},
379406
},
380407
}
408+
if formatSet.Line.Width != 1 {
409+
shape.SpPr.Ln = xlsxLineProperties{
410+
W: f.ptToEMUs(formatSet.Line.Width),
411+
}
412+
}
381413
if len(formatSet.Paragraph) < 1 {
382414
formatSet.Paragraph = []formatShapeParagraph{
383415
{

shape_test.go

+65-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,75 @@ func TestAddShape(t *testing.T) {
1616
assert.NoError(t, f.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`))
1717
assert.NoError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`))
1818
assert.NoError(t, f.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`))
19-
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")
19+
assert.EqualError(t, f.AddShape("Sheet3", "H1", `{
20+
"type": "ellipseRibbon",
21+
"color":
22+
{
23+
"line": "#4286f4",
24+
"fill": "#8eb9ff"
25+
},
26+
"paragraph": [
27+
{
28+
"font":
29+
{
30+
"bold": true,
31+
"italic": true,
32+
"family": "Times New Roman",
33+
"size": 36,
34+
"color": "#777777",
35+
"underline": "single"
36+
}
37+
}],
38+
"height": 90
39+
}`), "sheet Sheet3 is not exist")
2040
assert.EqualError(t, f.AddShape("Sheet3", "H1", ""), "unexpected end of JSON input")
21-
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"`)
41+
assert.EqualError(t, f.AddShape("Sheet1", "A", `{
42+
"type": "rect",
43+
"paragraph": [
44+
{
45+
"text": "Rectangle",
46+
"font":
47+
{
48+
"color": "CD5C5C"
49+
}
50+
},
51+
{
52+
"text": "Shape",
53+
"font":
54+
{
55+
"bold": true,
56+
"color": "2980B9"
57+
}
58+
}]
59+
}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
2260
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx")))
2361

2462
// Test add first shape for given sheet.
2563
f = NewFile()
26-
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}`))
64+
assert.NoError(t, f.AddShape("Sheet1", "A1", `{
65+
"type": "ellipseRibbon",
66+
"color":
67+
{
68+
"line": "#4286f4",
69+
"fill": "#8eb9ff"
70+
},
71+
"paragraph": [
72+
{
73+
"font":
74+
{
75+
"bold": true,
76+
"italic": true,
77+
"family": "Times New Roman",
78+
"size": 36,
79+
"color": "#777777",
80+
"underline": "single"
81+
}
82+
}],
83+
"height": 90,
84+
"line":
85+
{
86+
"width": 1.2
87+
}
88+
}`))
2789
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
2890
}

xmlDrawing.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,22 @@ type xlsxBlipFill struct {
234234
Stretch xlsxStretch `xml:"a:stretch"`
235235
}
236236

237+
// xlsxLineProperties specifies the width of a line in EMUs. This simple type
238+
// has a minimum value of greater than or equal to 0. This simple type has a
239+
// maximum value of less than or equal to 20116800.
240+
type xlsxLineProperties struct {
241+
W int `xml:"w,attr,omitempty"`
242+
}
243+
237244
// xlsxSpPr directly maps the spPr (Shape Properties). This element specifies
238245
// the visual shape properties that can be applied to a picture. These are the
239246
// same properties that are allowed to describe the visual properties of a shape
240247
// but are used here to describe the visual appearance of a picture within a
241248
// document.
242249
type xlsxSpPr struct {
243-
Xfrm xlsxXfrm `xml:"a:xfrm"`
244-
PrstGeom xlsxPrstGeom `xml:"a:prstGeom"`
250+
Xfrm xlsxXfrm `xml:"a:xfrm"`
251+
PrstGeom xlsxPrstGeom `xml:"a:prstGeom"`
252+
Ln xlsxLineProperties `xml:"a:ln"`
245253
}
246254

247255
// xlsxPic elements encompass the definition of pictures within the DrawingML
@@ -469,6 +477,7 @@ type formatShape struct {
469477
Height int `json:"height"`
470478
Format formatPicture `json:"format"`
471479
Color formatShapeColor `json:"color"`
480+
Line formatLine `json:"line"`
472481
Paragraph []formatShapeParagraph `json:"paragraph"`
473482
}
474483

@@ -485,3 +494,8 @@ type formatShapeColor struct {
485494
Fill string `json:"fill"`
486495
Effect string `json:"effect"`
487496
}
497+
498+
// formatLine directly maps the line settings of the shape.
499+
type formatLine struct {
500+
Width float64 `json:"width"`
501+
}

0 commit comments

Comments
 (0)