From 7e12b560ce40fc756fa5347d25a64ea48f9710ac Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 7 Mar 2021 15:02:04 +0800 Subject: #625, support setting formula for cell in streaming API --- stream.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'stream.go') diff --git a/stream.go b/stream.go index f5fda9d..7aaf7b4 100644 --- a/stream.go +++ b/stream.go @@ -71,6 +71,13 @@ type StreamWriter struct { // fmt.Println(err) // } // +// Set cell value and cell formula for a worksheet with stream writer: +// +// err := streamWriter.SetRow("A1", []interface{}{ +// excelize.Cell{Value: 1}, +// excelize.Cell{Value: 2}, +// excelize.Cell{Formula: "SUM(A1,B1)"}}); +// func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) { sheetID := f.getSheetID(sheet) if sheetID == -1 { @@ -106,7 +113,14 @@ func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) { // // Create a table of F2:H6 with format set: // -// err := sw.AddTable("F2", "H6", `{"table_name":"table","table_style":"TableStyleMedium2","show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`) +// err := sw.AddTable("F2", "H6", `{ +// "table_name": "table", +// "table_style": "TableStyleMedium2", +// "show_first_column": true, +// "show_last_column": true, +// "show_row_stripes": false, +// "show_column_stripes": true +// }`) // // Note that the table must be at least two lines including the header. The // header cells must contain strings and must be unique. @@ -266,6 +280,7 @@ func getRowElement(token xml.Token, hrow int) (startElement xml.StartElement, ok // a value. type Cell struct { StyleID int + Formula string Value interface{} } @@ -291,9 +306,11 @@ func (sw *StreamWriter) SetRow(axis string, values []interface{}) error { if v, ok := val.(Cell); ok { c.S = v.StyleID val = v.Value + setCellFormula(&c, v.Formula) } else if v, ok := val.(*Cell); ok && v != nil { c.S = v.StyleID val = v.Value + setCellFormula(&c, v.Formula) } if err = setCellValFunc(&c, val); err != nil { _, _ = sw.rawData.WriteString(``) @@ -305,6 +322,13 @@ func (sw *StreamWriter) SetRow(axis string, values []interface{}) error { return sw.rawData.Sync() } +// setCellFormula provides a function to set formula of a cell. +func setCellFormula(c *xlsxC, formula string) { + if formula != "" { + c.F = &xlsxF{Content: formula} + } +} + // setCellValFunc provides a function to set value of a cell. func setCellValFunc(c *xlsxC, val interface{}) (err error) { switch val := val.(type) { @@ -373,6 +397,11 @@ func writeCell(buf *bufferedWriter, c xlsxC) { fmt.Fprintf(buf, ` t="%s"`, c.T) } _, _ = buf.WriteString(`>`) + if c.F != nil { + _, _ = buf.WriteString(``) + _ = xml.EscapeText(buf, []byte(c.F.Content)) + _, _ = buf.WriteString(``) + } if c.V != "" { _, _ = buf.WriteString(``) _ = xml.EscapeText(buf, []byte(c.V)) -- cgit v1.2.1