summaryrefslogtreecommitdiff
path: root/cell_test.go
diff options
context:
space:
mode:
authortonnyzhang <450024933@qq.com>2021-02-22 06:04:13 -0600
committerGitHub <noreply@github.com>2021-02-22 20:04:13 +0800
commitbbb8ebfa8cc648ec09094d16eddebb03240baecf (patch)
tree95e74fa22a61bebc3f51023589e4e081fe898799 /cell_test.go
parent283339534741d3f0ff01c2ed2adc7c87445edf07 (diff)
add GetCellRichText method and test (#789)
Diffstat (limited to 'cell_test.go')
-rw-r--r--cell_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/cell_test.go b/cell_test.go
index c3c20f7..026189c 100644
--- a/cell_test.go
+++ b/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))