From b19e5940b84eb10f6b16642aea91c618f702fea2 Mon Sep 17 00:00:00 2001
From: hly-717 <2546626370@qq.com>
Date: Sat, 25 Jan 2025 10:01:27 +0800
Subject: [PATCH] Fix GetStyle function can not get VertAlign format (#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
---
cell.go | 23 +++++++++++++----------
cell_test.go | 46 +++++++++++++++++++++++++++++++++++++---------
go.mod | 6 +++---
go.sum | 12 ++++++------
stream.go | 4 ++--
stream_test.go | 23 -----------------------
6 files changed, 61 insertions(+), 53 deletions(-)
diff --git a/cell.go b/cell.go
index 00892f428e..fe204d66cb 100644
--- a/cell.go
+++ b/cell.go
@@ -1101,7 +1101,7 @@ func getCellRichText(si *xlsxSI) (runs []RichTextRun) {
}
// GetCellRichText provides a function to get rich text of cell by given
-// worksheet.
+// worksheet and cell reference.
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
ws, err := f.workSheetReader(sheet)
if err != nil {
@@ -1164,7 +1164,7 @@ func newRpr(fnt *Font) *xlsxRPr {
// newFont create font format by given run properties for the rich text.
func newFont(rPr *xlsxRPr) *Font {
- font := Font{Underline: "none"}
+ var font Font
font.Bold = rPr.B != nil
font.Italic = rPr.I != nil
if rPr.U != nil {
@@ -1179,6 +1179,9 @@ func newFont(rPr *xlsxRPr) *Font {
if rPr.Sz != nil && rPr.Sz.Val != nil {
font.Size = *rPr.Sz.Val
}
+ if rPr.VertAlign != nil && rPr.VertAlign.Val != nil {
+ font.VertAlign = *rPr.VertAlign.Val
+ }
font.Strike = rPr.Strike != nil
if rPr.Color != nil {
font.Color = strings.TrimPrefix(rPr.Color.RGB, "FF")
@@ -1245,7 +1248,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
// Text: "bold",
// Font: &excelize.Font{
// Bold: true,
-// Color: "2354e8",
+// Color: "2354E8",
// Family: "Times New Roman",
// },
// },
@@ -1259,7 +1262,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
// Text: "italic ",
// Font: &excelize.Font{
// Bold: true,
-// Color: "e83723",
+// Color: "E83723",
// Italic: true,
// Family: "Times New Roman",
// },
@@ -1268,7 +1271,7 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
// Text: "text with color and font-family,",
// Font: &excelize.Font{
// Bold: true,
-// Color: "2354e8",
+// Color: "2354E8",
// Family: "Times New Roman",
// },
// },
@@ -1276,20 +1279,20 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
// Text: "\r\nlarge text with ",
// Font: &excelize.Font{
// Size: 14,
-// Color: "ad23e8",
+// Color: "AD23E8",
// },
// },
// {
// Text: "strike",
// Font: &excelize.Font{
-// Color: "e89923",
+// Color: "E89923",
// Strike: true,
// },
// },
// {
// Text: " superscript",
// Font: &excelize.Font{
-// Color: "dbc21f",
+// Color: "DBC21F",
// VertAlign: "superscript",
// },
// },
@@ -1297,14 +1300,14 @@ func setRichText(runs []RichTextRun) ([]xlsxR, error) {
// Text: " and ",
// Font: &excelize.Font{
// Size: 14,
-// Color: "ad23e8",
+// Color: "AD23E8",
// VertAlign: "baseline",
// },
// },
// {
// Text: "underline",
// Font: &excelize.Font{
-// Color: "23e833",
+// Color: "23E833",
// Underline: "single",
// },
// },
diff --git a/cell_test.go b/cell_test.go
index 5590a3682a..ba051a46b9 100644
--- a/cell_test.go
+++ b/cell_test.go
@@ -856,7 +856,7 @@ func TestSetCellRichText(t *testing.T) {
Text: "bold",
Font: &Font{
Bold: true,
- Color: "2354e8",
+ Color: "2354E8",
ColorIndexed: 0,
Family: "Times New Roman",
},
@@ -871,7 +871,7 @@ func TestSetCellRichText(t *testing.T) {
Text: "italic ",
Font: &Font{
Bold: true,
- Color: "e83723",
+ Color: "E83723",
Italic: true,
Family: "Times New Roman",
},
@@ -880,7 +880,7 @@ func TestSetCellRichText(t *testing.T) {
Text: "text with color and font-family, ",
Font: &Font{
Bold: true,
- Color: "2354e8",
+ Color: "2354E8",
Family: "Times New Roman",
},
},
@@ -888,20 +888,20 @@ func TestSetCellRichText(t *testing.T) {
Text: "\r\nlarge text with ",
Font: &Font{
Size: 14,
- Color: "ad23e8",
+ Color: "AD23E8",
},
},
{
Text: "strike",
Font: &Font{
- Color: "e89923",
+ Color: "E89923",
Strike: true,
},
},
{
Text: " superscript",
Font: &Font{
- Color: "dbc21f",
+ Color: "DBC21F",
VertAlign: "superscript",
},
},
@@ -909,14 +909,14 @@ func TestSetCellRichText(t *testing.T) {
Text: " and ",
Font: &Font{
Size: 14,
- Color: "ad23e8",
- VertAlign: "BASELINE",
+ Color: "AD23E8",
+ VertAlign: "baseline",
},
},
{
Text: "underline",
Font: &Font{
- Color: "23e833",
+ Color: "23E833",
Underline: "single",
},
},
@@ -937,6 +937,11 @@ func TestSetCellRichText(t *testing.T) {
})
assert.NoError(t, err)
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
+
+ runs, err := f.GetCellRichText("Sheet1", "A1")
+ assert.NoError(t, err)
+ assert.Equal(t, richTextRun, runs)
+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
// Test set cell rich text on not exists worksheet
assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN does not exist")
@@ -1153,6 +1158,29 @@ func TestSharedStringsError(t *testing.T) {
})
}
+func TestSetCellIntFunc(t *testing.T) {
+ cases := []struct {
+ val interface{}
+ target string
+ }{
+ {val: 128, target: "128"},
+ {val: int8(-128), target: "-128"},
+ {val: int16(-32768), target: "-32768"},
+ {val: int32(-2147483648), target: "-2147483648"},
+ {val: int64(-9223372036854775808), target: "-9223372036854775808"},
+ {val: uint(128), target: "128"},
+ {val: uint8(255), target: "255"},
+ {val: uint16(65535), target: "65535"},
+ {val: uint32(4294967295), target: "4294967295"},
+ {val: uint64(18446744073709551615), target: "18446744073709551615"},
+ }
+ for _, c := range cases {
+ cell := &xlsxC{}
+ setCellIntFunc(cell, c.val)
+ assert.Equal(t, c.target, cell.V)
+ }
+}
+
func TestSIString(t *testing.T) {
assert.Empty(t, xlsxSI{}.String())
}
diff --git a/go.mod b/go.mod
index a732a05547..3c36460069 100644
--- a/go.mod
+++ b/go.mod
@@ -7,10 +7,10 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tiendc/go-deepcopy v1.2.0
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6
- github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7
- golang.org/x/crypto v0.31.0
+ github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71
+ golang.org/x/crypto v0.32.0
golang.org/x/image v0.18.0
- golang.org/x/net v0.33.0
+ golang.org/x/net v0.34.0
golang.org/x/text v0.21.0
)
diff --git a/go.sum b/go.sum
index 40e347c144..222a04df31 100644
--- a/go.sum
+++ b/go.sum
@@ -13,14 +13,14 @@ github.com/tiendc/go-deepcopy v1.2.0 h1:6vCCs+qdLQHzFqY1fcPirsAWOmrLbuccilfp8UzD
github.com/tiendc/go-deepcopy v1.2.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 h1:8m6DWBG+dlFNbx5ynvrE7NgI+Y7OlZVMVTpayoW+rCc=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
-github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
-github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
-golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
-golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
+github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71 h1:hOh7aVDrvGJRxzXrQbDY8E+02oaI//5cHL+97oYpEPw=
+github.com/xuri/nfp v0.0.0-20250111060730-82a408b9aa71/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
+golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
+golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
-golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
-golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
+golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
+golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
diff --git a/stream.go b/stream.go
index 245a7bc3e1..d5e5f0544e 100644
--- a/stream.go
+++ b/stream.go
@@ -664,8 +664,8 @@ func (sw *StreamWriter) writeSheetData() {
if !sw.sheetWritten {
bulkAppendFields(&sw.rawData, sw.worksheet, 4, 5)
if sw.worksheet.Cols != nil {
+ _, _ = sw.rawData.WriteString("")
for _, col := range sw.worksheet.Cols.Col {
- _, _ = sw.rawData.WriteString("")
sw.rawData.WriteString(``)
- _, _ = sw.rawData.WriteString("")
}
+ _, _ = sw.rawData.WriteString("")
}
_, _ = sw.rawData.WriteString(``)
sw.sheetWritten = true
diff --git a/stream_test.go b/stream_test.go
index 9d64b59144..a2eea18391 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -413,29 +413,6 @@ func TestStreamSetCellValFunc(t *testing.T) {
}
}
-func TestSetCellIntFunc(t *testing.T) {
- cases := []struct {
- val interface{}
- target string
- }{
- {val: 128, target: "128"},
- {val: int8(-128), target: "-128"},
- {val: int16(-32768), target: "-32768"},
- {val: int32(-2147483648), target: "-2147483648"},
- {val: int64(-9223372036854775808), target: "-9223372036854775808"},
- {val: uint(128), target: "128"},
- {val: uint8(255), target: "255"},
- {val: uint16(65535), target: "65535"},
- {val: uint32(4294967295), target: "4294967295"},
- {val: uint64(18446744073709551615), target: "18446744073709551615"},
- }
- for _, c := range cases {
- cell := &xlsxC{}
- setCellIntFunc(cell, c.val)
- assert.Equal(t, c.target, cell.V)
- }
-}
-
func TestStreamWriterOutlineLevel(t *testing.T) {
file := NewFile()
streamWriter, err := file.NewStreamWriter("Sheet1")