From 8a775057c2b04aad25d88d4b07d0c2fc9ff1f32c Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 29 Apr 2022 13:53:09 +0800 Subject: [PATCH] This closes #1213, fix get incorrect rich text value caused by missing cell type checking --- cell.go | 6 +++--- cell_test.go | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/cell.go b/cell.go index b2818e7fbd7..3c44af45ec5 100644 --- a/cell.go +++ b/cell.go @@ -764,7 +764,7 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro return } siIdx, err := strconv.Atoi(cellData.V) - if nil != err { + if err != nil || cellData.T != "s" { return } sst := f.sharedStringsReader() @@ -776,7 +776,7 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro run := RichTextRun{ Text: v.T.Val, } - if nil != v.RPr { + if v.RPr != nil { font := Font{Underline: "none"} font.Bold = v.RPr.B != nil font.Italic = v.RPr.I != nil @@ -793,7 +793,7 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro font.Size = *v.RPr.Sz.Val } font.Strike = v.RPr.Strike != nil - if nil != v.RPr.Color { + if v.RPr.Color != nil { font.Color = strings.TrimPrefix(v.RPr.Color.RGB, "FF") } run.Font = &font diff --git a/cell_test.go b/cell_test.go index 73b3018b3fd..77179cc237d 100644 --- a/cell_test.go +++ b/cell_test.go @@ -502,8 +502,13 @@ func TestGetCellRichText(t *testing.T) { }, } assert.NoError(t, f.SetCellRichText("Sheet1", "A1", runsSource)) + assert.NoError(t, f.SetCellValue("Sheet1", "A2", false)) - runs, err := f.GetCellRichText("Sheet1", "A1") + runs, err := f.GetCellRichText("Sheet1", "A2") + assert.NoError(t, err) + assert.Equal(t, []RichTextRun(nil), runs) + + runs, err = f.GetCellRichText("Sheet1", "A1") assert.NoError(t, err) assert.Equal(t, runsSource[0].Text, runs[0].Text)