Skip to content

Commit bbb8ebf

Browse files
authored
add GetCellRichText method and test (qax-os#789)
1 parent 2833395 commit bbb8ebf

File tree

4 files changed

+115
-11
lines changed

4 files changed

+115
-11
lines changed

cell.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,54 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
494494
return nil
495495
}
496496

497+
// GetCellRichText provides a function to get rich text of cell by given
498+
// worksheet.
499+
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
500+
ws, err := f.workSheetReader(sheet)
501+
if err != nil {
502+
return
503+
}
504+
cellData, _, _, err := f.prepareCell(ws, sheet, cell)
505+
if err != nil {
506+
return
507+
}
508+
siIdx, err := strconv.Atoi(cellData.V)
509+
if nil != err {
510+
return
511+
}
512+
sst := f.sharedStringsReader()
513+
if len(sst.SI) <= siIdx || siIdx < 0 {
514+
return
515+
}
516+
si := sst.SI[siIdx]
517+
for _, v := range si.R {
518+
run := RichTextRun{
519+
Text: v.T.Val,
520+
}
521+
if nil != v.RPr {
522+
font := Font{}
523+
font.Bold = v.RPr.B != nil
524+
font.Italic = v.RPr.I != nil
525+
if nil != v.RPr.U {
526+
font.Underline = *v.RPr.U.Val
527+
}
528+
if nil != v.RPr.RFont {
529+
font.Family = *v.RPr.RFont.Val
530+
}
531+
if nil != v.RPr.Sz {
532+
font.Size = *v.RPr.Sz.Val
533+
}
534+
font.Strike = v.RPr.Strike != nil
535+
if nil != v.RPr.Color {
536+
font.Color = strings.TrimPrefix(v.RPr.Color.RGB, "FF")
537+
}
538+
run.Font = &font
539+
}
540+
runs = append(runs, run)
541+
}
542+
return
543+
}
544+
497545
// SetCellRichText provides a function to set cell with rich text by given
498546
// worksheet. For example, set rich text on the A1 cell of the worksheet named
499547
// Sheet1:
@@ -619,14 +667,15 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
619667
fnt := textRun.Font
620668
if fnt != nil {
621669
rpr := xlsxRPr{}
670+
trueVal := ""
622671
if fnt.Bold {
623-
rpr.B = " "
672+
rpr.B = &trueVal
624673
}
625674
if fnt.Italic {
626-
rpr.I = " "
675+
rpr.I = &trueVal
627676
}
628677
if fnt.Strike {
629-
rpr.Strike = " "
678+
rpr.Strike = &trueVal
630679
}
631680
if fnt.Underline != "" {
632681
rpr.U = &attrValString{Val: &fnt.Underline}

cell_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package excelize
33
import (
44
"fmt"
55
"path/filepath"
6+
"reflect"
67
"strconv"
8+
"strings"
79
"sync"
810
"testing"
911
"time"
@@ -221,7 +223,59 @@ func TestOverflowNumericCell(t *testing.T) {
221223
// GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
222224
assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
223225
}
226+
func TestGetCellRichText(t *testing.T) {
227+
f := NewFile()
224228

229+
runsSource := []RichTextRun{
230+
{
231+
Text: "a\n",
232+
},
233+
{
234+
Text: "b",
235+
Font: &Font{
236+
Underline: "single",
237+
Color: "ff0000",
238+
Bold: true,
239+
Italic: true,
240+
Family: "Times New Roman",
241+
Size: 100,
242+
Strike: true,
243+
},
244+
},
245+
}
246+
assert.NoError(t, f.SetCellRichText("Sheet1", "A1", runsSource))
247+
248+
runs, err := f.GetCellRichText("Sheet1", "A1")
249+
assert.NoError(t, err)
250+
251+
assert.Equal(t, runsSource[0].Text, runs[0].Text)
252+
assert.Nil(t, runs[0].Font)
253+
assert.NotNil(t, runs[1].Font)
254+
255+
runsSource[1].Font.Color = strings.ToUpper(runsSource[1].Font.Color)
256+
assert.True(t, reflect.DeepEqual(runsSource[1].Font, runs[1].Font), "should get the same font")
257+
258+
// Test get cell rich text when string item index overflow
259+
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "2"
260+
runs, err = f.GetCellRichText("Sheet1", "A1")
261+
assert.NoError(t, err)
262+
assert.Equal(t, 0, len(runs))
263+
// Test get cell rich text when string item index is negative
264+
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "-1"
265+
runs, err = f.GetCellRichText("Sheet1", "A1")
266+
assert.NoError(t, err)
267+
assert.Equal(t, 0, len(runs))
268+
// Test get cell rich text on invalid string item index
269+
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "x"
270+
_, err = f.GetCellRichText("Sheet1", "A1")
271+
assert.EqualError(t, err, "strconv.Atoi: parsing \"x\": invalid syntax")
272+
// Test set cell rich text on not exists worksheet
273+
_, err = f.GetCellRichText("SheetN", "A1")
274+
assert.EqualError(t, err, "sheet SheetN is not exist")
275+
// Test set cell rich text with illegal cell coordinates
276+
_, err = f.GetCellRichText("Sheet1", "A")
277+
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
278+
}
225279
func TestSetCellRichText(t *testing.T) {
226280
f := NewFile()
227281
assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))

comment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,15 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
253253
}
254254
}
255255
defaultFont := f.GetDefaultFont()
256+
bold := ""
256257
cmt := xlsxComment{
257258
Ref: cell,
258259
AuthorID: 0,
259260
Text: xlsxText{
260261
R: []xlsxR{
261262
{
262263
RPr: &xlsxRPr{
263-
B: " ",
264+
B: &bold,
264265
Sz: &attrValFloat{Val: float64Ptr(9)},
265266
Color: &xlsxColor{
266267
Indexed: 81,

xmlSharedStrings.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ type xlsxRPr struct {
8686
RFont *attrValString `xml:"rFont"`
8787
Charset *attrValInt `xml:"charset"`
8888
Family *attrValInt `xml:"family"`
89-
B string `xml:"b,omitempty"`
90-
I string `xml:"i,omitempty"`
91-
Strike string `xml:"strike,omitempty"`
92-
Outline string `xml:"outline,omitempty"`
93-
Shadow string `xml:"shadow,omitempty"`
94-
Condense string `xml:"condense,omitempty"`
95-
Extend string `xml:"extend,omitempty"`
89+
B *string `xml:"b"`
90+
I *string `xml:"i"`
91+
Strike *string `xml:"strike"`
92+
Outline *string `xml:"outline"`
93+
Shadow *string `xml:"shadow"`
94+
Condense *string `xml:"condense"`
95+
Extend *string `xml:"extend"`
9696
Color *xlsxColor `xml:"color"`
9797
Sz *attrValFloat `xml:"sz"`
9898
U *attrValString `xml:"u"`

0 commit comments

Comments
 (0)