2019-12-10 00:16:17 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2019-12-29 12:45:10 +08:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2019-12-29 16:02:31 +08:00
|
|
|
"io/ioutil"
|
2019-12-10 00:16:17 +08:00
|
|
|
"math/rand"
|
2019-12-29 16:02:31 +08:00
|
|
|
"os"
|
2019-12-10 00:16:17 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-12-29 16:02:31 +08:00
|
|
|
"time"
|
2019-12-10 00:16:17 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-12-29 12:45:10 +08:00
|
|
|
func BenchmarkStreamWriter(b *testing.B) {
|
|
|
|
file := NewFile()
|
|
|
|
|
|
|
|
row := make([]interface{}, 10)
|
|
|
|
for colID := 0; colID < 10; colID++ {
|
|
|
|
row[colID] = colID
|
|
|
|
}
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
streamWriter, _ := file.NewStreamWriter("Sheet1")
|
|
|
|
for rowID := 10; rowID <= 110; rowID++ {
|
|
|
|
cell, _ := CoordinatesToCellName(1, rowID)
|
|
|
|
streamWriter.SetRow(cell, row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:16:17 +08:00
|
|
|
func TestStreamWriter(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Test max characters in a cell.
|
|
|
|
row := make([]interface{}, 1)
|
|
|
|
row[0] = strings.Repeat("c", 32769)
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", row))
|
2019-12-10 00:16:17 +08:00
|
|
|
|
|
|
|
// Test leading and ending space(s) character characters in a cell.
|
|
|
|
row = make([]interface{}, 1)
|
|
|
|
row[0] = " characters"
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A2", row))
|
2019-12-10 00:16:17 +08:00
|
|
|
|
|
|
|
row = make([]interface{}, 1)
|
|
|
|
row[0] = []byte("Word")
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A3", row))
|
2019-12-10 00:16:17 +08:00
|
|
|
|
2019-12-29 16:02:31 +08:00
|
|
|
// Test set cell with style.
|
|
|
|
styleID, err := file.NewStyle(`{"font":{"color":"#777777"}}`)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}}))
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A5", []interface{}{&Cell{StyleID: styleID, Value: "cell"}}))
|
|
|
|
assert.EqualError(t, streamWriter.SetRow("A6", []interface{}{time.Now()}), "only UTC time expected")
|
|
|
|
|
2019-12-10 00:16:17 +08:00
|
|
|
for rowID := 10; rowID <= 51200; rowID++ {
|
|
|
|
row := make([]interface{}, 50)
|
|
|
|
for colID := 0; colID < 50; colID++ {
|
|
|
|
row[colID] = rand.Intn(640000)
|
|
|
|
}
|
|
|
|
cell, _ := CoordinatesToCellName(1, rowID)
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow(cell, row))
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
|
|
|
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.NoError(t, streamWriter.Flush())
|
2020-11-10 23:48:09 +08:00
|
|
|
// Save spreadsheet by the given path.
|
2019-12-10 00:16:17 +08:00
|
|
|
assert.NoError(t, file.SaveAs(filepath.Join("test", "TestStreamWriter.xlsx")))
|
|
|
|
|
2019-12-29 16:02:31 +08:00
|
|
|
// Test close temporary file error.
|
2019-12-22 00:02:09 +08:00
|
|
|
file = NewFile()
|
|
|
|
streamWriter, err = file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2021-02-11 00:07:46 +08:00
|
|
|
for rowID := 10; rowID <= 25600; rowID++ {
|
2019-12-22 00:02:09 +08:00
|
|
|
row := make([]interface{}, 50)
|
|
|
|
for colID := 0; colID < 50; colID++ {
|
|
|
|
row[colID] = rand.Intn(640000)
|
|
|
|
}
|
|
|
|
cell, _ := CoordinatesToCellName(1, rowID)
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow(cell, row))
|
2019-12-22 00:02:09 +08:00
|
|
|
}
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.NoError(t, streamWriter.rawData.Close())
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.Error(t, streamWriter.Flush())
|
2019-12-29 16:02:31 +08:00
|
|
|
|
|
|
|
streamWriter.rawData.tmp, err = ioutil.TempFile(os.TempDir(), "excelize-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = streamWriter.rawData.Reader()
|
|
|
|
assert.NoError(t, err)
|
2020-08-15 00:09:50 +08:00
|
|
|
assert.NoError(t, streamWriter.rawData.tmp.Close())
|
2019-12-29 16:02:31 +08:00
|
|
|
assert.NoError(t, os.Remove(streamWriter.rawData.tmp.Name()))
|
2020-02-13 00:00:42 +08:00
|
|
|
|
|
|
|
// Test unsupport charset
|
|
|
|
file = NewFile()
|
|
|
|
delete(file.Sheet, "xl/worksheets/sheet1.xml")
|
|
|
|
file.XLSX["xl/worksheets/sheet1.xml"] = MacintoshCyrillicCharset
|
|
|
|
streamWriter, err = file.NewStreamWriter("Sheet1")
|
|
|
|
assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:45:10 +08:00
|
|
|
func TestStreamTable(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Write some rows. We want enough rows to force a temp file (>16MB).
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", []interface{}{"A", "B", "C"}))
|
|
|
|
row := []interface{}{1, 2, 3}
|
|
|
|
for r := 2; r < 10000; r++ {
|
|
|
|
assert.NoError(t, streamWriter.SetRow(fmt.Sprintf("A%d", r), row))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a table.
|
|
|
|
assert.NoError(t, streamWriter.AddTable("A1", "C2", ``))
|
|
|
|
assert.NoError(t, streamWriter.Flush())
|
|
|
|
|
|
|
|
// Verify the table has names.
|
|
|
|
var table xlsxTable
|
|
|
|
assert.NoError(t, xml.Unmarshal(file.XLSX["xl/tables/table1.xml"], &table))
|
|
|
|
assert.Equal(t, "A", table.TableColumns.TableColumn[0].Name)
|
|
|
|
assert.Equal(t, "B", table.TableColumns.TableColumn[1].Name)
|
|
|
|
assert.Equal(t, "C", table.TableColumns.TableColumn[2].Name)
|
2019-12-29 16:02:31 +08:00
|
|
|
|
|
|
|
assert.NoError(t, streamWriter.AddTable("A1", "C1", ``))
|
|
|
|
|
|
|
|
// Test add table with illegal formatset.
|
|
|
|
assert.EqualError(t, streamWriter.AddTable("B26", "A21", `{x}`), "invalid character 'x' looking for beginning of object key string")
|
|
|
|
// Test add table with illegal cell coordinates.
|
|
|
|
assert.EqualError(t, streamWriter.AddTable("A", "B1", `{}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, streamWriter.AddTable("A1", "B", `{}`), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
|
2019-12-29 12:45:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewStreamWriter(t *testing.T) {
|
|
|
|
// Test error exceptions
|
|
|
|
file := NewFile()
|
|
|
|
_, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = file.NewStreamWriter("SheetN")
|
|
|
|
assert.EqualError(t, err, "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:16:17 +08:00
|
|
|
func TestSetRow(t *testing.T) {
|
|
|
|
// Test error exceptions
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2019-12-29 12:45:10 +08:00
|
|
|
assert.EqualError(t, streamWriter.SetRow("A", []interface{}{}), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
2019-12-29 16:02:31 +08:00
|
|
|
|
|
|
|
func TestSetCellValFunc(t *testing.T) {
|
|
|
|
c := &xlsxC{}
|
|
|
|
assert.NoError(t, setCellValFunc(c, 128))
|
|
|
|
assert.NoError(t, setCellValFunc(c, int8(-128)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, int16(-32768)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, int32(-2147483648)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, int64(-9223372036854775808)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, uint(128)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, uint8(255)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, uint16(65535)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, uint32(4294967295)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, uint64(18446744073709551615)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, float32(100.1588)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, float64(100.1588)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, " Hello"))
|
|
|
|
assert.NoError(t, setCellValFunc(c, []byte(" Hello")))
|
|
|
|
assert.NoError(t, setCellValFunc(c, time.Now().UTC()))
|
|
|
|
assert.NoError(t, setCellValFunc(c, time.Duration(1e13)))
|
|
|
|
assert.NoError(t, setCellValFunc(c, true))
|
|
|
|
assert.NoError(t, setCellValFunc(c, nil))
|
|
|
|
assert.NoError(t, setCellValFunc(c, complex64(5+10i)))
|
|
|
|
}
|