You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package xlsx
import (
"joybuywork/internal/plugins/export"
)
type Option func(e *Export)
// UseDefaultSheet 创建文件开始的默认工作簿
func UseDefaultSheet(name string) Option {
return func(e *Export) {
//旧的默认工作表
index := e.CreateSheet(name)
e.file.SetActiveSheet(index)
//这个是组件自带的,所以删除掉
e.file.DeleteSheet(DefaultSheetName)
e.sheet = name
}
}
type Sheet struct {
Name string //当前sheet的名称
Header export.HeaderField //当前sheet的字段数
SwitchField []SwitchField //切换字段和标识值
Total int64 //当前sheet的写入的总条数,用于自动切sheet,当 SwitchField = nil 的时候才使用该值
writeNum int64 //写入当前sheet的条数
sfIdx int64 //当前sheet从第几行开始写入
fields map[string]string //头部对应的表格列和字段名 map[string]string{"A":"字段key1", "B": "字段key2"}
header map[string]string //表头数据map[string]string{"A1":"表头1", "B1": "表头2"}
}
type SwitchField struct {
Key string //当前sheet的字段标识
Val interface{} //当前sheet的字段标识的值
}
// UseManySheet 多sheet导出
func UseManySheet(ss ...Sheet) Option {
return func(e *Export) {
var defaultIdx = -1
for k, v := range ss {
//工作表存在,代表已经设置过表头,不用再设置了
_ = e.CreateSheet(v.Name)
//初始化
e.initSheet(&v, true)
if defaultIdx == -1 && (v.Total > 0 || v.SwitchField != nil) {
defaultIdx = k
}
//设置他的索引
e.ss = append(e.ss, v)
if v.SwitchField != nil {
if e.sfv == nil {
e.sfv = make(map[string]int)
}
for _, v1 := range v.SwitchField {
e.sfv[e.getSfvKey(v1.Key, v1.Val)] = k
}
}
}
//从第一个有导出数据的的sheet开始
e.initSheet(&ss[defaultIdx], false)
}
}