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

close task#8188

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