Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This closes #2068, breaking changes: SetCellInt function required int64 data type parameter #2071

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ func (f *File) setCellIntFunc(sheet, cell string, value interface{}) error {
var err error
switch v := value.(type) {
case int:
err = f.SetCellInt(sheet, cell, v)
err = f.SetCellInt(sheet, cell, int64(v))
case int8:
err = f.SetCellInt(sheet, cell, int(v))
err = f.SetCellInt(sheet, cell, int64(v))
case int16:
err = f.SetCellInt(sheet, cell, int(v))
err = f.SetCellInt(sheet, cell, int64(v))
case int32:
err = f.SetCellInt(sheet, cell, int(v))
err = f.SetCellInt(sheet, cell, int64(v))
case int64:
err = f.SetCellInt(sheet, cell, int(v))
err = f.SetCellInt(sheet, cell, v)
case uint:
err = f.SetCellUint(sheet, cell, uint64(v))
case uint8:
Expand Down Expand Up @@ -288,7 +288,7 @@ func setCellDuration(value time.Duration) (t string, v string) {

// SetCellInt provides a function to set int type value of a cell by given
// worksheet name, cell reference and cell value.
func (f *File) SetCellInt(sheet, cell string, value int) error {
func (f *File) SetCellInt(sheet, cell string, value int64) error {
f.mu.Lock()
ws, err := f.workSheetReader(sheet)
if err != nil {
Expand All @@ -309,8 +309,8 @@ func (f *File) SetCellInt(sheet, cell string, value int) error {
}

// setCellInt prepares cell type and string type cell value by a given integer.
func setCellInt(value int) (t string, v string) {
v = strconv.Itoa(value)
func setCellInt(value int64) (t string, v string) {
v = strconv.FormatInt(value, 10)
return
}

Expand Down
6 changes: 3 additions & 3 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ func TestWriteArrayFormula(t *testing.T) {
valCell := cell(1, i+firstResLine)
assocCell := cell(2, i+firstResLine)

assert.NoError(t, f.SetCellInt("Sheet1", valCell, values[i]))
assert.NoError(t, f.SetCellInt("Sheet1", valCell, int64(values[i])))
assert.NoError(t, f.SetCellStr("Sheet1", assocCell, sample[assoc[i]]))
}

Expand All @@ -642,8 +642,8 @@ func TestWriteArrayFormula(t *testing.T) {
stdevCell := cell(i+2, 4)
calcStdevCell := cell(i+2, 5)

assert.NoError(t, f.SetCellInt("Sheet1", calcAvgCell, average(i)))
assert.NoError(t, f.SetCellInt("Sheet1", calcStdevCell, stdev(i)))
assert.NoError(t, f.SetCellInt("Sheet1", calcAvgCell, int64(average(i))))
assert.NoError(t, f.SetCellInt("Sheet1", calcStdevCell, int64(stdev(i))))

// Average can be done with AVERAGEIF
assert.NoError(t, f.SetCellFormula("Sheet1", avgCell, fmt.Sprintf("ROUND(AVERAGEIF(%s,%s,%s),0)", assocRange, nameCell, valRange)))
Expand Down
4 changes: 2 additions & 2 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func BenchmarkNewSheet(b *testing.B) {
func newSheetWithSet() {
file := NewFile()
for i := 0; i < 1000; i++ {
_ = file.SetCellInt("Sheet1", "A"+strconv.Itoa(i+1), i)
_ = file.SetCellInt("Sheet1", "A"+strconv.Itoa(i+1), int64(i))
}
file = nil
}
Expand All @@ -691,7 +691,7 @@ func BenchmarkFile_SaveAs(b *testing.B) {
func newSheetWithSave() {
file := NewFile()
for i := 0; i < 1000; i++ {
_ = file.SetCellInt("Sheet1", "A"+strconv.Itoa(i+1), i)
_ = file.SetCellInt("Sheet1", "A"+strconv.Itoa(i+1), int64(i))
}
_ = file.Save()
}
Expand Down
10 changes: 5 additions & 5 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,15 @@ func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) error {
func setCellIntFunc(c *xlsxC, val interface{}) {
switch val := val.(type) {
case int:
c.T, c.V = setCellInt(val)
c.T, c.V = setCellInt(int64(val))
case int8:
c.T, c.V = setCellInt(int(val))
c.T, c.V = setCellInt(int64(val))
case int16:
c.T, c.V = setCellInt(int(val))
c.T, c.V = setCellInt(int64(val))
case int32:
c.T, c.V = setCellInt(int(val))
c.T, c.V = setCellInt(int64(val))
case int64:
c.T, c.V = setCellInt(int(val))
c.T, c.V = setCellInt(val)
case uint:
c.T, c.V = setCellUint(uint64(val))
case uint8:
Expand Down
23 changes: 23 additions & 0 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@ func TestStreamSetCellValFunc(t *testing.T) {
}
}

func TestSetCellIntFunc(t *testing.T) {
cases := []struct{
val interface{}
target string
}{
{val: 128, target: "128"},
{val: int8(-128), target: "-128"},
{val: int16(-32768), target: "-32768"},
{val: int32(-2147483648), target: "-2147483648"},
{val: int64(-9223372036854775808), target: "-9223372036854775808"},
{val: uint(128), target: "128"},
{val: uint8(255), target: "255"},
{val: uint16(65535), target: "65535"},
{val: uint32(4294967295), target: "4294967295"},
{val: uint64(18446744073709551615), target: "18446744073709551615"},
}
for _, c := range cases {
cell := &xlsxC{}
setCellIntFunc(cell, c.val)
assert.Equal(t, c.target, cell.V)
}
}

func TestStreamWriterOutlineLevel(t *testing.T) {
file := NewFile()
streamWriter, err := file.NewStreamWriter("Sheet1")
Expand Down