From f44153ea4679247070d6f1e31bb0934a10bebb31 Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 25 Oct 2022 10:24:45 +0800 Subject: This closes #1377, stream writer writes inline string type for string cell value - Add `CellTypeFormula`, `CellTypeInlineString`, `CellTypeSharedString` and remove `CellTypeString` in `CellType` enumeration - Unit tests updated --- stream.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'stream.go') diff --git a/stream.go b/stream.go index aaa4589..fa78d8b 100644 --- a/stream.go +++ b/stream.go @@ -263,7 +263,7 @@ func (sw *StreamWriter) getRowValues(hRow, hCol, vCol int) (res []string, err er if col < hCol || col > vCol { continue } - res[col-hCol] = c.V + res[col-hCol], _ = c.getValueFrom(sw.File, nil, false) } return res, nil } @@ -462,7 +462,7 @@ func (sw *StreamWriter) MergeCell(hCell, vCell string) error { // setCellFormula provides a function to set formula of a cell. func setCellFormula(c *xlsxC, formula string) { if formula != "" { - c.F = &xlsxF{Content: formula} + c.T, c.F = "str", &xlsxF{Content: formula} } } @@ -477,9 +477,9 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error { case float64: c.T, c.V = setCellFloat(val, -1, 64) case string: - c.T, c.V, c.XMLSpace = setCellStr(val) + c.setCellValue(val) case []byte: - c.T, c.V, c.XMLSpace = setCellStr(string(val)) + c.setCellValue(string(val)) case time.Duration: c.T, c.V = setCellDuration(val) case time.Time: @@ -488,20 +488,19 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error { if wb != nil && wb.WorkbookPr != nil { date1904 = wb.WorkbookPr.Date1904 } - c.T, c.V, isNum, err = setCellTime(val, date1904) - if isNum && c.S == 0 { + if isNum, err = c.setCellTime(val, date1904); isNum && c.S == 0 { style, _ := sw.File.NewStyle(&Style{NumFmt: 22}) c.S = style } case bool: c.T, c.V = setCellBool(val) case nil: - c.T, c.V, c.XMLSpace = setCellStr("") + c.setCellValue("") case []RichTextRun: c.T, c.IS = "inlineStr", &xlsxSI{} c.IS.R, err = setRichText(val) default: - c.T, c.V, c.XMLSpace = setCellStr(fmt.Sprint(val)) + c.setCellValue(fmt.Sprint(val)) } return err } @@ -569,10 +568,25 @@ func writeCell(buf *bufferedWriter, c xlsxC) { _, _ = buf.WriteString(``) } if c.IS != nil { - is, _ := xml.Marshal(c.IS.R) - _, _ = buf.WriteString(``) - _, _ = buf.Write(is) - _, _ = buf.WriteString(``) + if len(c.IS.R) > 0 { + is, _ := xml.Marshal(c.IS.R) + _, _ = buf.WriteString(``) + _, _ = buf.Write(is) + _, _ = buf.WriteString(``) + } + if c.IS.T != nil { + _, _ = buf.WriteString(``) + _, _ = buf.Write([]byte(c.IS.T.Val)) + _, _ = buf.WriteString(``) + } } _, _ = buf.WriteString(``) } -- cgit v1.2.1