The GetCellRichText function support to return inline rich text (#1724)

Co-authored-by: jintian.wang <jintian.wang@mihoyo.com>
This commit is contained in:
Tian 2023-11-20 23:57:45 +08:00 committed by GitHub
parent 6220a798fd
commit 55e4d4b2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

10
cell.go
View File

@ -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

View File

@ -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")