Skip to content

Commit

Permalink
This closes #1955, refs #119, support to set cell value with an IEEE …
Browse files Browse the repository at this point in the history
…754 "not-a-number" value or infinity
  • Loading branch information
xuri committed Jul 18, 2024
1 parent 68a1704 commit 4dd3447
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
17 changes: 12 additions & 5 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"bytes"
"encoding/xml"
"fmt"
"math"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -385,6 +386,9 @@ func setCellBool(value bool) (t string, v string) {
// var x float32 = 1.325
// f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32)
func (f *File) SetCellFloat(sheet, cell string, value float64, precision, bitSize int) error {
if math.IsNaN(value) || math.IsInf(value, 0) {
return f.SetCellStr(sheet, cell, fmt.Sprint(value))
}
f.mu.Lock()
ws, err := f.workSheetReader(sheet)
if err != nil {
Expand All @@ -399,16 +403,19 @@ func (f *File) SetCellFloat(sheet, cell string, value float64, precision, bitSiz
return err
}
c.S = ws.prepareCellStyle(col, row, c.S)
c.T, c.V = setCellFloat(value, precision, bitSize)
c.IS = nil
c.setCellFloat(value, precision, bitSize)
return f.removeFormula(c, ws, sheet)
}

// setCellFloat prepares cell type and string type cell value by a given float
// value.
func setCellFloat(value float64, precision, bitSize int) (t string, v string) {
v = strconv.FormatFloat(value, 'f', precision, bitSize)
return
func (c *xlsxC) setCellFloat(value float64, precision, bitSize int) {
if math.IsNaN(value) || math.IsInf(value, 0) {
c.setInlineStr(fmt.Sprint(value))
return
}
c.T, c.V = "", strconv.FormatFloat(value, 'f', precision, bitSize)
c.IS = nil
}

// SetCellStr provides a function to set string type value of a cell. Total
Expand Down
13 changes: 13 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ func TestSetCellValue(t *testing.T) {
val, err = f.GetCellValue("Sheet1", "B1")
assert.NoError(t, err)
assert.Equal(t, "b", val)

f = NewFile()
// Test set cell value with an IEEE 754 "not-a-number" value or infinity
for num, expected := range map[float64]string{
math.NaN(): "NaN",
math.Inf(0): "+Inf",
math.Inf(-1): "-Inf",
} {
assert.NoError(t, f.SetCellValue("Sheet1", "A1", num))
val, err := f.GetCellValue("Sheet1", "A1")
assert.NoError(t, err)
assert.Equal(t, expected, val)
}
}

func TestSetCellValues(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
setCellIntFunc(c, val)
case float32:
c.T, c.V = setCellFloat(float64(val), -1, 32)
c.setCellFloat(float64(val), -1, 32)
case float64:
c.T, c.V = setCellFloat(val, -1, 64)
c.setCellFloat(val, -1, 64)
case string:
c.setCellValue(val)
case []byte:
Expand Down
5 changes: 4 additions & 1 deletion stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"io"
"math"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -76,6 +77,8 @@ func TestStreamWriter(t *testing.T) {
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: 20, Hidden: true, StyleID: styleID}))
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)}))

for rowID := 10; rowID <= 51200; rowID++ {
row := make([]interface{}, 50)
for colID := 0; colID < 50; colID++ {
Expand Down Expand Up @@ -145,7 +148,7 @@ func TestStreamWriter(t *testing.T) {
cells += len(row)
}
assert.NoError(t, rows.Close())
assert.Equal(t, 2559559, cells)
assert.Equal(t, 2559562, cells)
// Save spreadsheet with password.
assert.NoError(t, file.SaveAs(filepath.Join("test", "EncryptionTestStreamWriter.xlsx"), Options{Password: "password"}))
assert.NoError(t, file.Close())
Expand Down

0 comments on commit 4dd3447

Please sign in to comment.