From 55e4d4b2c305a6c43178c19a630e54ba7b2c5446 Mon Sep 17 00:00:00 2001 From: Tian <75908403+parkoo@users.noreply.github.com> Date: Mon, 20 Nov 2023 23:57:45 +0800 Subject: [PATCH] The GetCellRichText function support to return inline rich text (#1724) Co-authored-by: jintian.wang --- cell.go | 10 ++++++++++ cell_test.go | 27 +++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/cell.go b/cell.go index 80d113a..204b8df 100644 --- a/cell.go +++ b/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 diff --git a/cell_test.go b/cell_test.go index ea802c0..c41bf58 100644 --- a/cell_test.go +++ b/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")