Skip to content

Commit 2c8dc5c

Browse files
authored
This closes #1169, initialize add form controls supported (#1181)
- Breaking changes: * Change `func (f *File) AddShape(sheet, cell string, opts *Shape) error` to `func (f *File) AddShape(sheet string, opts *Shape) error` * Rename the `Runs` field to `Paragraph` in the exported `Comment` data type - Add new exported function `AddFormControl` support to add button and radio form controls - Add check for shape type for the `AddShape` function, an error will be returned if no shape type is specified - Updated functions documentation and the unit tests
1 parent 8418bd7 commit 2c8dc5c

File tree

10 files changed

+877
-493
lines changed

10 files changed

+877
-493
lines changed

comment.go

Lines changed: 0 additions & 429 deletions
This file was deleted.

excelize_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,6 @@ func TestNewFile(t *testing.T) {
383383
assert.NoError(t, f.Save())
384384
}
385385

386-
func TestAddDrawingVML(t *testing.T) {
387-
// Test addDrawingVML with illegal cell reference
388-
f := NewFile()
389-
assert.EqualError(t, f.addDrawingVML(0, "", "*", 0, 0), newCellNameToCoordinatesError("*", newInvalidCellNameError("*")).Error())
390-
391-
f.Pkg.Store("xl/drawings/vmlDrawing1.vml", MacintoshCyrillicCharset)
392-
assert.EqualError(t, f.addDrawingVML(0, "xl/drawings/vmlDrawing1.vml", "A1", 0, 0), "XML syntax error on line 1: invalid UTF-8")
393-
}
394-
395386
func TestSetCellHyperLink(t *testing.T) {
396387
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
397388
assert.NoError(t, err)
@@ -978,7 +969,7 @@ func TestSetDeleteSheet(t *testing.T) {
978969
f, err := prepareTestBook4()
979970
assert.NoError(t, err)
980971
assert.NoError(t, f.DeleteSheet("Sheet1"))
981-
assert.NoError(t, f.AddComment("Sheet1", Comment{Cell: "A1", Author: "Excelize", Runs: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}))
972+
assert.NoError(t, f.AddComment("Sheet1", Comment{Cell: "A1", Author: "Excelize", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}))
982973
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDeleteSheet.TestBook4.xlsx")))
983974
})
984975
}

shape.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func parseShapeOptions(opts *Shape) (*Shape, error) {
2222
if opts == nil {
2323
return nil, ErrParameterInvalid
2424
}
25+
if opts.Type == "" {
26+
return nil, ErrParameterInvalid
27+
}
2528
if opts.Width == 0 {
2629
opts.Width = defaultShapeSize
2730
}
@@ -285,7 +288,7 @@ func parseShapeOptions(opts *Shape) (*Shape, error) {
285288
// wavy
286289
// wavyHeavy
287290
// wavyDbl
288-
func (f *File) AddShape(sheet, cell string, opts *Shape) error {
291+
func (f *File) AddShape(sheet string, opts *Shape) error {
289292
options, err := parseShapeOptions(opts)
290293
if err != nil {
291294
return err
@@ -313,7 +316,7 @@ func (f *File) AddShape(sheet, cell string, opts *Shape) error {
313316
f.addSheetDrawing(sheet, rID)
314317
f.addSheetNameSpace(sheet, SourceRelationship)
315318
}
316-
if err = f.addDrawingShape(sheet, drawingXML, cell, options); err != nil {
319+
if err = f.addDrawingShape(sheet, drawingXML, opts.Cell, options); err != nil {
317320
return err
318321
}
319322
return f.addContentTypePart(drawingID, "drawings")

shape_test.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ func TestAddShape(t *testing.T) {
1212
if !assert.NoError(t, err) {
1313
t.FailNow()
1414
}
15-
shape := &Shape{
15+
assert.NoError(t, f.AddShape("Sheet1", &Shape{
16+
Cell: "A30",
1617
Type: "rect",
1718
Paragraph: []RichTextRun{
1819
{Text: "Rectangle", Font: &Font{Color: "CD5C5C"}},
1920
{Text: "Shape", Font: &Font{Bold: true, Color: "2980B9"}},
2021
},
21-
}
22-
assert.NoError(t, f.AddShape("Sheet1", "A30", shape))
23-
assert.NoError(t, f.AddShape("Sheet1", "B30", &Shape{Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}))
24-
assert.NoError(t, f.AddShape("Sheet1", "C30", &Shape{Type: "rect"}))
25-
assert.EqualError(t, f.AddShape("Sheet3", "H1",
22+
}))
23+
assert.NoError(t, f.AddShape("Sheet1", &Shape{Cell: "B30", Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}))
24+
assert.NoError(t, f.AddShape("Sheet1", &Shape{Cell: "C30", Type: "rect"}))
25+
assert.EqualError(t, f.AddShape("Sheet3",
2626
&Shape{
27+
Cell: "H1",
2728
Type: "ellipseRibbon",
2829
Line: ShapeLine{Color: "4286F4"},
2930
Fill: Fill{Color: []string{"8EB9FF"}},
@@ -41,15 +42,24 @@ func TestAddShape(t *testing.T) {
4142
},
4243
},
4344
), "sheet Sheet3 does not exist")
44-
assert.EqualError(t, f.AddShape("Sheet3", "H1", nil), ErrParameterInvalid.Error())
45-
assert.EqualError(t, f.AddShape("Sheet1", "A", shape), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
45+
assert.EqualError(t, f.AddShape("Sheet3", nil), ErrParameterInvalid.Error())
46+
assert.EqualError(t, f.AddShape("Sheet1", &Shape{Cell: "A1"}), ErrParameterInvalid.Error())
47+
assert.EqualError(t, f.AddShape("Sheet1", &Shape{
48+
Cell: "A",
49+
Type: "rect",
50+
Paragraph: []RichTextRun{
51+
{Text: "Rectangle", Font: &Font{Color: "CD5C5C"}},
52+
{Text: "Shape", Font: &Font{Bold: true, Color: "2980B9"}},
53+
},
54+
}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
4655
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx")))
4756

4857
// Test add first shape for given sheet
4958
f = NewFile()
5059
lineWidth := 1.2
51-
assert.NoError(t, f.AddShape("Sheet1", "A1",
60+
assert.NoError(t, f.AddShape("Sheet1",
5261
&Shape{
62+
Cell: "A1",
5363
Type: "ellipseRibbon",
5464
Line: ShapeLine{Color: "4286F4", Width: &lineWidth},
5565
Fill: Fill{Color: []string{"8EB9FF"}},
@@ -69,16 +79,23 @@ func TestAddShape(t *testing.T) {
6979
}))
7080
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
7181
// Test add shape with invalid sheet name
72-
assert.EqualError(t, f.AddShape("Sheet:1", "A30", shape), ErrSheetNameInvalid.Error())
82+
assert.EqualError(t, f.AddShape("Sheet:1", &Shape{
83+
Cell: "A30",
84+
Type: "rect",
85+
Paragraph: []RichTextRun{
86+
{Text: "Rectangle", Font: &Font{Color: "CD5C5C"}},
87+
{Text: "Shape", Font: &Font{Bold: true, Color: "2980B9"}},
88+
},
89+
}), ErrSheetNameInvalid.Error())
7390
// Test add shape with unsupported charset style sheet
7491
f.Styles = nil
7592
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
76-
assert.EqualError(t, f.AddShape("Sheet1", "B30", &Shape{Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}), "XML syntax error on line 1: invalid UTF-8")
93+
assert.EqualError(t, f.AddShape("Sheet1", &Shape{Cell: "B30", Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}), "XML syntax error on line 1: invalid UTF-8")
7794
// Test add shape with unsupported charset content types
7895
f = NewFile()
7996
f.ContentTypes = nil
8097
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
81-
assert.EqualError(t, f.AddShape("Sheet1", "B30", &Shape{Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}), "XML syntax error on line 1: invalid UTF-8")
98+
assert.EqualError(t, f.AddShape("Sheet1", &Shape{Cell: "B30", Type: "rect", Paragraph: []RichTextRun{{Text: "Rectangle"}, {}}}), "XML syntax error on line 1: invalid UTF-8")
8299
}
83100

84101
func TestAddDrawingShape(t *testing.T) {

test/vbaProject.bin

1 KB
Binary file not shown.

0 commit comments

Comments
 (0)