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)
|
2021-02-15 00:09:35 +08:00
|
|
|
_ = streamWriter.SetRow(cell, row)
|
2019-12-29 12:45:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-04-30 00:14:42 +08:00
|
|
|
row[0] = strings.Repeat("c", TotalCellChars+2)
|
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.
|
2022-01-05 00:13:29 +08:00
|
|
|
styleID, err := file.NewStyle(&Style{Font: &Font{Color: "#777777"}})
|
2019-12-29 16:02:31 +08:00
|
|
|
assert.NoError(t, err)
|
2022-09-21 00:29:34 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}, Cell{Formula: "SUM(A10,B10)"}}, RowOpts{Height: 45, StyleID: styleID}))
|
2021-03-07 15:02:04 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A5", []interface{}{&Cell{StyleID: styleID, Value: "cell"}, &Cell{Formula: "SUM(A10,B10)"}}))
|
2021-06-04 23:06:58 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A6", []interface{}{time.Now()}))
|
2021-12-31 00:00:01 +08:00
|
|
|
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: 20, Hidden: true, StyleID: styleID}))
|
2021-07-25 00:43:07 +08:00
|
|
|
assert.EqualError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: MaxRowHeight + 1}), ErrMaxRowHeight.Error())
|
2019-12-29 16:02:31 +08:00
|
|
|
|
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")))
|
|
|
|
|
2021-05-14 00:09:23 +08:00
|
|
|
// Test set cell column overflow.
|
2022-07-14 23:36:43 +08:00
|
|
|
assert.ErrorIs(t, streamWriter.SetRow("XFD1", []interface{}{"A", "B", "C"}), ErrColumnNumber)
|
2021-05-14 00:09:23 +08:00
|
|
|
|
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
|
|
|
|
2021-03-30 23:02:22 +08:00
|
|
|
// Test unsupported charset
|
2020-02-13 00:00:42 +08:00
|
|
|
file = NewFile()
|
2021-07-05 00:03:56 +08:00
|
|
|
file.Sheet.Delete("xl/worksheets/sheet1.xml")
|
|
|
|
file.Pkg.Store("xl/worksheets/sheet1.xml", MacintoshCyrillicCharset)
|
2021-02-15 00:09:35 +08:00
|
|
|
_, err = file.NewStreamWriter("Sheet1")
|
2020-02-13 00:00:42 +08:00
|
|
|
assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
|
2021-04-30 00:14:42 +08:00
|
|
|
|
|
|
|
// Test read cell.
|
|
|
|
file = NewFile()
|
|
|
|
streamWriter, err = file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", []interface{}{Cell{StyleID: styleID, Value: "Data"}}))
|
|
|
|
assert.NoError(t, streamWriter.Flush())
|
|
|
|
cellValue, err := file.GetCellValue("Sheet1", "A1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Data", cellValue)
|
2021-09-18 23:20:24 +08:00
|
|
|
|
|
|
|
// Test stream reader for a worksheet with huge amounts of data.
|
|
|
|
file, err = OpenFile(filepath.Join("test", "TestStreamWriter.xlsx"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rows, err := file.Rows("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
cells := 0
|
|
|
|
for rows.Next() {
|
|
|
|
row, err := rows.Columns()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
cells += len(row)
|
|
|
|
}
|
|
|
|
assert.NoError(t, rows.Close())
|
|
|
|
assert.Equal(t, 2559558, cells)
|
2022-05-31 11:05:10 +08:00
|
|
|
// Save spreadsheet with password.
|
|
|
|
assert.NoError(t, file.SaveAs(filepath.Join("test", "EncryptionTestStreamWriter.xlsx"), Options{Password: "password"}))
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, file.Close())
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:09:24 +08:00
|
|
|
func TestStreamSetColWidth(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, streamWriter.SetColWidth(3, 2, 20))
|
2022-07-14 23:36:43 +08:00
|
|
|
assert.ErrorIs(t, streamWriter.SetColWidth(0, 3, 20), ErrColumnNumber)
|
|
|
|
assert.ErrorIs(t, streamWriter.SetColWidth(MaxColumns+1, 3, 20), ErrColumnNumber)
|
2021-05-10 00:09:24 +08:00
|
|
|
assert.EqualError(t, streamWriter.SetColWidth(1, 3, MaxColumnWidth+1), ErrColumnWidth.Error())
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", []interface{}{"A", "B", "C"}))
|
|
|
|
assert.EqualError(t, streamWriter.SetColWidth(2, 3, 20), ErrStreamSetColWidth.Error())
|
|
|
|
}
|
|
|
|
|
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
|
2021-07-05 00:03:56 +08:00
|
|
|
val, ok := file.Pkg.Load("xl/tables/table1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NoError(t, xml.Unmarshal(val.([]byte), &table))
|
2019-12-29 12:45:10 +08:00
|
|
|
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")
|
2022-09-18 00:07:15 +08:00
|
|
|
// Test add table with illegal cell reference.
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, streamWriter.AddTable("A", "B1", `{}`), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
|
|
|
assert.EqualError(t, streamWriter.AddTable("A1", "B", `{}`), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
|
2019-12-29 12:45:10 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 10:04:36 +08:00
|
|
|
func TestStreamMergeCells(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, streamWriter.MergeCell("A1", "D1"))
|
2022-09-18 00:07:15 +08:00
|
|
|
// Test merge cells with illegal cell reference.
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, streamWriter.MergeCell("A", "D1"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2021-04-28 10:04:36 +08:00
|
|
|
assert.NoError(t, streamWriter.Flush())
|
|
|
|
// Save spreadsheet by the given path.
|
|
|
|
assert.NoError(t, file.SaveAs(filepath.Join("test", "TestStreamMergeCells.xlsx")))
|
|
|
|
}
|
|
|
|
|
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")
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, err, "sheet SheetN does not exist")
|
2019-12-29 12:45:10 +08:00
|
|
|
}
|
|
|
|
|
2022-09-21 00:29:34 +08:00
|
|
|
func TestStreamMarshalAttrs(t *testing.T) {
|
|
|
|
var r *RowOpts
|
|
|
|
attrs, err := r.marshalAttrs()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStreamSetRow(t *testing.T) {
|
2019-12-10 00:16:17 +08:00
|
|
|
// Test error exceptions
|
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, streamWriter.SetRow("A", []interface{}{}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2019-12-10 00:16:17 +08:00
|
|
|
}
|
2019-12-29 16:02:31 +08:00
|
|
|
|
2022-09-21 00:29:34 +08:00
|
|
|
func TestStreamSetRowNilValues(t *testing.T) {
|
2022-08-04 16:50:33 +08:00
|
|
|
file := NewFile()
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
streamWriter.SetRow("A1", []interface{}{nil, nil, Cell{Value: "foo"}})
|
|
|
|
streamWriter.Flush()
|
|
|
|
ws, err := file.workSheetReader("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEqual(t, ws.SheetData.Row[0].C[0].XMLName.Local, "c")
|
|
|
|
}
|
|
|
|
|
2022-09-21 00:29:34 +08:00
|
|
|
func TestStreamSetRowWithStyle(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
zeroStyleID := 0
|
|
|
|
grayStyleID, err := file.NewStyle(&Style{Font: &Font{Color: "#777777"}})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
blueStyleID, err := file.NewStyle(&Style{Font: &Font{Color: "#0000FF"}})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, streamWriter.SetRow("A1", []interface{}{
|
|
|
|
"value1",
|
|
|
|
Cell{Value: "value2"},
|
|
|
|
&Cell{Value: "value2"},
|
|
|
|
Cell{StyleID: blueStyleID, Value: "value3"},
|
|
|
|
&Cell{StyleID: blueStyleID, Value: "value3"},
|
|
|
|
}, RowOpts{StyleID: grayStyleID}))
|
|
|
|
err = streamWriter.Flush()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
ws, err := file.workSheetReader("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, grayStyleID, ws.SheetData.Row[0].C[0].S)
|
|
|
|
assert.Equal(t, zeroStyleID, ws.SheetData.Row[0].C[1].S)
|
|
|
|
assert.Equal(t, zeroStyleID, ws.SheetData.Row[0].C[2].S)
|
|
|
|
assert.Equal(t, blueStyleID, ws.SheetData.Row[0].C[3].S)
|
|
|
|
assert.Equal(t, blueStyleID, ws.SheetData.Row[0].C[4].S)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStreamSetCellValFunc(t *testing.T) {
|
2021-12-31 00:00:01 +08:00
|
|
|
f := NewFile()
|
|
|
|
sw, err := f.NewStreamWriter("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
2019-12-29 16:02:31 +08:00
|
|
|
c := &xlsxC{}
|
2021-12-31 00:00:01 +08:00
|
|
|
assert.NoError(t, sw.setCellValFunc(c, 128))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, int8(-128)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, int16(-32768)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, int32(-2147483648)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, int64(-9223372036854775808)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, uint(128)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, uint8(255)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, uint16(65535)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, uint32(4294967295)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, uint64(18446744073709551615)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, float32(100.1588)))
|
2022-03-24 00:19:30 +08:00
|
|
|
assert.NoError(t, sw.setCellValFunc(c, 100.1588))
|
2021-12-31 00:00:01 +08:00
|
|
|
assert.NoError(t, sw.setCellValFunc(c, " Hello"))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, []byte(" Hello")))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, time.Now().UTC()))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, time.Duration(1e13)))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, true))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, nil))
|
|
|
|
assert.NoError(t, sw.setCellValFunc(c, complex64(5+10i)))
|
2019-12-29 16:02:31 +08:00
|
|
|
}
|