提交 7f0f4d89 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

close task#8188

上级 12318e4f
...@@ -6,10 +6,8 @@ version: 1.0 ...@@ -6,10 +6,8 @@ version: 1.0
fields: fields:
- field: field_step_negative # 区间指定步长。 - field: field_step_negative # 区间指定负数步长。
range: z-a:-2 # 9,7,5,4... range: 9-1:-2 # 9,7,5,4...
loop: 1-3
loopfix: "|"
prefix: "[" prefix: "["
postfix: "]\t" postfix: "]\t"
...@@ -25,6 +23,11 @@ fields: ...@@ -25,6 +23,11 @@ fields:
prefix: "[" prefix: "["
postfix: "]\t" postfix: "]\t"
- field: field_value # 引用同文件其他字段进行数学运算。
value: "$field_step_negative * 2 - 1"
prefix: "["
postfix: "]\t"
- field: field_nested_range - field: field_nested_range
from: zentao.number.v1.yaml # 引用用户自定义ranges,存于users目录下。 from: zentao.number.v1.yaml # 引用用户自定义ranges,存于users目录下。
use: medium use: medium
......
...@@ -28,7 +28,7 @@ func Generate(defaultFile string, configFile string, fieldsToExportStr, format, ...@@ -28,7 +28,7 @@ func Generate(defaultFile string, configFile string, fieldsToExportStr, format,
return return
} }
if format == constant.FormatExcel { if format == constant.FormatExcel || format == constant.FormatCsv {
gen.Write(rows, format, table, colIsNumArr, fieldsToExport) gen.Write(rows, format, table, colIsNumArr, fieldsToExport)
} else { } else {
gen.Print(rows, format, table, colIsNumArr, fieldsToExport) gen.Print(rows, format, table, colIsNumArr, fieldsToExport)
......
...@@ -37,7 +37,7 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool, ...@@ -37,7 +37,7 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool,
col = stringUtils.AddPad(col, field) col = stringUtils.AddPad(col, field)
} }
if j > 0 && vari.Human { if j > 0 && vari.Human { // use a tab
lineForText = strings.TrimRight(lineForText, "\t") lineForText = strings.TrimRight(lineForText, "\t")
col = strings.TrimLeft(col, "\t") col = strings.TrimLeft(col, "\t")
......
package gen package gen
import ( import (
"fmt" "encoding/csv"
"github.com/360EntSecGroup-Skylar/excelize/v2" "github.com/360EntSecGroup-Skylar/excelize/v2"
constant "github.com/easysoft/zendata/src/utils/const" constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log" logUtils "github.com/easysoft/zendata/src/utils/log"
...@@ -13,6 +13,9 @@ import ( ...@@ -13,6 +13,9 @@ import (
const ( const (
sheetName = "Sheet1" sheetName = "Sheet1"
) )
var (
csvWriter *csv.Writer
)
func Write(rows [][]string, format string, table string, colIsNumArr []bool, func Write(rows [][]string, format string, table string, colIsNumArr []bool,
fields []string) (lines []interface{}) { fields []string) (lines []interface{}) {
...@@ -23,23 +26,41 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool, ...@@ -23,23 +26,41 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool,
if format == constant.FormatExcel { if format == constant.FormatExcel {
printExcelHeader(fields, f) printExcelHeader(fields, f)
} else if format == constant.FormatCsv {
csvWriter = csv.NewWriter(logUtils.FileWriter)
} }
for i, row := range rows { csvData := make([][]string, 0)
for j, col := range row { for i, cols := range rows {
csvRow := make([]string, 0)
for j, col := range cols {
col = replacePlaceholder(col) col = replacePlaceholder(col)
field := vari.TopFieldMap[fields[j]] field := vari.TopFieldMap[fields[j]]
if field.Length > runewidth.StringWidth(col) { if field.Length > runewidth.StringWidth(col) {
col = stringUtils.AddPad(col, field) col = stringUtils.AddPad(col, field)
} }
if format == constant.FormatExcel {
colName, _ := excelize.CoordinatesToCellName(j + 1, i + 2) colName, _ := excelize.CoordinatesToCellName(j + 1, i + 2)
f.SetCellValue(sheetName, colName, col) f.SetCellValue(sheetName, colName, col)
} else if format == constant.FormatCsv {
csvRow = append(csvRow, col)
} }
} }
csvData = append(csvData, csvRow)
}
if err := f.SaveAs(logUtils.FilePath); err != nil { var err error
fmt.Println(err) if format == constant.FormatExcel {
err = f.SaveAs(logUtils.FilePath)
} else if format == constant.FormatCsv {
err = csvWriter.WriteAll(csvData)
csvWriter.Flush()
}
if err != nil {
logUtils.PrintErrMsg(err.Error())
} }
return return
......
...@@ -72,6 +72,7 @@ type FieldSimple struct { ...@@ -72,6 +72,7 @@ type FieldSimple struct {
Field string `yaml:"field"` Field string `yaml:"field"`
Note string `yaml:"note"` Note string `yaml:"note"`
Range string `yaml:"range"` Range string `yaml:"range"`
Value string `yaml:"value"`
Prefix string `yaml:"prefix"` Prefix string `yaml:"prefix"`
Postfix string `yaml:"postfix"` Postfix string `yaml:"postfix"`
Loop string `yaml:"loop"` Loop string `yaml:"loop"`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册