Documentation
¶
Index ¶
- Constants
- Variables
- func MatchTitlePath[T any](tree *PathTree[T], sheet Sheet, params *Params) (map[int]T, error)
- func MatchTitlePathByStream[T any](tree *PathTree[T], stream StreamSheet, params *Params) (map[int]T, error)
- func NewRowMapper[T any](objType reflect.Type, sheet Sheet, params *Params) (*RowMapper[T], *PathTree[int], error)
- func NewRowMapperByStream[T any](objType reflect.Type, stream StreamSheet, params *Params) (*RowMapper[T], *PathTree[int], error)
- func NewXlsWorkbookByFile(wb xls.Workbook) *xlsWorkbook
- func TitleEscape(t string) string
- func TitleUnescape(t string) (string, error)
- type ColumnMapper
- type Constraint
- type EORMReader
- func (e *EORMReader[T]) All() iter.Seq2[*T, error]
- func (e *EORMReader[T]) CheckValue() error
- func (e *EORMReader[T]) ClrLastError()
- func (e *EORMReader[T]) Current() (t *T, err error)
- func (e *EORMReader[T]) CurrentRowNumber() (int, bool)
- func (e *EORMReader[T]) DataStartRow() int
- func (e *EORMReader[T]) IsMatched() bool
- func (e *EORMReader[T]) IsPerfectMatch() bool
- func (e *EORMReader[T]) IsValid() bool
- func (e *EORMReader[T]) LastError() error
- func (e *EORMReader[T]) Next() bool
- func (e *EORMReader[T]) NoErrorRows() iter.Seq2[int, *T]
- type EORMWriter
- type EscapeError
- type MappingType
- type MatchLevel
- type Option
- func WithFirstRowWildcard() Option
- func WithGenLastLayerNoMerged() Option
- func WithIgnoreOutOfRange() Option
- func WithIgnoreParseError() Option
- func WithIgnoreReadRowError() Option
- func WithMatchLevel(l MatchLevel) Option
- func WithParams(src *Params) Option
- func WithTitleStartRow(r int) Option
- func WithTrimSpace() Option
- func WithWrite() Option
- type Params
- type PathTree
- type Row
- type RowMapper
- type Sheet
- type SheetWriter
- type StreamSheet
- type TitleLayer
- type TitlePath
- type TitlePaths
- type TreeItem
- type Workbook
- func NewWorkbook(filePath string) (Workbook, error)
- func NewWorkbookByReadSeeker(filename string, reader io.ReadSeeker) (Workbook, error)
- func NewXlsWorkbook(filePath string) (Workbook, error)
- func NewXlsWorkbookByReadSeeker(reader io.ReadSeeker) (Workbook, error)
- func NewXlsxWorkbook(filePath string) (Workbook, error)
- func NewXlsxWorkbookByFile(f *excelize.File) (Workbook, error)
- func NewXlsxWorkbookByReadSeeker(reader io.ReadSeeker) (Workbook, error)
- type XlsCell
- func (x XlsCell) IsBlank(data structure.CellData) bool
- func (x XlsCell) IsBool(data structure.CellData) bool
- func (x XlsCell) IsFloat(data structure.CellData) bool
- func (x XlsCell) IsInt(data structure.CellData) bool
- func (x XlsCell) IsString(data structure.CellData) bool
- func (x XlsCell) ToBool(data structure.CellData) (bool, error)
- func (x XlsCell) ToFloat64(data structure.CellData) (float64, error)
- func (x XlsCell) ToInt64(data structure.CellData) (int64, error)
- func (x XlsCell) ToString(data structure.CellData) (string, error)
- func (x XlsCell) Type(data structure.CellData) XlsCellType
- type XlsCellType
Constants ¶
const ( ConstraintDefault = "" ConstraintRequired = "required" ConstraintNotNull = "not_null" )
const ( Version = common.Version(1000) Copyright = "Copyright 2025 [email protected]" )
Variables ¶
var ( ErrNotFound = errors.New("excel: not found") ErrOutOfRange = errors.New("excel: out of range") ErrNil = errors.New("excel: nil") ErrEmptyCell = errors.New("excel: empty cell") ErrInvalidValueType = errors.New("excel: invalid value type") // ErrInvalidCellValue #NULL!, #DIV/0!, #VALUE!, #REF!, #NAME?, #NUM!!, #N/A ErrInvalidCellValue = errors.New("excel: invalid cell value") ErrExcelNotInitialized = errors.New("excel: not initialized") ErrEof = errors.New("excel: eof") ErrParseError = errors.New("cell value parse error") )
var ( ErrEmptyPath = errors.New("eorm: empty path") ErrUnsupported = errors.New("eorm: unsupported") ErrInvalidState = errors.New("eorm: invalid state") ErrRowNotFound = errors.New("eorm: row not found") ErrRequiredColumnNotFound = errors.New("eorm: required column not found") ErrInsufficientMatchLevel = errors.New("eorm: insufficient match level") )
Functions ¶
func MatchTitlePath ¶
MatchTitlePath 将excel文件(sheet)中的表头与 tree 所代表的对象属性中定义的映射路径进行匹配, 从而得到 { ColumnIndex : FieldIndex } 的映射关系 returns column index to value mapping
func MatchTitlePathByStream ¶ added in v0.2.0
func MatchTitlePathByStream[T any](tree *PathTree[T], stream StreamSheet, params *Params) (map[int]T, error)
func NewRowMapper ¶
func NewRowMapperByStream ¶ added in v0.2.0
func NewXlsWorkbookByFile ¶ added in v0.2.1
func TitleEscape ¶
func TitleUnescape ¶
Types ¶
type ColumnMapper ¶
type ColumnMapper struct {
// contains filtered or unexported fields
}
ColumnMapper 记录对象属性与表格列之间的映射关系
func NewColumnMapper ¶ added in v0.2.0
func NewColumnMapper(objType reflect.Type, fieldIdx int, field reflect.StructField, params *Params) (*ColumnMapper, error)
func (*ColumnMapper) CellToField ¶ added in v0.2.0
func (m *ColumnMapper) CellToField(rowData reflect.Value, row Row, columnIndexes []int, params *Params) error
CellToField read data from row and set data to mapped object rowData
func (*ColumnMapper) String ¶
func (m *ColumnMapper) String() string
type Constraint ¶
type Constraint string
func (Constraint) IsValid ¶
func (c Constraint) IsValid() bool
func (Constraint) NeedMapper ¶
func (c Constraint) NeedMapper() bool
func (Constraint) NeedValue ¶
func (c Constraint) NeedValue() bool
func (Constraint) String ¶
func (c Constraint) String() string
type EORMReader ¶ added in v0.2.0
type EORMReader[T any] struct { // contains filtered or unexported fields }
func (*EORMReader[T]) All ¶ added in v0.2.0
func (e *EORMReader[T]) All() iter.Seq2[*T, error]
All 遍历每一行,无论是否有error。无论使用Next()|Current() 还是 All() 或者 NoErrorRows() 只能遍历一次
func (*EORMReader[T]) CheckValue ¶ added in v0.2.0
func (e *EORMReader[T]) CheckValue() error
func (*EORMReader[T]) ClrLastError ¶ added in v0.2.0
func (e *EORMReader[T]) ClrLastError()
func (*EORMReader[T]) Current ¶ added in v0.2.0
func (e *EORMReader[T]) Current() (t *T, err error)
Current 返回当前行的对象
func (*EORMReader[T]) CurrentRowNumber ¶ added in v0.2.0
func (e *EORMReader[T]) CurrentRowNumber() (int, bool)
CurrentRowNumber 返回当前行下标,如果-1,则说明尚未开始遍历,如果-2,表示遍历已完成。 第二返回值表示这个值是否在值范围内,这个值为true时则第一返回值返回了遍历过程中当前值所在sheet的行下标
func (*EORMReader[T]) DataStartRow ¶ added in v0.2.0
func (e *EORMReader[T]) DataStartRow() int
func (*EORMReader[T]) IsMatched ¶ added in v0.2.0
func (e *EORMReader[T]) IsMatched() bool
func (*EORMReader[T]) IsPerfectMatch ¶ added in v0.2.0
func (e *EORMReader[T]) IsPerfectMatch() bool
func (*EORMReader[T]) IsValid ¶ added in v0.2.0
func (e *EORMReader[T]) IsValid() bool
func (*EORMReader[T]) LastError ¶ added in v0.2.0
func (e *EORMReader[T]) LastError() error
func (*EORMReader[T]) Next ¶ added in v0.2.0
func (e *EORMReader[T]) Next() bool
Next 移动到下一行,如果还有行则返回true,否则返回false。 无论使用Next()|Current() 还是 All() 或者 NoErrorRows() 只能遍历一次
func (*EORMReader[T]) NoErrorRows ¶ added in v0.2.0
func (e *EORMReader[T]) NoErrorRows() iter.Seq2[int, *T]
NoErrorRows 遍历未出错的row,及其在表格中的行号(从0开始,不包括表头)。 无论使用Next()|Current() 还是 All() 或者 NoErrorRows() 只能遍历一次
type EORMWriter ¶ added in v0.2.0
type EORMWriter[T any] struct { // contains filtered or unexported fields }
func NewWriter ¶ added in v0.2.0
func NewWriter[T any](w SheetWriter, objType reflect.Type, opts ...Option) (*EORMWriter[T], error)
func (*EORMWriter[T]) Append ¶ added in v0.2.0
func (w *EORMWriter[T]) Append(objs ...*T) (int, error)
Append write all not nil objects from curRowIndex in input order. returns the number of wrote objects.
func (*EORMWriter[T]) CurrentRowIndex ¶ added in v0.2.2
func (w *EORMWriter[T]) CurrentRowIndex() int
func (*EORMWriter[T]) SkipRows ¶ added in v0.2.0
func (w *EORMWriter[T]) SkipRows(n int)
type EscapeError ¶
type EscapeError string
func (EscapeError) Error ¶
func (e EscapeError) Error() string
type MappingType ¶
type MappingType byte
const ( MTString MappingType = iota MTInt64 MTFloat64 MTBool MTStringSlice MTInt64Slice MTFloat64Slice MTBoolSlice MTInvalid )
func NewMappingType ¶
func NewMappingType(typ reflect.Type) (MappingType, error)
func (MappingType) IsSingle ¶
func (mt MappingType) IsSingle() bool
func (MappingType) IsSlice ¶
func (mt MappingType) IsSlice() bool
func (MappingType) IsValid ¶
func (mt MappingType) IsValid() bool
func (MappingType) String ¶
func (mt MappingType) String() string
type MatchLevel ¶
type MatchLevel byte
const ( MatchLevelNone MatchLevel = iota // 无要求 MatchLevelMatched // 存在匹配字段即可 MatchLevelPerfect // 类型所有具有"eorm"标签的字段均已找到匹配的列 )
func (MatchLevel) Formalize ¶
func (l MatchLevel) Formalize() MatchLevel
type Option ¶
type Option func(p *Params)
func WithFirstRowWildcard ¶
func WithFirstRowWildcard() Option
func WithGenLastLayerNoMerged ¶
func WithGenLastLayerNoMerged() Option
func WithIgnoreOutOfRange ¶
func WithIgnoreOutOfRange() Option
func WithIgnoreParseError ¶
func WithIgnoreParseError() Option
func WithIgnoreReadRowError ¶
func WithIgnoreReadRowError() Option
func WithMatchLevel ¶
func WithMatchLevel(l MatchLevel) Option
func WithParams ¶
func WithTitleStartRow ¶
func WithTrimSpace ¶
func WithTrimSpace() Option
type Params ¶
type Params struct {
TrimSpace bool // 是否删除首尾空格,缺省不删除
IgnoreOutOfRange bool // 越界时认为是零值,而不报错
IgnoreReadRowError bool // 迭代sheet的row时,如果出错是否跳过
IgnoreParseError bool // 遇到 ErrParseError 时当作无值处理
GenWildcardForFirstRow bool // 生成 TitlePath 时,通配第一行title
GenLastRowNoMerged bool // 生成TitlePath时,最后一行的空不认为是横向合并
TitleStartRow int // 从哪一行(行号从0开始)开始分析title_path, 所有小于0的值均被认为是0
RequiredMatchLevel MatchLevel // 需要类型与excel表头的匹配程度,无论何值,tag.constraint的要求必须达成
Writable bool // 是否可写
}
type PathTree ¶
type PathTree[T any] struct { // contains filtered or unexported fields }
PathTree 以 root 为根节点的路径树,所有叶子节点都有值,而值类型为 T。树的所有分支深度都为 depth。 实际使用时,PathTree = { TitlePath : FieldIndex }, 也就是以在属性tag中定义的表头路径为键,以该属性在对象中的属性下标为值的键值对。
type Row ¶
type Row interface {
ColumnCount() int
GetColumn(index int) (string, error)
GetInt64Column(index int) (int64, error)
GetFloat64Column(index int) (float64, error)
GetBoolColumn(index int) (bool, error)
AllColumns() iter.Seq2[int, string]
}
Row excel表格中一行数据的抽象类型接口
type RowMapper ¶
type RowMapper[T any] struct { // contains filtered or unexported fields }
RowMapper RowMapper[T]对象的主要功能是把Row转换为一个类型为*T的对象。其中:
* RowMapper.typ属性是T类型的reflect.Type。 * RowMapper.fields保存所有类型T中所有需要映射的属性和信息ColumnMapper * RowMapper.columns保存T中每一个需要映射的属性值需要由Row中哪些列的值构成。
ColumnMapper中保存了属性值的构成方法,分为两种:
* 当ColumnMapper.HasSetter==false时,直接赋值给属性值 * 当ColumnMapper.HasSetter==true时,通过ColumnMapper.Setter保存的*T的方法设置属性值。
无论是哪种方式,值的类型都保存在ColumnMapper.fieldType中,而值类型的Kind()只能是string, int64, float64, bool, []string, [] int64, []float64, []bool之一。
转换方法为RowMapper.Transit(row Row) (*T, error)方法,其步骤为:
- 遍历由对象属性fieldIndex到Row中列columnIndexes的映射表RowMapper.columns,当映射到多列时,也就是len(columnIndexes)> 1,值类型必须是[]string, []int64, []float64, []bool之一。
- 遍历columnIndexes,从row中获取各列对应的值,并转换为ColumnMapper.fieldType的类型,得到fieldValue
- 创建RowMapper.typ类型对应的指针对象rowData
- 当ColumnMapper.HasSetter==false时,将fieldValue直接赋值给rowData对应index为fieldIndex的属性
- 当ColumnMapper.HasSetter==true时,将fieldValue传递给rowData对象对应的ColumnMapper.Setter方法,完成值设置。
- 返回新创建的rowData
func (*RowMapper[T]) IsPerfectMatch ¶
IsPerfectMatch 对象每一个属性都找到了对应列
type SheetWriter ¶ added in v0.2.0
type SheetWriter interface {
GetSheetName() string
// Write write value to the cell(row, column), where both row and column start with 0.
// The valid value types are int64, float64, string, and bool.
Write(row, column int, value any) error
// Stream get StreamSheet of current sheet
Stream() (StreamSheet, error)
}
SheetWriter used to write ORM object to excel sheet
type StreamSheet ¶ added in v0.2.0
type StreamSheet interface {
Next() bool
Current() (Row, error)
CurrentRowNumber() int
Skip(rowCount int) error
Close() error
}
StreamSheet 通过stream模式读取row data的Sheet,在使用完打开的 StreamSheet 之前不应关闭 Workbook。只能按顺序遍历。
type TitleLayer ¶
type TitleLayer[T any] struct { // contains filtered or unexported fields }
TitleLayer 从 PathTree 的根开始,带有传承的记录每一级根据列内容匹配的列和对应的节点 当 key == -1 时,表示所有列都匹配的节点。初始化时的值为 {-1: root}. 使用 PathTree 与实际excel文件表头进行匹配得到 { excel列下表ColumnIndex : 映射对象对应属性下标FieldIndex } 对应关系的过程中, 用 TitleLayer 记录从文件表头自顶向下,每一行匹配后 ColumnIndex 与 PathTree中对应层级的 TreeItem 之间的对应关系。
func NewTitleLayer ¶
func NewTitleLayer[T any](root TreeItem[T]) *TitleLayer[T]
func (*TitleLayer[T]) IsRoot ¶
func (m *TitleLayer[T]) IsRoot() bool
func (*TitleLayer[T]) NextRow ¶
func (m *TitleLayer[T]) NextRow(row Row) (*TitleLayer[T], error)
func (*TitleLayer[T]) Size ¶
func (m *TitleLayer[T]) Size() int
func (*TitleLayer[T]) Values ¶
func (m *TitleLayer[T]) Values() (map[int]T, error)
type TitlePath ¶
type TitlePath []string
TitlePath 用来指定在多级(包括单级)表头中的路径,从而确定指向的列。
func MustTitlePath ¶
type TitlePaths ¶
type TitlePaths []TitlePath
func BuildTitlePaths ¶
func BuildTitlePaths(sheet Sheet, depth int, opts ...Option) (TitlePaths, error)
func (TitlePaths) Info ¶
func (tps TitlePaths) Info() string
type TreeItem ¶
type TreeItem[T any] interface { IsValue() bool HasValue() bool GetValue() T IsBranch() bool HasChild(title string) bool GetChild(title string) TreeItem[T] SetChild(title string, child TreeItem[T]) error ChildrenKeys() []string Depth() (int, error) }
TreeItem 只支持由上而下的树状结构构建的表头,每一个TreeItem都有其明确的父节点
type Workbook ¶
type Workbook interface {
SheetCount() int
GetSheet(index int) (Sheet, error)
GetSheetByName(name string) (Sheet, error)
GetStreamSheet(index int) (StreamSheet, error)
GetStreamSheetByName(name string) (StreamSheet, error)
GetSheetWriter(index int) (SheetWriter, error)
GetSheetWriterByName(name string) (SheetWriter, error)
Save() error
WriteTo(w io.Writer) (int64, error)
Close() error
}
Workbook excel文件的抽象接口类型
func NewWorkbook ¶
func NewWorkbookByReadSeeker ¶
func NewWorkbookByReadSeeker(filename string, reader io.ReadSeeker) (Workbook, error)
func NewXlsWorkbook ¶
func NewXlsWorkbookByReadSeeker ¶
func NewXlsWorkbookByReadSeeker(reader io.ReadSeeker) (Workbook, error)
func NewXlsxWorkbook ¶
func NewXlsxWorkbookByFile ¶ added in v0.2.1
func NewXlsxWorkbookByReadSeeker ¶
func NewXlsxWorkbookByReadSeeker(reader io.ReadSeeker) (Workbook, error)
type XlsCellType ¶
type XlsCellType byte
const ( XlsCellBlank XlsCellType = iota // empty cell XlsCellBoolOrErr // boolean value or error XlsCellFake // fake, not exist. like a placeholder XlsCellString // string value XlsCellFloat // float64 XlsCellInt // int64 XlsCellNil XlsCellUnknown )
