Skip to content

Commit 65a53b3

Browse files
committed
Breaking changes: replace the type ShapeParagraph with RichTextRun
- This removes the `Color` field from the type `Shape`, and uses the `Fill` instead of it - Remove sharp symbol from hex RGB color - Update unit tests
1 parent 669c432 commit 65a53b3

19 files changed

+120
-112
lines changed

calc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13489,7 +13489,7 @@ func (fn *formulaFuncs) LENB(argsList *list.List) formulaArg {
1348913489
return newErrorFormulaArg(formulaErrorVALUE, "LENB requires 1 string argument")
1349013490
}
1349113491
bytes := 0
13492-
for _, r := range []rune(argsList.Front().Value.(formulaArg).String) {
13492+
for _, r := range argsList.Front().Value.(formulaArg).Value() {
1349313493
b := utf8.RuneLen(r)
1349413494
if b == 1 {
1349513495
bytes++

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ type HyperlinkOpts struct {
843843
// }
844844
// // Set underline and font color style for the cell.
845845
// style, err := f.NewStyle(&excelize.Style{
846-
// Font: &excelize.Font{Color: "#1265BE", Underline: "single"},
846+
// Font: &excelize.Font{Color: "1265BE", Underline: "single"},
847847
// })
848848
// if err != nil {
849849
// fmt.Println(err)

cell_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestConcurrency(t *testing.T) {
3737
uint64(1<<32 - 1), true, complex64(5 + 10i),
3838
}))
3939
// Concurrency create style
40-
style, err := f.NewStyle(&Style{Font: &Font{Color: "#1265BE", Underline: "single"}})
40+
style, err := f.NewStyle(&Style{Font: &Font{Color: "1265BE", Underline: "single"}})
4141
assert.NoError(t, err)
4242
// Concurrency set cell style
4343
assert.NoError(t, f.SetCellStyle("Sheet1", "A3", "A3", style))
@@ -948,3 +948,7 @@ func TestSharedStringsError(t *testing.T) {
948948
return assert.NoError(t, os.Remove(v.(string)))
949949
})
950950
}
951+
952+
func TestSIString(t *testing.T) {
953+
assert.Empty(t, xlsxSI{}.String())
954+
}

chart_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestAddChart(t *testing.T) {
200200
sheetName, cell string
201201
opts *Chart
202202
}{
203-
{sheetName: "Sheet1", cell: "P1", opts: &Chart{Type: "col", Series: series, Format: format, Legend: ChartLegend{Position: "none", ShowLegendKey: true}, Title: ChartTitle{Name: "2D Column Chart"}, PlotArea: plotArea, ShowBlanksAs: "zero", XAxis: ChartAxis{Font: Font{Bold: true, Italic: true, Underline: "dbl", Color: "#000000"}}, YAxis: ChartAxis{Font: Font{Bold: false, Italic: false, Underline: "sng", Color: "#777777"}}}},
203+
{sheetName: "Sheet1", cell: "P1", opts: &Chart{Type: "col", Series: series, Format: format, Legend: ChartLegend{Position: "none", ShowLegendKey: true}, Title: ChartTitle{Name: "2D Column Chart"}, PlotArea: plotArea, ShowBlanksAs: "zero", XAxis: ChartAxis{Font: Font{Bold: true, Italic: true, Underline: "dbl", Color: "000000"}}, YAxis: ChartAxis{Font: Font{Bold: false, Italic: false, Underline: "sng", Color: "777777"}}}},
204204
{sheetName: "Sheet1", cell: "X1", opts: &Chart{Type: "colStacked", Series: series, Format: format, Legend: legend, Title: ChartTitle{Name: "2D Stacked Column Chart"}, PlotArea: plotArea, ShowBlanksAs: "zero"}},
205205
{sheetName: "Sheet1", cell: "P16", opts: &Chart{Type: "colPercentStacked", Series: series, Format: format, Legend: legend, Title: ChartTitle{Name: "100% Stacked Column Chart"}, PlotArea: plotArea, ShowBlanksAs: "zero"}},
206206
{sheetName: "Sheet1", cell: "X16", opts: &Chart{Type: "col3DClustered", Series: series, Format: format, Legend: ChartLegend{Position: "bottom", ShowLegendKey: false}, Title: ChartTitle{Name: "3D Clustered Column Chart"}, PlotArea: plotArea, ShowBlanksAs: "zero"}},

col_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func TestSetColStyle(t *testing.T) {
320320
f := NewFile()
321321
assert.NoError(t, f.SetCellValue("Sheet1", "B2", "Hello"))
322322

323-
styleID, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"#94d3a2"}, Pattern: 1}})
323+
styleID, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"94D3A2"}, Pattern: 1}})
324324
assert.NoError(t, err)
325325
// Test set column style on not exists worksheet
326326
assert.EqualError(t, f.SetColStyle("SheetN", "E", styleID), "sheet SheetN does not exist")

comment.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
228228
ID: "_x0000_s1025",
229229
Type: "#_x0000_t202",
230230
Style: "position:absolute;73.5pt;width:108pt;height:59.25pt;z-index:1;visibility:hidden",
231-
Fillcolor: "#fbf6d6",
232-
Strokecolor: "#edeaa1",
231+
Fillcolor: "#FBF6D6",
232+
Strokecolor: "#EDEAA1",
233233
Val: v.Val,
234234
}
235235
vml.Shape = append(vml.Shape, s)
@@ -238,7 +238,7 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
238238
}
239239
sp := encodeShape{
240240
Fill: &vFill{
241-
Color2: "#fbfe82",
241+
Color2: "#FBFE82",
242242
Angle: -180,
243243
Type: "gradient",
244244
Fill: &oFill{
@@ -275,8 +275,8 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
275275
ID: "_x0000_s1025",
276276
Type: "#_x0000_t202",
277277
Style: "position:absolute;73.5pt;width:108pt;height:59.25pt;z-index:1;visibility:hidden",
278-
Fillcolor: "#fbf6d6",
279-
Strokecolor: "#edeaa1",
278+
Fillcolor: "#FBF6D6",
279+
Strokecolor: "#EDEAA1",
280280
Val: string(s[13 : len(s)-14]),
281281
}
282282
vml.Shape = append(vml.Shape, shape)

excelize_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,11 @@ func TestSetCellStyleBorder(t *testing.T) {
677677
assert.NoError(t, err)
678678
assert.NoError(t, f.SetCellStyle("Sheet1", "J21", "L25", style))
679679

680-
style, err = f.NewStyle(&Style{Border: []Border{{Type: "left", Color: "0000FF", Style: 2}, {Type: "top", Color: "00FF00", Style: 3}, {Type: "bottom", Color: "FFFF00", Style: 4}, {Type: "right", Color: "FF0000", Style: 5}, {Type: "diagonalDown", Color: "A020F0", Style: 6}, {Type: "diagonalUp", Color: "A020F0", Style: 7}}, Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#E0EBF5"}, Shading: 1}})
680+
style, err = f.NewStyle(&Style{Border: []Border{{Type: "left", Color: "0000FF", Style: 2}, {Type: "top", Color: "00FF00", Style: 3}, {Type: "bottom", Color: "FFFF00", Style: 4}, {Type: "right", Color: "FF0000", Style: 5}, {Type: "diagonalDown", Color: "A020F0", Style: 6}, {Type: "diagonalUp", Color: "A020F0", Style: 7}}, Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 1}})
681681
assert.NoError(t, err)
682682
assert.NoError(t, f.SetCellStyle("Sheet1", "M28", "K24", style))
683683

684-
style, err = f.NewStyle(&Style{Border: []Border{{Type: "left", Color: "0000FF", Style: 2}, {Type: "top", Color: "00FF00", Style: 3}, {Type: "bottom", Color: "FFFF00", Style: 4}, {Type: "right", Color: "FF0000", Style: 5}, {Type: "diagonalDown", Color: "A020F0", Style: 6}, {Type: "diagonalUp", Color: "A020F0", Style: 7}}, Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#E0EBF5"}, Shading: 4}})
684+
style, err = f.NewStyle(&Style{Border: []Border{{Type: "left", Color: "0000FF", Style: 2}, {Type: "top", Color: "00FF00", Style: 3}, {Type: "bottom", Color: "FFFF00", Style: 4}, {Type: "right", Color: "FF0000", Style: 5}, {Type: "diagonalDown", Color: "A020F0", Style: 6}, {Type: "diagonalUp", Color: "A020F0", Style: 7}}, Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 4}})
685685
assert.NoError(t, err)
686686
assert.NoError(t, f.SetCellStyle("Sheet1", "M28", "K24", style))
687687

@@ -721,7 +721,7 @@ func TestSetCellStyleBorder(t *testing.T) {
721721
},
722722
Fill: Fill{
723723
Type: "pattern",
724-
Color: []string{"#E0EBF5"},
724+
Color: []string{"E0EBF5"},
725725
Pattern: 1,
726726
},
727727
})
@@ -767,7 +767,7 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
767767
} else {
768768
assert.NoError(t, f.SetCellValue("Sheet2", c, val))
769769
}
770-
style, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#E0EBF5"}, Shading: 5}, NumFmt: d})
770+
style, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 5}, NumFmt: d})
771771
if !assert.NoError(t, err) {
772772
t.FailNow()
773773
}
@@ -839,7 +839,7 @@ func TestSetCellStyleCustomNumberFormat(t *testing.T) {
839839
style, err := f.NewStyle(&Style{CustomNumFmt: &customNumFmt})
840840
assert.NoError(t, err)
841841
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
842-
style, err = f.NewStyle(&Style{CustomNumFmt: &customNumFmt, Font: &Font{Color: "#9A0511"}})
842+
style, err = f.NewStyle(&Style{CustomNumFmt: &customNumFmt, Font: &Font{Color: "9A0511"}})
843843
assert.NoError(t, err)
844844
assert.NoError(t, f.SetCellStyle("Sheet1", "A2", "A2", style))
845845

@@ -855,11 +855,11 @@ func TestSetCellStyleFill(t *testing.T) {
855855

856856
var style int
857857
// Test set fill for cell with invalid parameter
858-
style, err = f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#E0EBF5"}, Shading: 6}})
858+
style, err = f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 6}})
859859
assert.NoError(t, err)
860860
assert.NoError(t, f.SetCellStyle("Sheet1", "O23", "O23", style))
861861

862-
style, err = f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF"}, Shading: 1}})
862+
style, err = f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF"}, Shading: 1}})
863863
assert.NoError(t, err)
864864
assert.NoError(t, f.SetCellStyle("Sheet1", "O23", "O23", style))
865865

@@ -879,7 +879,7 @@ func TestSetCellStyleFont(t *testing.T) {
879879
assert.NoError(t, err)
880880

881881
var style int
882-
style, err = f.NewStyle(&Style{Font: &Font{Bold: true, Italic: true, Family: "Times New Roman", Size: 36, Color: "#777777", Underline: "single"}})
882+
style, err = f.NewStyle(&Style{Font: &Font{Bold: true, Italic: true, Family: "Times New Roman", Size: 36, Color: "777777", Underline: "single"}})
883883
assert.NoError(t, err)
884884

885885
assert.NoError(t, f.SetCellStyle("Sheet2", "A1", "A1", style))
@@ -899,7 +899,7 @@ func TestSetCellStyleFont(t *testing.T) {
899899

900900
assert.NoError(t, f.SetCellStyle("Sheet2", "A4", "A4", style))
901901

902-
style, err = f.NewStyle(&Style{Font: &Font{Color: "#777777", Strike: true}})
902+
style, err = f.NewStyle(&Style{Font: &Font{Color: "777777", Strike: true}})
903903
assert.NoError(t, err)
904904

905905
assert.NoError(t, f.SetCellStyle("Sheet2", "A5", "A5", style))
@@ -1002,19 +1002,19 @@ func TestConditionalFormat(t *testing.T) {
10021002
var format1, format2, format3, format4 int
10031003
var err error
10041004
// Rose format for bad conditional
1005-
format1, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "#9A0511"}, Fill: Fill{Type: "pattern", Color: []string{"#FEC7CE"}, Pattern: 1}})
1005+
format1, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "9A0511"}, Fill: Fill{Type: "pattern", Color: []string{"FEC7CE"}, Pattern: 1}})
10061006
assert.NoError(t, err)
10071007

10081008
// Light yellow format for neutral conditional
1009-
format2, err = f.NewConditionalStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"#FEEAA0"}, Pattern: 1}})
1009+
format2, err = f.NewConditionalStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"FEEAA0"}, Pattern: 1}})
10101010
assert.NoError(t, err)
10111011

10121012
// Light green format for good conditional
1013-
format3, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "#09600B"}, Fill: Fill{Type: "pattern", Color: []string{"#C7EECF"}, Pattern: 1}})
1013+
format3, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "09600B"}, Fill: Fill{Type: "pattern", Color: []string{"C7EECF"}, Pattern: 1}})
10141014
assert.NoError(t, err)
10151015

10161016
// conditional style with align and left border
1017-
format4, err = f.NewConditionalStyle(&Style{Alignment: &Alignment{WrapText: true}, Border: []Border{{Type: "left", Color: "#000000", Style: 1}}})
1017+
format4, err = f.NewConditionalStyle(&Style{Alignment: &Alignment{WrapText: true}, Border: []Border{{Type: "left", Color: "000000", Style: 1}}})
10181018
assert.NoError(t, err)
10191019

10201020
// Color scales: 2 color
@@ -1206,7 +1206,7 @@ func TestConditionalFormat(t *testing.T) {
12061206
f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
12071207
assert.NoError(t, err)
12081208

1209-
_, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "#9A0511"}, Fill: Fill{Type: "", Color: []string{"#FEC7CE"}, Pattern: 1}})
1209+
_, err = f.NewConditionalStyle(&Style{Font: &Font{Color: "9A0511"}, Fill: Fill{Type: "", Color: []string{"FEC7CE"}, Pattern: 1}})
12101210
assert.NoError(t, err)
12111211
assert.NoError(t, f.Close())
12121212
}

pivotTable_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestAddPivotTable(t *testing.T) {
7070
}))
7171
assert.NoError(t, f.AddPivotTable(&PivotTableOptions{
7272
DataRange: "Sheet1!$A$1:$E$31",
73-
PivotTableRange: "Sheet1!$G$39:$W$52",
73+
PivotTableRange: "Sheet1!$G$42:$W$55",
7474
Rows: []PivotTableField{{Data: "Month"}},
7575
Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Year"}},
7676
Data: []PivotTableField{{Data: "Sales", Subtotal: "CountNums", Name: "Summarize by CountNums"}},

rows_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,9 @@ func TestCheckRow(t *testing.T) {
990990

991991
func TestSetRowStyle(t *testing.T) {
992992
f := NewFile()
993-
style1, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"#63BE7B"}, Pattern: 1}})
993+
style1, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"63BE7B"}, Pattern: 1}})
994994
assert.NoError(t, err)
995-
style2, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"#E0EBF5"}, Pattern: 1}})
995+
style2, err := f.NewStyle(&Style{Fill: Fill{Type: "pattern", Color: []string{"E0EBF5"}, Pattern: 1}})
996996
assert.NoError(t, err)
997997
assert.NoError(t, f.SetCellStyle("Sheet1", "B2", "B2", style1))
998998
assert.EqualError(t, f.SetRowStyle("Sheet1", 5, -1, style2), newInvalidRowNumberError(-1).Error())

shape.go

+26-18
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ func parseShapeOptions(opts *Shape) (*Shape, error) {
5454
// lineWidth := 1.2
5555
// err := f.AddShape("Sheet1", "G6",
5656
// &excelize.Shape{
57-
// Type: "rect",
58-
// Color: excelize.ShapeColor{Line: "#4286f4", Fill: "#8eb9ff"},
59-
// Paragraph: []excelize.ShapeParagraph{
57+
// Type: "rect",
58+
// Line: excelize.ShapeLine{Color: "4286F4", Width: &lineWidth},
59+
// Fill: excelize.Fill{Color: []string{"8EB9FF"}},
60+
// Paragraph: []excelize.RichTextRun{
6061
// {
6162
// Text: "Rectangle Shape",
62-
// Font: excelize.Font{
63+
// Font: &excelize.Font{
6364
// Bold: true,
6465
// Italic: true,
6566
// Family: "Times New Roman",
6667
// Size: 18,
67-
// Color: "#777777",
68+
// Color: "777777",
6869
// Underline: "sng",
6970
// },
7071
// },
7172
// },
7273
// Width: 180,
7374
// Height: 40,
74-
// Line: excelize.ShapeLine{Width: &lineWidth},
7575
// },
7676
// )
7777
//
@@ -352,6 +352,10 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *Shape) erro
352352
to.RowOff = y2 * EMU
353353
twoCellAnchor.From = &from
354354
twoCellAnchor.To = &to
355+
var solidColor string
356+
if len(opts.Fill.Color) == 1 {
357+
solidColor = opts.Fill.Color[0]
358+
}
355359
shape := xdrSp{
356360
Macro: opts.Macro,
357361
NvSpPr: &xdrNvSpPr{
@@ -369,9 +373,9 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *Shape) erro
369373
},
370374
},
371375
Style: &xdrStyle{
372-
LnRef: setShapeRef(opts.Color.Line, 2),
373-
FillRef: setShapeRef(opts.Color.Fill, 1),
374-
EffectRef: setShapeRef(opts.Color.Effect, 0),
376+
LnRef: setShapeRef(opts.Line.Color, 2),
377+
FillRef: setShapeRef(solidColor, 1),
378+
EffectRef: setShapeRef("", 0),
375379
FontRef: &aFontRef{
376380
Idx: "minor",
377381
SchemeClr: &attrValString{
@@ -399,23 +403,27 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *Shape) erro
399403
return err
400404
}
401405
if len(opts.Paragraph) < 1 {
402-
opts.Paragraph = []ShapeParagraph{
406+
opts.Paragraph = []RichTextRun{
403407
{
404-
Font: Font{
408+
Font: &Font{
405409
Bold: false,
406410
Italic: false,
407411
Underline: "none",
408412
Family: defaultFont,
409413
Size: 11,
410-
Color: "#000000",
414+
Color: "000000",
411415
},
412416
Text: " ",
413417
},
414418
}
415419
}
416420
for _, p := range opts.Paragraph {
417421
u := "none"
418-
if idx := inStrSlice(supportedDrawingUnderlineTypes, p.Font.Underline, true); idx != -1 {
422+
font := &Font{}
423+
if p.Font != nil {
424+
font = p.Font
425+
}
426+
if idx := inStrSlice(supportedDrawingUnderlineTypes, font.Underline, true); idx != -1 {
419427
u = supportedDrawingUnderlineTypes[idx]
420428
}
421429
text := p.Text
@@ -425,21 +433,21 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *Shape) erro
425433
paragraph := &aP{
426434
R: &aR{
427435
RPr: aRPr{
428-
I: p.Font.Italic,
429-
B: p.Font.Bold,
436+
I: font.Italic,
437+
B: font.Bold,
430438
Lang: "en-US",
431439
AltLang: "en-US",
432440
U: u,
433-
Sz: p.Font.Size * 100,
434-
Latin: &xlsxCTTextFont{Typeface: p.Font.Family},
441+
Sz: font.Size * 100,
442+
Latin: &xlsxCTTextFont{Typeface: font.Family},
435443
},
436444
T: text,
437445
},
438446
EndParaRPr: &aEndParaRPr{
439447
Lang: "en-US",
440448
},
441449
}
442-
srgbClr := strings.ReplaceAll(strings.ToUpper(p.Font.Color), "#", "")
450+
srgbClr := strings.ReplaceAll(strings.ToUpper(font.Color), "#", "")
443451
if len(srgbClr) == 6 {
444452
paragraph.R.RPr.SolidFill = &aSolidFill{
445453
SrgbClr: &attrValString{

0 commit comments

Comments
 (0)