Skip to content

Commit

Permalink
This closes qax-os#1247, add new function SetSheetCol for set works…
Browse files Browse the repository at this point in the history
…heet column values (qax-os#1320)

Signed-off-by: cdenicola <[email protected]>
Co-authored-by: cdenicola <[email protected]>
  • Loading branch information
2 people authored and rodoard committed Aug 26, 2022
1 parent 9277acc commit 6d3f8a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,20 +1068,38 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
//
// err := f.SetSheetRow("Sheet1", "B6", &[]interface{}{"1", nil, 2})
func (f *File) SetSheetRow(sheet, axis string, slice interface{}) error {
return f.setSheetCells(sheet, axis, slice, rows)
}

// SetSheetCol writes an array to column by given worksheet name, starting
// coordinate and a pointer to array type 'slice'. For example, writes an
// array to column B start with the cell B6 on Sheet1:
//
// err := f.SetSheetCol("Sheet1", "B6", &[]interface{}{"1", nil, 2})
func (f *File) SetSheetCol(sheet, axis string, slice interface{}) error {
return f.setSheetCells(sheet, axis, slice, columns)
}

// setSheetCells provides a function to set worksheet cells value.
func (f *File) setSheetCells(sheet, axis string, slice interface{}, dir adjustDirection) error {
col, row, err := CellNameToCoordinates(axis)
if err != nil {
return err
}

// Make sure 'slice' is a Ptr to Slice
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Slice {
return ErrParameterInvalid
}
v = v.Elem()

for i := 0; i < v.Len(); i++ {
cell, err := CoordinatesToCellName(col+i, row)
var cell string
var err error
if dir == rows {
cell, err = CoordinatesToCellName(col+i, row)
} else {
cell, err = CoordinatesToCellName(col, row+i)
}
// Error should never happen here. But keep checking to early detect regressions
// if it will be introduced in the future.
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,23 @@ func TestSharedStrings(t *testing.T) {
assert.NoError(t, f.Close())
}

func TestSetSheetCol(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
t.FailNow()
}

assert.NoError(t, f.SetSheetCol("Sheet1", "B27", &[]interface{}{"cell", nil, int32(42), float64(42), time.Now().UTC()}))

assert.EqualError(t, f.SetSheetCol("Sheet1", "", &[]interface{}{"cell", nil, 2}),
newCellNameToCoordinatesError("", newInvalidCellNameError("")).Error())

assert.EqualError(t, f.SetSheetCol("Sheet1", "B27", []interface{}{}), ErrParameterInvalid.Error())
assert.EqualError(t, f.SetSheetCol("Sheet1", "B27", &f), ErrParameterInvalid.Error())
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetSheetCol.xlsx")))
assert.NoError(t, f.Close())
}

func TestSetSheetRow(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
Expand Down

0 comments on commit 6d3f8a1

Please sign in to comment.