Skip to content

Commit b25ec6e

Browse files
author
dvelderp
committed
xlsx.SetCellValue() now supports bool value
1 parent 541d29f commit b25ec6e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cell.go

+31
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
4141
// []byte
4242
// time.Duration
4343
// time.Time
44+
// bool
4445
// nil
4546
//
4647
// 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{}) {
6364
f.setDefaultTimeStyle(sheet, axis, 22)
6465
case nil:
6566
f.SetCellStr(sheet, axis, "")
67+
case bool:
68+
f.SetCellBool(sheet, axis, bool(value.(bool)))
6669
default:
6770
f.setCellIntValue(sheet, axis, value)
6871
}
@@ -96,6 +99,34 @@ func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
9699
}
97100
}
98101

102+
// SetCellBool provides function to set bool type value of a cell by given
103+
// worksheet name, cell coordinates and cell value.
104+
func (f *File) SetCellBool(sheet, axis string, value bool) {
105+
xlsx := f.workSheetReader(sheet)
106+
axis = f.mergeCellsParser(xlsx, axis)
107+
col := string(strings.Map(letterOnlyMapF, axis))
108+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
109+
if err != nil {
110+
return
111+
}
112+
xAxis := row - 1
113+
yAxis := TitleToNumber(col)
114+
115+
rows := xAxis + 1
116+
cell := yAxis + 1
117+
118+
completeRow(xlsx, rows, cell)
119+
completeCol(xlsx, rows, cell)
120+
121+
xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
122+
xlsx.SheetData.Row[xAxis].C[yAxis].T = "b"
123+
if value {
124+
xlsx.SheetData.Row[xAxis].C[yAxis].V = "1"
125+
} else {
126+
xlsx.SheetData.Row[xAxis].C[yAxis].V = "0"
127+
}
128+
}
129+
99130
// GetCellValue provides function to get formatted value from cell by given
100131
// worksheet name and axis in XLSX file. If it is possible to apply a format to
101132
// the cell value, it will do so, if not then an error will be returned, along

excelize_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ func TestOpenFile(t *testing.T) {
8181
xlsx.SetCellValue("Sheet2", "F14", uint32(1<<32-1))
8282
xlsx.SetCellValue("Sheet2", "F15", uint64(1<<32-1))
8383
xlsx.SetCellValue("Sheet2", "F16", true)
84+
// Test boolean write
85+
booltest := []struct {
86+
value bool
87+
expected string
88+
}{
89+
{false, "0"},
90+
{true, "1"},
91+
}
92+
for _, test := range booltest {
93+
xlsx.SetCellValue("Sheet2", "F16", test.value)
94+
value := xlsx.GetCellValue("Sheet2", "F16")
95+
if value != test.expected {
96+
t.Errorf(`Expecting result of xlsx.SetCellValue("Sheet2", "F16", %v) to be %v (false), got: %s `, test.value, test.expected, value)
97+
}
98+
}
8499
xlsx.SetCellValue("Sheet2", "G2", nil)
85100
xlsx.SetCellValue("Sheet2", "G4", time.Now())
86101
// 02:46:40

0 commit comments

Comments
 (0)