Skip to content

Commit

Permalink
feat: add bool parse string
Browse files Browse the repository at this point in the history
  • Loading branch information
VarusHsu committed Jan 28, 2024
1 parent ca33db7 commit e57fe4b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
21 changes: 20 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ type options struct {
floatFmt byte // 小数的格式,默认为'f',详细见 strconv.FormatFloat 的注释
ifNullValue string // null pointer 空值的默认显示
sheetHeaders []SheetModel // 当没有数据时,表头的默认显示
trueValue *string // bool类型的true显示值
falseValue *string // bool类型的false显示值
}

func WithTimeFormatLayout(layout string) Option {
Expand Down Expand Up @@ -225,6 +227,14 @@ func WithSheetHeaders(headers ...SheetModel) Option {
}
}

// WithBoolValueAs 当字段类型为bool时,true和false的显示值
func WithBoolValueAs(trueValue, falseValue string) Option {
return func(options *options) {
options.trueValue = &trueValue
options.falseValue = &falseValue
}
}

func appendRow(f *excelize.File, sheetModel SheetModel, line int, options *options) error {
sheetName := sheetModel.SheetName()
// find if sheetName exists
Expand Down Expand Up @@ -285,8 +295,17 @@ func appendRow(f *excelize.File, sheetModel SheetModel, line int, options *optio
reflect.Float32, reflect.Float64:
value := fieldValue.Interface() // get field value (type interface{})
switch value.(type) { // type assertion
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, bool, string:
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, string:
f.SetCellValue(sheetName, cellName, value) // set cell value
case bool:
b := value.(bool)
if options.trueValue != nil && b {
f.SetCellValue(sheetName, cellName, *options.trueValue)
} else if options.falseValue != nil && !b {
f.SetCellValue(sheetName, cellName, *options.falseValue)
} else {
f.SetCellValue(sheetName, cellName, value)
}
case float32: // convert float32 to string using options
f.SetCellValue(sheetName,
cellName,
Expand Down
65 changes: 56 additions & 9 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ func TestWriteExcel(t *testing.T) {
}

models = append(models, sheet3)
err = WriteExcelSaveAs("test3.xlsx", models)
err = WriteExcelSaveAs("test2.xlsx", models)
assert.EqualError(t, err, "sheetModel must have a sheet name")

sheet4 := Sheet4(1)
models = make([]SheetModel, 0)
models = append(models, sheet4)
err = WriteExcelSaveAs("test4.xlsx", models)
err = WriteExcelSaveAs("test3.xlsx", models)
assert.EqualError(t, err, "sheetModel must be struct")

sheet5 := Sheet5{
Col1: "string",
}
models = make([]SheetModel, 0)
models = append(models, sheet5)
err = WriteExcelSaveAs("test5.xlsx", models)
err = WriteExcelSaveAs("test4.xlsx", models)

sheet6 := Sheet6{
Col1: map[string]string{
Expand All @@ -144,7 +144,7 @@ func TestWriteExcel(t *testing.T) {
}
models = make([]SheetModel, 0)
models = append(models, sheet6)
err = WriteExcelSaveAs("test6.xlsx", models)
err = WriteExcelSaveAs("test5.xlsx", models)
assert.EqualError(t, err, "unsupported type map")

sheet7 := Sheet7{
Expand All @@ -154,7 +154,7 @@ func TestWriteExcel(t *testing.T) {
}
models = make([]SheetModel, 0)
models = append(models, sheet7)
err = WriteExcelSaveAs("test7.xlsx", models)
err = WriteExcelSaveAs("test6.xlsx", models)
assert.EqualError(t, err, "unsupported type excelorm.subStruct")
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func TestWithTimeFormatLayout(t *testing.T) {
var models []SheetModel
models = append(models, sheet1, sheet1, sheet1, sheet1, sheet1, sheet2, sheet2, sheet2, sheet2, sheet2)

err := WriteExcelSaveAs("test.xlsx", models, WithTimeFormatLayout("2006/01/02 15:04:05"))
err := WriteExcelSaveAs("test7.xlsx", models, WithTimeFormatLayout("2006/01/02 15:04:05"))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestWithIfNullValue(t *testing.T) {
var models []SheetModel
models = append(models, sheet1, sheet1, sheet1, sheet1, sheet1, sheet2, sheet2, sheet2, sheet2, sheet2)

err := WriteExcelSaveAs("test.xlsx", models, WithIfNullValue("-"))
err := WriteExcelSaveAs("test8.xlsx", models, WithIfNullValue("-"))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestWithFloatPrecision(t *testing.T) {
var models []SheetModel
models = append(models, sheet1, sheet1, sheet1, sheet1, sheet1, sheet2, sheet2, sheet2, sheet2, sheet2)

err := WriteExcelSaveAs("test.xlsx", models, WithFloatPrecision(10))
err := WriteExcelSaveAs("test9.xlsx", models, WithFloatPrecision(10))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -292,7 +292,54 @@ func TestWithFloatFmt(t *testing.T) {
var models []SheetModel
models = append(models, sheet1, sheet1, sheet1, sheet1, sheet1, sheet2, sheet2, sheet2, sheet2, sheet2)

err := WriteExcelSaveAs("test.xlsx", models, WithFloatFmt('e'))
err := WriteExcelSaveAs("test10.xlsx", models, WithFloatFmt('e'))
if err != nil {
t.Error(err)
}
}

func TestWithBoolValueAs(t *testing.T) {
sheet1 := Sheet1{
Col1: "string",
Col2: 1,
Col3: 1.1,
Col4: true,
Col5: time.Now(),
Col6: nil,
Col7: nil,
Col8: nil,
Col9: nil,
Col10: nil,
}
var a = "string_value"
sheet2 := Sheet2{
Col1: "string",
Col2: 1,
Col3: 1.1,
Col4: false,
Col5: time.Now(),
Col6: &a,
Col7: nil,
Col8: nil,
Col9: nil,
Col10: nil,
}
var models []SheetModel
models = append(models, sheet1, sheet1, sheet1, sheet1, sheet1, sheet2, sheet2, sheet2, sheet2, sheet2)

err := WriteExcelSaveAs("test11.xlsx", models, WithBoolValueAs("是", "否"))
if err != nil {
t.Error(err)
}
err = WriteExcelSaveAs("test12.xlsx", models, WithBoolValueAs("1", "0"))
if err != nil {
t.Error(err)
}
err = WriteExcelSaveAs("test13.xlsx", models, WithBoolValueAs("true", "false"))
if err != nil {
t.Error(err)
}
err = WriteExcelSaveAs("test14.xlsx", models) // default
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit e57fe4b

Please sign in to comment.