From bbb8ebfa8cc648ec09094d16eddebb03240baecf Mon Sep 17 00:00:00 2001 From: tonnyzhang <450024933@qq.com> Date: Mon, 22 Feb 2021 06:04:13 -0600 Subject: add GetCellRichText method and test (#789) --- cell_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'cell_test.go') 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)) -- cgit v1.2.1