forked from p30928647/excelize
add GetCellRichText method and test (#789)
This commit is contained in:
parent
2833395347
commit
bbb8ebfa8c
55
cell.go
55
cell.go
|
@ -494,6 +494,54 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetCellRichText provides a function to get rich text of cell by given
|
||||
// worksheet.
|
||||
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
|
||||
ws, err := f.workSheetReader(sheet)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cellData, _, _, err := f.prepareCell(ws, sheet, cell)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
siIdx, err := strconv.Atoi(cellData.V)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
sst := f.sharedStringsReader()
|
||||
if len(sst.SI) <= siIdx || siIdx < 0 {
|
||||
return
|
||||
}
|
||||
si := sst.SI[siIdx]
|
||||
for _, v := range si.R {
|
||||
run := RichTextRun{
|
||||
Text: v.T.Val,
|
||||
}
|
||||
if nil != v.RPr {
|
||||
font := Font{}
|
||||
font.Bold = v.RPr.B != nil
|
||||
font.Italic = v.RPr.I != nil
|
||||
if nil != v.RPr.U {
|
||||
font.Underline = *v.RPr.U.Val
|
||||
}
|
||||
if nil != v.RPr.RFont {
|
||||
font.Family = *v.RPr.RFont.Val
|
||||
}
|
||||
if nil != v.RPr.Sz {
|
||||
font.Size = *v.RPr.Sz.Val
|
||||
}
|
||||
font.Strike = v.RPr.Strike != nil
|
||||
if nil != v.RPr.Color {
|
||||
font.Color = strings.TrimPrefix(v.RPr.Color.RGB, "FF")
|
||||
}
|
||||
run.Font = &font
|
||||
}
|
||||
runs = append(runs, run)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SetCellRichText provides a function to set cell with rich text by given
|
||||
// worksheet. For example, set rich text on the A1 cell of the worksheet named
|
||||
// Sheet1:
|
||||
|
@ -619,14 +667,15 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
|
|||
fnt := textRun.Font
|
||||
if fnt != nil {
|
||||
rpr := xlsxRPr{}
|
||||
trueVal := ""
|
||||
if fnt.Bold {
|
||||
rpr.B = " "
|
||||
rpr.B = &trueVal
|
||||
}
|
||||
if fnt.Italic {
|
||||
rpr.I = " "
|
||||
rpr.I = &trueVal
|
||||
}
|
||||
if fnt.Strike {
|
||||
rpr.Strike = " "
|
||||
rpr.Strike = &trueVal
|
||||
}
|
||||
if fnt.Underline != "" {
|
||||
rpr.U = &attrValString{Val: &fnt.Underline}
|
||||
|
|
54
cell_test.go
54
cell_test.go
|
@ -3,7 +3,9 @@ package excelize
|
|||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -221,7 +223,59 @@ func TestOverflowNumericCell(t *testing.T) {
|
|||
// GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
|
||||
assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
|
||||
}
|
||||
func TestGetCellRichText(t *testing.T) {
|
||||
f := NewFile()
|
||||
|
||||
runsSource := []RichTextRun{
|
||||
{
|
||||
Text: "a\n",
|
||||
},
|
||||
{
|
||||
Text: "b",
|
||||
Font: &Font{
|
||||
Underline: "single",
|
||||
Color: "ff0000",
|
||||
Bold: true,
|
||||
Italic: true,
|
||||
Family: "Times New Roman",
|
||||
Size: 100,
|
||||
Strike: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.NoError(t, f.SetCellRichText("Sheet1", "A1", runsSource))
|
||||
|
||||
runs, err := f.GetCellRichText("Sheet1", "A1")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, runsSource[0].Text, runs[0].Text)
|
||||
assert.Nil(t, runs[0].Font)
|
||||
assert.NotNil(t, runs[1].Font)
|
||||
|
||||
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
|
||||
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "2"
|
||||
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
|
||||
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "-1"
|
||||
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
|
||||
f.Sheet["xl/worksheets/sheet1.xml"].SheetData.Row[0].C[0].V = "x"
|
||||
_, err = f.GetCellRichText("Sheet1", "A1")
|
||||
assert.EqualError(t, err, "strconv.Atoi: parsing \"x\": invalid syntax")
|
||||
// Test set cell rich text on not exists worksheet
|
||||
_, err = f.GetCellRichText("SheetN", "A1")
|
||||
assert.EqualError(t, err, "sheet SheetN is not exist")
|
||||
// Test set cell rich text with illegal cell coordinates
|
||||
_, err = f.GetCellRichText("Sheet1", "A")
|
||||
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
}
|
||||
func TestSetCellRichText(t *testing.T) {
|
||||
f := NewFile()
|
||||
assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))
|
||||
|
|
|
@ -253,6 +253,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
|
|||
}
|
||||
}
|
||||
defaultFont := f.GetDefaultFont()
|
||||
bold := ""
|
||||
cmt := xlsxComment{
|
||||
Ref: cell,
|
||||
AuthorID: 0,
|
||||
|
@ -260,7 +261,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
|
|||
R: []xlsxR{
|
||||
{
|
||||
RPr: &xlsxRPr{
|
||||
B: " ",
|
||||
B: &bold,
|
||||
Sz: &attrValFloat{Val: float64Ptr(9)},
|
||||
Color: &xlsxColor{
|
||||
Indexed: 81,
|
||||
|
|
|
@ -86,13 +86,13 @@ type xlsxRPr struct {
|
|||
RFont *attrValString `xml:"rFont"`
|
||||
Charset *attrValInt `xml:"charset"`
|
||||
Family *attrValInt `xml:"family"`
|
||||
B string `xml:"b,omitempty"`
|
||||
I string `xml:"i,omitempty"`
|
||||
Strike string `xml:"strike,omitempty"`
|
||||
Outline string `xml:"outline,omitempty"`
|
||||
Shadow string `xml:"shadow,omitempty"`
|
||||
Condense string `xml:"condense,omitempty"`
|
||||
Extend string `xml:"extend,omitempty"`
|
||||
B *string `xml:"b"`
|
||||
I *string `xml:"i"`
|
||||
Strike *string `xml:"strike"`
|
||||
Outline *string `xml:"outline"`
|
||||
Shadow *string `xml:"shadow"`
|
||||
Condense *string `xml:"condense"`
|
||||
Extend *string `xml:"extend"`
|
||||
Color *xlsxColor `xml:"color"`
|
||||
Sz *attrValFloat `xml:"sz"`
|
||||
U *attrValString `xml:"u"`
|
||||
|
|
Loading…
Reference in New Issue