2019-12-29 16:02:31 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddShape(t *testing.T) {
|
|
|
|
f, err := prepareTestBook1()
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
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", "C30", `{"type":"rect","paragraph":[]}`))
|
2021-09-08 22:05:42 +08:00
|
|
|
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
|
2022-08-28 00:16:41 +08:00
|
|
|
}`), "sheet Sheet3 does not exist")
|
2019-12-29 16:02:31 +08:00
|
|
|
assert.EqualError(t, f.AddShape("Sheet3", "H1", ""), "unexpected end of JSON input")
|
2021-09-08 22:05:42 +08:00
|
|
|
assert.EqualError(t, f.AddShape("Sheet1", "A", `{
|
|
|
|
"type": "rect",
|
|
|
|
"paragraph": [
|
|
|
|
{
|
|
|
|
"text": "Rectangle",
|
|
|
|
"font":
|
|
|
|
{
|
|
|
|
"color": "CD5C5C"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"text": "Shape",
|
|
|
|
"font":
|
|
|
|
{
|
|
|
|
"bold": true,
|
|
|
|
"color": "2980B9"
|
|
|
|
}
|
|
|
|
}]
|
2021-12-07 00:26:53 +08:00
|
|
|
}`), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2019-12-29 16:02:31 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx")))
|
|
|
|
|
|
|
|
// Test add first shape for given sheet.
|
|
|
|
f = NewFile()
|
2021-09-08 22:05:42 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}`))
|
2019-12-29 16:02:31 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
|
2022-11-13 00:40:04 +08:00
|
|
|
// Test add shape with unsupported charset style sheet.
|
2022-11-12 00:02:11 +08:00
|
|
|
f.Styles = nil
|
|
|
|
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
|
|
|
|
assert.EqualError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`), "XML syntax error on line 1: invalid UTF-8")
|
2022-11-13 00:40:04 +08:00
|
|
|
// Test add shape with unsupported charset content types.
|
|
|
|
f = NewFile()
|
|
|
|
f.ContentTypes = nil
|
|
|
|
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
|
|
|
|
assert.EqualError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`), "XML syntax error on line 1: invalid UTF-8")
|
2022-11-12 00:02:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDrawingShape(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
path := "xl/drawings/drawing1.xml"
|
|
|
|
f.Pkg.Store(path, MacintoshCyrillicCharset)
|
|
|
|
assert.EqualError(t, f.addDrawingShape("sheet1", path, "A1", &shapeOptions{}), "XML syntax error on line 1: invalid UTF-8")
|
2019-12-29 16:02:31 +08:00
|
|
|
}
|