xlsx.SetCellValue() now supports bool value

This commit is contained in:
dvelderp 2018-01-25 18:06:40 +01:00
parent 541d29f3b2
commit b25ec6e9d3
2 changed files with 46 additions and 0 deletions

31
cell.go
View File

@ -41,6 +41,7 @@ func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
// []byte // []byte
// time.Duration // time.Duration
// time.Time // time.Time
// bool
// nil // nil
// //
// Note that default date format is m/d/yy h:mm of time.Time type value. You can // Note that default date format is m/d/yy h:mm of time.Time type value. You can
@ -63,6 +64,8 @@ func (f *File) SetCellValue(sheet, axis string, value interface{}) {
f.setDefaultTimeStyle(sheet, axis, 22) f.setDefaultTimeStyle(sheet, axis, 22)
case nil: case nil:
f.SetCellStr(sheet, axis, "") f.SetCellStr(sheet, axis, "")
case bool:
f.SetCellBool(sheet, axis, bool(value.(bool)))
default: default:
f.setCellIntValue(sheet, axis, value) f.setCellIntValue(sheet, axis, value)
} }
@ -96,6 +99,34 @@ func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
} }
} }
// SetCellBool provides function to set bool type value of a cell by given
// worksheet name, cell coordinates and cell value.
func (f *File) SetCellBool(sheet, axis string, value bool) {
xlsx := f.workSheetReader(sheet)
axis = f.mergeCellsParser(xlsx, axis)
col := string(strings.Map(letterOnlyMapF, axis))
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
if err != nil {
return
}
xAxis := row - 1
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
completeRow(xlsx, rows, cell)
completeCol(xlsx, rows, cell)
xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
xlsx.SheetData.Row[xAxis].C[yAxis].T = "b"
if value {
xlsx.SheetData.Row[xAxis].C[yAxis].V = "1"
} else {
xlsx.SheetData.Row[xAxis].C[yAxis].V = "0"
}
}
// GetCellValue provides function to get formatted value from cell by given // GetCellValue provides function to get formatted value from cell by given
// worksheet name and axis in XLSX file. If it is possible to apply a format to // worksheet name and axis in XLSX file. If it is possible to apply a format to
// the cell value, it will do so, if not then an error will be returned, along // the cell value, it will do so, if not then an error will be returned, along

View File

@ -81,6 +81,21 @@ func TestOpenFile(t *testing.T) {
xlsx.SetCellValue("Sheet2", "F14", uint32(1<<32-1)) xlsx.SetCellValue("Sheet2", "F14", uint32(1<<32-1))
xlsx.SetCellValue("Sheet2", "F15", uint64(1<<32-1)) xlsx.SetCellValue("Sheet2", "F15", uint64(1<<32-1))
xlsx.SetCellValue("Sheet2", "F16", true) xlsx.SetCellValue("Sheet2", "F16", true)
// Test boolean write
booltest := []struct {
value bool
expected string
}{
{false, "0"},
{true, "1"},
}
for _, test := range booltest {
xlsx.SetCellValue("Sheet2", "F16", test.value)
value := xlsx.GetCellValue("Sheet2", "F16")
if value != test.expected {
t.Errorf(`Expecting result of xlsx.SetCellValue("Sheet2", "F16", %v) to be %v (false), got: %s `, test.value, test.expected, value)
}
}
xlsx.SetCellValue("Sheet2", "G2", nil) xlsx.SetCellValue("Sheet2", "G2", nil)
xlsx.SetCellValue("Sheet2", "G4", time.Now()) xlsx.SetCellValue("Sheet2", "G4", time.Now())
// 02:46:40 // 02:46:40