The GetCellRichText function support to return inline rich text (#1724)
Co-authored-by: jintian.wang <jintian.wang@mihoyo.com>
This commit is contained in:
parent
6220a798fd
commit
55e4d4b2c3
10
cell.go
10
cell.go
|
@ -971,6 +971,9 @@ func (f *File) SetCellHyperLink(sheet, cell, link, linkType string, opts ...Hype
|
|||
|
||||
// getCellRichText returns rich text of cell by given string item.
|
||||
func getCellRichText(si *xlsxSI) (runs []RichTextRun) {
|
||||
if si.T != nil {
|
||||
runs = append(runs, RichTextRun{Text: si.T.Val})
|
||||
}
|
||||
for _, v := range si.R {
|
||||
run := RichTextRun{
|
||||
Text: v.T.Val,
|
||||
|
@ -994,6 +997,13 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
if c.T == "inlineStr" && c.IS != nil {
|
||||
runs = getCellRichText(c.IS)
|
||||
return
|
||||
}
|
||||
if c.T == "" {
|
||||
return
|
||||
}
|
||||
siIdx, err := strconv.Atoi(c.V)
|
||||
if err != nil || c.T != "s" {
|
||||
return
|
||||
|
|
27
cell_test.go
27
cell_test.go
|
@ -685,26 +685,41 @@ func TestGetCellRichText(t *testing.T) {
|
|||
runsSource[1].Font.Color = strings.ToUpper(runsSource[1].Font.Color)
|
||||
assert.True(t, reflect.DeepEqual(runsSource[1].Font, runs[1].Font), "should get the same font")
|
||||
|
||||
// Test get cell rich text when string item index overflow
|
||||
// Test get cell rich text with inlineStr
|
||||
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0].V = "2"
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0] = xlsxC{
|
||||
T: "inlineStr",
|
||||
IS: &xlsxSI{
|
||||
T: &xlsxT{Val: "A"},
|
||||
R: []xlsxR{{T: &xlsxT{Val: "1"}}},
|
||||
},
|
||||
}
|
||||
runs, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []RichTextRun{{Text: "A"}, {Text: "1"}}, runs)
|
||||
|
||||
// Test get cell rich text when string item index overflow
|
||||
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0] = xlsxC{V: "2", IS: &xlsxSI{}}
|
||||
runs, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(runs))
|
||||
// Test get cell rich text when string item index is negative
|
||||
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0].V = "-1"
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0] = xlsxC{T: "s", V: "-1", IS: &xlsxSI{}}
|
||||
runs, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(runs))
|
||||
// Test get cell rich text on invalid string item index
|
||||
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0].V = "x"
|
||||
_, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.EqualError(t, err, "strconv.Atoi: parsing \"x\": invalid syntax")
|
||||
ws.(*xlsxWorksheet).SheetData.Row[0].C[0] = xlsxC{V: "x"}
|
||||
runs, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(runs))
|
||||
// Test set cell rich text on not exists worksheet
|
||||
_, err = f.GetCellRichText("SheetN", "A1")
|
||||
assert.EqualError(t, err, "sheet SheetN does not exist")
|
||||
|
|
Loading…
Reference in New Issue