Skip to content

Commit

Permalink
Fix GetStyle function can not get VertAlign format (qax-os#2079)
Browse files Browse the repository at this point in the history
- Fix redundant cols element generated by stream writer
- Update dependencies module
- Update docs for the GetCellRichText function
- Move TestSetCellIntFunc function to cell_test.go
  • Loading branch information
hly-717 authored Jan 25, 2025
1 parent 0e0e2da commit b19e594
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 53 deletions.
23 changes: 13 additions & 10 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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",
// },
// },
Expand All @@ -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",
// },
Expand All @@ -1268,43 +1271,43 @@ 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",
// },
// },
// {
// 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",
// },
// },
// {
// Text: " and ",
// Font: &excelize.Font{
// Size: 14,
// Color: "ad23e8",
// Color: "AD23E8",
// VertAlign: "baseline",
// },
// },
// {
// Text: "underline",
// Font: &excelize.Font{
// Color: "23e833",
// Color: "23E833",
// Underline: "single",
// },
// },
Expand Down
46 changes: 37 additions & 9 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -880,43 +880,43 @@ func TestSetCellRichText(t *testing.T) {
Text: "text with color and font-family, ",
Font: &Font{
Bold: true,
Color: "2354e8",
Color: "2354E8",
Family: "Times New Roman",
},
},
{
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",
},
},
{
Text: " and ",
Font: &Font{
Size: 14,
Color: "ad23e8",
VertAlign: "BASELINE",
Color: "AD23E8",
VertAlign: "baseline",
},
},
{
Text: "underline",
Font: &Font{
Color: "23e833",
Color: "23E833",
Underline: "single",
},
},
Expand All @@ -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")
Expand Down Expand Up @@ -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())
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<cols>")
for _, col := range sw.worksheet.Cols.Col {
_, _ = sw.rawData.WriteString("<cols>")
sw.rawData.WriteString(`<col min="`)
sw.rawData.WriteString(strconv.Itoa(col.Min))
sw.rawData.WriteString(`" max="`)
Expand All @@ -682,8 +682,8 @@ func (sw *StreamWriter) writeSheetData() {
sw.rawData.WriteString(`"`)
}
sw.rawData.WriteString(`/>`)
_, _ = sw.rawData.WriteString("</cols>")
}
_, _ = sw.rawData.WriteString("</cols>")
}
_, _ = sw.rawData.WriteString(`<sheetData>`)
sw.sheetWritten = true
Expand Down
23 changes: 0 additions & 23 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit b19e594

Please sign in to comment.