From 8ebe7134dbf29d96951ee98fa05abb11d411c667 Mon Sep 17 00:00:00 2001 From: mengzhongyuan Date: Mon, 9 Jun 2025 22:19:12 +0800 Subject: [PATCH 1/3] refactor: make receiver name of xlsxC unified Change-Id: Idd8da825025a3ab046d74380ec36f25091c41560 Signed-off-by: mengzhongyuan --- rows.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/rows.go b/rows.go index 5dbfaf86ea..237e71d25c 100644 --- a/rows.go +++ b/rows.go @@ -247,21 +247,21 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta } // cellXMLAttrHandler parse the cell XML element attributes of the worksheet. -func (cell *xlsxC) cellXMLAttrHandler(start *xml.StartElement) error { +func (c *xlsxC) cellXMLAttrHandler(start *xml.StartElement) error { for _, attr := range start.Attr { switch attr.Name.Local { case "r": - cell.R = attr.Value + c.R = attr.Value case "s": val, err := strconv.ParseInt(attr.Value, 10, 64) if err != nil { return err } if math.MinInt <= val && val <= math.MaxInt { - cell.S = int(val) + c.S = int(val) } case "t": - cell.T = attr.Value + c.T = attr.Value default: } } @@ -269,9 +269,9 @@ func (cell *xlsxC) cellXMLAttrHandler(start *xml.StartElement) error { } // cellXMLHandler parse the cell XML element of the worksheet. -func (cell *xlsxC) cellXMLHandler(decoder *xml.Decoder, start *xml.StartElement) error { - cell.XMLName = start.Name - err := cell.cellXMLAttrHandler(start) +func (c *xlsxC) cellXMLHandler(decoder *xml.Decoder, start *xml.StartElement) error { + c.XMLName = start.Name + err := c.cellXMLAttrHandler(start) if err != nil { return err } @@ -286,11 +286,11 @@ func (cell *xlsxC) cellXMLHandler(decoder *xml.Decoder, start *xml.StartElement) se = el switch se.Name.Local { case "v": - err = decoder.DecodeElement(&cell.V, &se) + err = decoder.DecodeElement(&c.V, &se) case "f": - err = decoder.DecodeElement(&cell.F, &se) + err = decoder.DecodeElement(&c.F, &se) case "is": - err = decoder.DecodeElement(&cell.IS, &se) + err = decoder.DecodeElement(&c.IS, &se) } if err != nil { return err From d83ae68cb290247c68a7fea39ff0b2c9d1df8d36 Mon Sep 17 00:00:00 2001 From: mengzhongyuan Date: Mon, 9 Jun 2025 22:35:57 +0800 Subject: [PATCH 2/3] This close #2151, streaming writer can set cell default value Change-Id: If019aea806b9527937a9fe8d90050d2ef3d783b0 Signed-off-by: mengzhongyuan --- cell.go | 7 ++++++- stream.go | 2 ++ stream_test.go | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cell.go b/cell.go index b389f73c7b..a471bff6d3 100644 --- a/cell.go +++ b/cell.go @@ -571,6 +571,10 @@ func (c *xlsxC) getCellBool(f *File, raw bool) (string, error) { return f.formattedValue(c, raw, CellTypeBool) } +// SetCellDefaultValue is a type for setCellDefault function, pass this type data, setCellDefault will be used to +// perform the write operation. +type SetCellDefaultValue string + // setCellDefault prepares cell type and string type cell value by a given // string. func (c *xlsxC) setCellDefault(value string) { @@ -580,7 +584,8 @@ func (c *xlsxC) setCellDefault(value string) { c.IS.T.Val = value return } - c.T, c.V, c.IS = value, value, nil + + c.T, c.V, c.IS = "", "", nil // I modify this to set c.T = "" explicitly to avoid misunderstanding of c.Type can be value return } c.T, c.V = "", value diff --git a/stream.go b/stream.go index 63309ff3bd..5700c81ad0 100644 --- a/stream.go +++ b/stream.go @@ -568,6 +568,8 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error { case []RichTextRun: c.T, c.IS = "inlineStr", &xlsxSI{} c.IS.R, err = setRichText(val) + case SetCellDefaultValue: + c.setCellDefault(string(val)) default: c.setCellValue(fmt.Sprint(val)) } diff --git a/stream_test.go b/stream_test.go index a2eea18391..2b3fa23a67 100644 --- a/stream_test.go +++ b/stream_test.go @@ -408,6 +408,8 @@ func TestStreamSetCellValFunc(t *testing.T) { true, nil, complex64(5 + 10i), + SetCellDefaultValue("100.1588"), + SetCellDefaultValue(" Hello"), } { assert.NoError(t, sw.setCellValFunc(c, val)) } From d7ba07017ccce288ac7605cf6e0a4619a5867c0c Mon Sep 17 00:00:00 2001 From: mengzhongyuan Date: Mon, 9 Jun 2025 22:52:36 +0800 Subject: [PATCH 3/3] test: add some test scenario for our business scenario in TestStreamWriter Change-Id: I7b29a679603217d81e566ddc42567a5c369f6bc7 Signed-off-by: mengzhongyuan --- stream_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/stream_test.go b/stream_test.go index 2b3fa23a67..35ec79e5cc 100644 --- a/stream_test.go +++ b/stream_test.go @@ -78,8 +78,14 @@ func TestStreamWriter(t *testing.T) { assert.Equal(t, ErrMaxRowHeight, streamWriter.SetRow("A8", nil, RowOpts{Height: MaxRowHeight + 1})) assert.NoError(t, streamWriter.SetRow("A9", []interface{}{math.NaN(), math.Inf(0), math.Inf(-1)})) + assert.NoError(t, streamWriter.SetRow("A10", []interface{}{ + SetCellDefaultValue("1.0"), "1.0", 1.0, + })) + assert.NoError(t, streamWriter.SetRow("A11", []interface{}{ + SetCellDefaultValue("2.0"), "2.0", 2.0, + })) - for rowID := 10; rowID <= 51200; rowID++ { + for rowID := 12; rowID <= 51200; rowID++ { row := make([]interface{}, 50) for colID := 0; colID < 50; colID++ { row[colID] = rand.Intn(640000) @@ -148,7 +154,7 @@ func TestStreamWriter(t *testing.T) { cells += len(row) } assert.NoError(t, rows.Close()) - assert.Equal(t, 2559562, cells) + assert.Equal(t, 2559468, cells) // Save spreadsheet with password. assert.NoError(t, file.SaveAs(filepath.Join("test", "EncryptionTestStreamWriter.xlsx"), Options{Password: "password"})) assert.NoError(t, file.Close())