Skip to content

Commit b19e594

Browse files
authored
Fix GetStyle function can not get VertAlign format (qax-os#2079)
- Fix redundant cols element generated by stream writer - Update dependencies module - Update docs for the GetCellRichText function - Move TestSetCellIntFunc function to cell_test.go
1 parent 0e0e2da commit b19e594

File tree

6 files changed

+61
-53
lines changed

6 files changed

+61
-53
lines changed

cell.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ func getCellRichText(si *xlsxSI) (runs []RichTextRun) {
11011101
}
11021102

11031103
// GetCellRichText provides a function to get rich text of cell by given
1104-
// worksheet.
1104+
// worksheet and cell reference.
11051105
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
11061106
ws, err := f.workSheetReader(sheet)
11071107
if err != nil {
@@ -1164,7 +1164,7 @@ func newRpr(fnt *Font) *xlsxRPr {
11641164

11651165
// newFont create font format by given run properties for the rich text.
11661166
func newFont(rPr *xlsxRPr) *Font {
1167-
font := Font{Underline: "none"}
1167+
var font Font
11681168
font.Bold = rPr.B != nil
11691169
font.Italic = rPr.I != nil
11701170
if rPr.U != nil {
@@ -1179,6 +1179,9 @@ func newFont(rPr *xlsxRPr) *Font {
11791179
if rPr.Sz != nil && rPr.Sz.Val != nil {
11801180
font.Size = *rPr.Sz.Val
11811181
}
1182+
if rPr.VertAlign != nil && rPr.VertAlign.Val != nil {
1183+
font.VertAlign = *rPr.VertAlign.Val
1184+
}
11821185
font.Strike = rPr.Strike != nil
11831186
if rPr.Color != nil {
11841187
font.Color = strings.TrimPrefix(rPr.Color.RGB, "FF")
@@ -1245,7 +1248,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
12451248
// Text: "bold",
12461249
// Font: &excelize.Font{
12471250
// Bold: true,
1248-
// Color: "2354e8",
1251+
// Color: "2354E8",
12491252
// Family: "Times New Roman",
12501253
// },
12511254
// },
@@ -1259,7 +1262,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
12591262
// Text: "italic ",
12601263
// Font: &excelize.Font{
12611264
// Bold: true,
1262-
// Color: "e83723",
1265+
// Color: "E83723",
12631266
// Italic: true,
12641267
// Family: "Times New Roman",
12651268
// },
@@ -1268,43 +1271,43 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
12681271
// Text: "text with color and font-family,",
12691272
// Font: &excelize.Font{
12701273
// Bold: true,
1271-
// Color: "2354e8",
1274+
// Color: "2354E8",
12721275
// Family: "Times New Roman",
12731276
// },
12741277
// },
12751278
// {
12761279
// Text: "\r\nlarge text with ",
12771280
// Font: &excelize.Font{
12781281
// Size: 14,
1279-
// Color: "ad23e8",
1282+
// Color: "AD23E8",
12801283
// },
12811284
// },
12821285
// {
12831286
// Text: "strike",
12841287
// Font: &excelize.Font{
1285-
// Color: "e89923",
1288+
// Color: "E89923",
12861289
// Strike: true,
12871290
// },
12881291
// },
12891292
// {
12901293
// Text: " superscript",
12911294
// Font: &excelize.Font{
1292-
// Color: "dbc21f",
1295+
// Color: "DBC21F",
12931296
// VertAlign: "superscript",
12941297
// },
12951298
// },
12961299
// {
12971300
// Text: " and ",
12981301
// Font: &excelize.Font{
12991302
// Size: 14,
1300-
// Color: "ad23e8",
1303+
// Color: "AD23E8",
13011304
// VertAlign: "baseline",
13021305
// },
13031306
// },
13041307
// {
13051308
// Text: "underline",
13061309
// Font: &excelize.Font{
1307-
// Color: "23e833",
1310+
// Color: "23E833",
13081311
// Underline: "single",
13091312
// },
13101313
// },

cell_test.go

+37-9
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ func TestSetCellRichText(t *testing.T) {
856856
Text: "bold",
857857
Font: &Font{
858858
Bold: true,
859-
Color: "2354e8",
859+
Color: "2354E8",
860860
ColorIndexed: 0,
861861
Family: "Times New Roman",
862862
},
@@ -871,7 +871,7 @@ func TestSetCellRichText(t *testing.T) {
871871
Text: "italic ",
872872
Font: &Font{
873873
Bold: true,
874-
Color: "e83723",
874+
Color: "E83723",
875875
Italic: true,
876876
Family: "Times New Roman",
877877
},
@@ -880,43 +880,43 @@ func TestSetCellRichText(t *testing.T) {
880880
Text: "text with color and font-family, ",
881881
Font: &Font{
882882
Bold: true,
883-
Color: "2354e8",
883+
Color: "2354E8",
884884
Family: "Times New Roman",
885885
},
886886
},
887887
{
888888
Text: "\r\nlarge text with ",
889889
Font: &Font{
890890
Size: 14,
891-
Color: "ad23e8",
891+
Color: "AD23E8",
892892
},
893893
},
894894
{
895895
Text: "strike",
896896
Font: &Font{
897-
Color: "e89923",
897+
Color: "E89923",
898898
Strike: true,
899899
},
900900
},
901901
{
902902
Text: " superscript",
903903
Font: &Font{
904-
Color: "dbc21f",
904+
Color: "DBC21F",
905905
VertAlign: "superscript",
906906
},
907907
},
908908
{
909909
Text: " and ",
910910
Font: &Font{
911911
Size: 14,
912-
Color: "ad23e8",
913-
VertAlign: "BASELINE",
912+
Color: "AD23E8",
913+
VertAlign: "baseline",
914914
},
915915
},
916916
{
917917
Text: "underline",
918918
Font: &Font{
919-
Color: "23e833",
919+
Color: "23E833",
920920
Underline: "single",
921921
},
922922
},
@@ -937,6 +937,11 @@ func TestSetCellRichText(t *testing.T) {
937937
})
938938
assert.NoError(t, err)
939939
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
940+
941+
runs, err := f.GetCellRichText("Sheet1", "A1")
942+
assert.NoError(t, err)
943+
assert.Equal(t, richTextRun, runs)
944+
940945
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
941946
// Test set cell rich text on not exists worksheet
942947
assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN does not exist")
@@ -1153,6 +1158,29 @@ func TestSharedStringsError(t *testing.T) {
11531158
})
11541159
}
11551160

1161+
func TestSetCellIntFunc(t *testing.T) {
1162+
cases := []struct {
1163+
val interface{}
1164+
target string
1165+
}{
1166+
{val: 128, target: "128"},
1167+
{val: int8(-128), target: "-128"},
1168+
{val: int16(-32768), target: "-32768"},
1169+
{val: int32(-2147483648), target: "-2147483648"},
1170+
{val: int64(-9223372036854775808), target: "-9223372036854775808"},
1171+
{val: uint(128), target: "128"},
1172+
{val: uint8(255), target: "255"},
1173+
{val: uint16(65535), target: "65535"},
1174+
{val: uint32(4294967295), target: "4294967295"},
1175+
{val: uint64(18446744073709551615), target: "18446744073709551615"},
1176+
}
1177+
for _, c := range cases {
1178+
cell := &xlsxC{}
1179+
setCellIntFunc(cell, c.val)
1180+
assert.Equal(t, c.target, cell.V)
1181+
}
1182+
}
1183+
11561184
func TestSIString(t *testing.T) {
11571185
assert.Empty(t, xlsxSI{}.String())
11581186
}

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ require (
77
github.com/stretchr/testify v1.9.0
88
github.com/tiendc/go-deepcopy v1.2.0
99
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6
10-
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7
11-
golang.org/x/crypto v0.31.0
10+
github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71
11+
golang.org/x/crypto v0.32.0
1212
golang.org/x/image v0.18.0
13-
golang.org/x/net v0.33.0
13+
golang.org/x/net v0.34.0
1414
golang.org/x/text v0.21.0
1515
)
1616

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ github.com/tiendc/go-deepcopy v1.2.0 h1:6vCCs+qdLQHzFqY1fcPirsAWOmrLbuccilfp8UzD
1313
github.com/tiendc/go-deepcopy v1.2.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
1414
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 h1:8m6DWBG+dlFNbx5ynvrE7NgI+Y7OlZVMVTpayoW+rCc=
1515
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
17-
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18-
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
19-
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
16+
github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71 h1:hOh7aVDrvGJRxzXrQbDY8E+02oaI//5cHL+97oYpEPw=
17+
github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
19+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
2020
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
2121
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
22-
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
23-
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
22+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
23+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
2424
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
2525
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
2626
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

stream.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,8 @@ func (sw *StreamWriter) writeSheetData() {
664664
if !sw.sheetWritten {
665665
bulkAppendFields(&sw.rawData, sw.worksheet, 4, 5)
666666
if sw.worksheet.Cols != nil {
667+
_, _ = sw.rawData.WriteString("<cols>")
667668
for _, col := range sw.worksheet.Cols.Col {
668-
_, _ = sw.rawData.WriteString("<cols>")
669669
sw.rawData.WriteString(`<col min="`)
670670
sw.rawData.WriteString(strconv.Itoa(col.Min))
671671
sw.rawData.WriteString(`" max="`)
@@ -682,8 +682,8 @@ func (sw *StreamWriter) writeSheetData() {
682682
sw.rawData.WriteString(`"`)
683683
}
684684
sw.rawData.WriteString(`/>`)
685-
_, _ = sw.rawData.WriteString("</cols>")
686685
}
686+
_, _ = sw.rawData.WriteString("</cols>")
687687
}
688688
_, _ = sw.rawData.WriteString(`<sheetData>`)
689689
sw.sheetWritten = true

stream_test.go

-23
Original file line numberDiff line numberDiff line change
@@ -413,29 +413,6 @@ func TestStreamSetCellValFunc(t *testing.T) {
413413
}
414414
}
415415

416-
func TestSetCellIntFunc(t *testing.T) {
417-
cases := []struct {
418-
val interface{}
419-
target string
420-
}{
421-
{val: 128, target: "128"},
422-
{val: int8(-128), target: "-128"},
423-
{val: int16(-32768), target: "-32768"},
424-
{val: int32(-2147483648), target: "-2147483648"},
425-
{val: int64(-9223372036854775808), target: "-9223372036854775808"},
426-
{val: uint(128), target: "128"},
427-
{val: uint8(255), target: "255"},
428-
{val: uint16(65535), target: "65535"},
429-
{val: uint32(4294967295), target: "4294967295"},
430-
{val: uint64(18446744073709551615), target: "18446744073709551615"},
431-
}
432-
for _, c := range cases {
433-
cell := &xlsxC{}
434-
setCellIntFunc(cell, c.val)
435-
assert.Equal(t, c.target, cell.V)
436-
}
437-
}
438-
439416
func TestStreamWriterOutlineLevel(t *testing.T) {
440417
file := NewFile()
441418
streamWriter, err := file.NewStreamWriter("Sheet1")

0 commit comments

Comments
 (0)