diff --git a/chart_test.go b/chart_test.go index 969b57fc7c..0d870add41 100644 --- a/chart_test.go +++ b/chart_test.go @@ -409,7 +409,7 @@ func TestDeleteChart(t *testing.T) { } func TestChartWithLogarithmicBase(t *testing.T) { - // Create test XLSX file with data + // Create test workbook with data f := NewFile() sheet1 := f.GetSheetName(0) categories := map[string]float64{ @@ -454,14 +454,14 @@ func TestChartWithLogarithmicBase(t *testing.T) { assert.NoError(t, f.AddChart(sheet1, c.cell, c.opts)) } - // Export XLSX file for human confirmation + // Export workbook for human confirmation assert.NoError(t, f.SaveAs(filepath.Join("test", "TestChartWithLogarithmicBase10.xlsx"))) - // Write the XLSX file to a buffer + // Write the workbook to a buffer var buffer bytes.Buffer assert.NoError(t, f.Write(&buffer)) - // Read back the XLSX file from the buffer + // Read back the workbook from the buffer newFile, err := OpenReader(&buffer) assert.NoError(t, err) diff --git a/excelize.go b/excelize.go index e0959aa646..c46984f505 100644 --- a/excelize.go +++ b/excelize.go @@ -228,7 +228,7 @@ func (f *File) getOptions(opts ...Options) *Options { } // CharsetTranscoder Set user defined codepage transcoder function for open -// XLSX from non UTF-8 encoding. +// workbook from non UTF-8 encoding. func (f *File) CharsetTranscoder(fn charsetTranscoderFn) *File { f.CharsetReader = fn; return f } // Creates new XML decoder with charset reader. diff --git a/excelize_test.go b/excelize_test.go index c441b5e169..b689602a98 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -365,11 +365,11 @@ func TestNewFile(t *testing.T) { f := NewFile() _, err := f.NewSheet("Sheet1") assert.NoError(t, err) - _, err = f.NewSheet("XLSXSheet2") + _, err = f.NewSheet("Sheet2") assert.NoError(t, err) - _, err = f.NewSheet("XLSXSheet3") + _, err = f.NewSheet("Sheet3") assert.NoError(t, err) - assert.NoError(t, f.SetCellInt("XLSXSheet2", "A23", 56)) + assert.NoError(t, f.SetCellInt("Sheet2", "A23", 56)) assert.NoError(t, f.SetCellStr("Sheet1", "B20", "42")) f.SetActiveSheet(0) @@ -962,7 +962,7 @@ func TestSetDeleteSheet(t *testing.T) { f, err := prepareTestBook3() assert.NoError(t, err) - assert.NoError(t, f.DeleteSheet("XLSXSheet3")) + assert.NoError(t, f.DeleteSheet("Sheet3")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDeleteSheet.TestBook3.xlsx"))) }) @@ -1610,13 +1610,13 @@ func prepareTestBook1() (*File, error) { func prepareTestBook3() (*File, error) { f := NewFile() - if _, err := f.NewSheet("XLSXSheet2"); err != nil { + if _, err := f.NewSheet("Sheet2"); err != nil { return nil, err } - if _, err := f.NewSheet("XLSXSheet3"); err != nil { + if _, err := f.NewSheet("Sheet3"); err != nil { return nil, err } - if err := f.SetCellInt("XLSXSheet2", "A23", 56); err != nil { + if err := f.SetCellInt("Sheet2", "A23", 56); err != nil { return nil, err } if err := f.SetCellStr("Sheet1", "B20", "42"); err != nil { diff --git a/go.mod b/go.mod index d9879927dd..ee3d8e5ba0 100644 --- a/go.mod +++ b/go.mod @@ -8,10 +8,10 @@ require ( github.com/stretchr/testify v1.8.4 github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 - golang.org/x/crypto v0.22.0 + golang.org/x/crypto v0.23.0 golang.org/x/image v0.14.0 - golang.org/x/net v0.24.0 - golang.org/x/text v0.14.0 + golang.org/x/net v0.25.0 + golang.org/x/text v0.15.0 ) require ( diff --git a/go.sum b/go.sum index a18c395579..cb6250459d 100644 --- a/go.sum +++ b/go.sum @@ -15,14 +15,14 @@ github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7 github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A= github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/sparkline.go b/sparkline.go index 5d872c3ef9..ec8baf63d4 100644 --- a/sparkline.go +++ b/sparkline.go @@ -359,9 +359,9 @@ var sparklineGroupPresets = []*xlsxX14SparklineGroup{ // AddSparkline provides a function to add sparklines to the worksheet by // given formatting options. Sparklines are small charts that fit in a single // cell and are used to show trends in data. Sparklines are a feature of Excel -// 2010 and later only. You can write them to an XLSX file that can be read by -// Excel 2007, but they won't be displayed. For example, add a grouped -// sparkline. Changes are applied to all three: +// 2010 and later only. You can write them to workbook that can be read by Excel +// 2007, but they won't be displayed. For example, add a grouped sparkline. +// Changes are applied to all three: // // err := f.AddSparkline("Sheet1", &excelize.SparklineOptions{ // Location: []string{"A1", "A2", "A3"}, diff --git a/stream.go b/stream.go index e8d9ea649c..189732f387 100644 --- a/stream.go +++ b/stream.go @@ -290,7 +290,7 @@ func (sw *StreamWriter) getRowValues(hRow, hCol, vCol int) (res []string, err er } } -// Check if the token is an XLSX row with the matching row number. +// Check if the token is an worksheet row with the matching row number. func getRowElement(token xml.Token, hRow int) (startElement xml.StartElement, ok bool) { startElement, ok = token.(xml.StartElement) if !ok { @@ -527,7 +527,7 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error { var err error switch val := val.(type) { case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: - err = setCellIntFunc(c, val) + setCellIntFunc(c, val) case float32: c.T, c.V = setCellFloat(float64(val), -1, 32) case float64: @@ -554,7 +554,7 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error { } // setCellIntFunc is a wrapper of SetCellInt. -func setCellIntFunc(c *xlsxC, val interface{}) (err error) { +func setCellIntFunc(c *xlsxC, val interface{}) { switch val := val.(type) { case int: c.T, c.V = setCellInt(val) @@ -576,9 +576,7 @@ func setCellIntFunc(c *xlsxC, val interface{}) (err error) { c.T, c.V = setCellUint(uint64(val)) case uint64: c.T, c.V = setCellUint(val) - default: } - return } // writeCell constructs a cell XML and writes it to the buffer. diff --git a/stream_test.go b/stream_test.go index da3fd14125..d7c116c84d 100644 --- a/stream_test.go +++ b/stream_test.go @@ -3,6 +3,7 @@ package excelize import ( "encoding/xml" "fmt" + "io" "math/rand" "os" "path/filepath" @@ -224,6 +225,8 @@ func TestStreamTable(t *testing.T) { assert.Equal(t, newCellNameToCoordinatesError("B", newInvalidCellNameError("B")), streamWriter.AddTable(&Table{Range: "A1:B"})) // Test add table with invalid table name assert.Equal(t, newInvalidNameError("1Table"), streamWriter.AddTable(&Table{Range: "A:B1", Name: "1Table"})) + // Test add table with row number exceeds maximum limit + assert.Equal(t, ErrMaxRows, streamWriter.AddTable(&Table{Range: "A1048576:C1048576"})) // Test add table with unsupported charset content types file.ContentTypes = nil file.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset) @@ -332,8 +335,7 @@ func TestStreamSetRowWithStyle(t *testing.T) { Cell{StyleID: blueStyleID, Value: "value3"}, &Cell{StyleID: blueStyleID, Value: "value3"}, }, RowOpts{StyleID: grayStyleID})) - err = streamWriter.Flush() - assert.NoError(t, err) + assert.NoError(t, streamWriter.Flush()) ws, err := file.workSheetReader("Sheet1") assert.NoError(t, err) @@ -398,3 +400,54 @@ func TestStreamWriterOutlineLevel(t *testing.T) { } assert.NoError(t, file.Close()) } + +func TestStreamWriterReader(t *testing.T) { + var ( + err error + sw = StreamWriter{ + rawData: bufferedWriter{}, + } + ) + sw.rawData.tmp, err = os.CreateTemp(os.TempDir(), "excelize-") + assert.NoError(t, err) + assert.NoError(t, sw.rawData.tmp.Close()) + // Test reader stat a closed temp file + _, err = sw.rawData.Reader() + assert.Error(t, err) + _, err = sw.getRowValues(1, 1, 1) + assert.Error(t, err) + os.Remove(sw.rawData.tmp.Name()) + + sw = StreamWriter{ + file: NewFile(), + rawData: bufferedWriter{}, + } + // Test getRowValues without expected row + sw.rawData.buf.WriteString("") + _, err = sw.getRowValues(1, 1, 1) + assert.NoError(t, err) + sw.rawData.buf.Reset() + // Test getRowValues with illegal cell reference + sw.rawData.buf.WriteString("") + _, err = sw.getRowValues(1, 1, 1) + assert.Equal(t, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")), err) + sw.rawData.buf.Reset() + // Test getRowValues with invalid c element characters + sw.rawData.buf.WriteString("") + _, err = sw.getRowValues(1, 1, 1) + assert.EqualError(t, err, "XML syntax error on line 1: element closed by ") + sw.rawData.buf.Reset() +} + +func TestStreamWriterGetRowElement(t *testing.T) { + // Test get row element without r attribute + dec := xml.NewDecoder(strings.NewReader("")) + for { + token, err := dec.Token() + if err == io.EOF { + break + } + _, ok := getRowElement(token, 0) + assert.False(t, ok) + } +}