Documentation ¶
Overview ¶
Package exl
Excel binding to struct written in Go.(Only supports Go1.18+)
Index ¶
- Variables
- func Read[T ReadConfigurator](reader io.Reader, filterFunc ...func(t T) (add bool)) ([]T, error)
- func ReadBinary[T ReadConfigurator](bytes []byte, filterFunc ...func(t T) (add bool)) ([]T, error)
- func ReadExcel(file string, sheetIndex int, walk func(index int, rows *xlsx.Row)) error
- func ReadFile[T ReadConfigurator](file string, filterFunc ...func(t T) (add bool)) ([]T, error)
- func UnmarshalBool(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalExcelUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalFloat(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalString(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalTextUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalTime(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalUInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func Write[T WriteConfigurator](file string, ts []T) error
- func WriteExcel(file string, data [][]string) error
- func WriteExcelTo(w io.Writer, data [][]string) error
- func WriteTo[T WriteConfigurator](w io.Writer, ts []T) error
- type ContentError
- type ExcelUnmarshalParameters
- type ExcelUnmarshaler
- type FieldError
- type ReadConfig
- type ReadConfigurator
- type UnmarshalErrorHandling
- type UnmarshalExcelFunc
- type WriteConfig
- type WriteConfigurator
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( ErrSheetIndexOutOfRange = errors.New("exl: sheet index out of range") ErrHeaderRowIndexOutOfRange = errors.New("exl: header row index out of range") ErrDataStartRowIndexOutOfRange = errors.New("exl: data start row index out of range") ErrNoUnmarshaler = errors.New("no unmarshaler") ErrNoDestinationField = errors.New("no destination field with matching tag") )
var DefaultUnmarshalFuncs = map[reflect.Kind]UnmarshalExcelFunc{ reflect.String: UnmarshalString, reflect.Bool: UnmarshalBool, reflect.Int: UnmarshalInt, reflect.Int8: UnmarshalInt, reflect.Int16: UnmarshalInt, reflect.Int32: UnmarshalInt, reflect.Int64: UnmarshalInt, reflect.Uint: UnmarshalUInt, reflect.Uintptr: UnmarshalUInt, reflect.Uint8: UnmarshalUInt, reflect.Uint16: UnmarshalUInt, reflect.Uint32: UnmarshalUInt, reflect.Uint64: UnmarshalUInt, reflect.Float32: UnmarshalFloat, reflect.Float64: UnmarshalFloat, }
var ErrCannotCastUnmarshaler = errors.New("cannot cast to unmarshaler interface")
ErrCannotCastUnmarshaler is returned in case a field technically implements an unmarshaler interface, but casting to it at runtime failed for some reason.
var ErrNegativeUInt = errors.New("negative integer provided for unsigned field")
var ErrNoRecognizedFormat = errors.New("no recognized format")
var ErrOverflow = errors.New("numeric overflow, number is too large for this field")
Functions ¶
func Read ¶
func Read[T ReadConfigurator](reader io.Reader, filterFunc ...func(t T) (add bool)) ([]T, error)
Read io.Reader each row bind to `T`
func ReadBinary ¶
func ReadBinary[T ReadConfigurator](bytes []byte, filterFunc ...func(t T) (add bool)) ([]T, error)
ReadBinary each row bind to `T`
func ReadFile ¶
func ReadFile[T ReadConfigurator](file string, filterFunc ...func(t T) (add bool)) ([]T, error)
ReadFile each row bind to `T`
func UnmarshalBool ¶ added in v1.3.0
func UnmarshalBool(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalExcelUnmarshaler ¶ added in v1.3.0
func UnmarshalExcelUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalFloat ¶ added in v1.3.0
func UnmarshalFloat(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalInt ¶ added in v1.3.0
func UnmarshalInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalString ¶ added in v1.3.0
func UnmarshalString(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalTextUnmarshaler ¶ added in v1.3.0
func UnmarshalTextUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalTime ¶ added in v1.3.0
func UnmarshalTime(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalUInt ¶ added in v1.3.0
func UnmarshalUInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func Write ¶
func Write[T WriteConfigurator](file string, ts []T) error
Write defines write []T to excel file
params: file,excel file full path
params: typed parameter T, must be implements exl.Bind
func WriteExcel ¶
WriteExcel defines write [][]string to excel
params: file, excel file pull path
params: data, write data to excel
func WriteExcelTo ¶ added in v1.2.2
WriteExcelTo defines write [][]string to excel
params: w, the dist writer
params: data, write data to excel
Types ¶
type ContentError ¶ added in v1.3.0
type ContentError struct { FieldErrors []FieldError LimitReached bool }
func (ContentError) Error ¶ added in v1.3.0
func (e ContentError) Error() string
Error implements error.
func (ContentError) Unwrap ¶ added in v1.3.0
func (e ContentError) Unwrap() []error
Unwrap Error implements the anonymous unwrap interface used by errors.Unwrap and others.
type ExcelUnmarshalParameters ¶ added in v1.3.0
type ExcelUnmarshaler ¶ added in v1.3.0
type ExcelUnmarshaler interface {
UnmarshalExcel(cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
}
type FieldError ¶ added in v1.3.0
type FieldError struct { RowIndex int // 0-based row index. Printed as 1-based row number in error text. ColumnIndex int // 0-based column index. ColumnHeader string Err error }
func (FieldError) Error ¶ added in v1.3.0
func (e FieldError) Error() string
Error implements error.
func (FieldError) Unwrap ¶ added in v1.3.0
func (e FieldError) Unwrap() error
Unwrap Error implements the anonymous unwrap interface used by errors.Unwrap and others.
type ReadConfig ¶ added in v1.2.1
type ReadConfig struct { // The tag name to use when looking for fields in the target struct. // Defaults to "excel". TagName string // The index of the worksheet to be read. // Defaults to 0, the first worksheet. SheetIndex int // The row index at which the column headers are read from. // Zero-based, defaults to 0. HeaderRowIndex int // Start the data reading at this row. // The header row counts as row. // Zero-based, defaults to 1. DataStartRowIndex int // Configure the default string unmarshaler to trim space after reading a cell. // Does not impact any other default unmarshaler, // but is available to custom unmarshalers via ExcelUnmarshalParameters.TrimSpace. // Defaults to false. TrimSpace bool // Fallback date formats for date parsing. // If an Excel cell is to be unmarshalled into a date, // and that cell is either not formatted as Date or contains raw text // (which can happen if Excel does not correctly recognize the date format) // then these formats are used in the order specified to try and parse // the raw cell value into a date. // There are no fallback formats configured by default. FallbackDateFormats []string // Skip reading columns for which no target field is found. // Defaults to true. SkipUnknownColumns bool // Skip reading columns, if there is a target field, // but the target type is unsupported // or caused an error when determining the unmarshaler to use. // Defaults to false. SkipUnknownTypes bool // Configure how errors during unmarshalling are handled. // Unmarshalling errors are e.g. invalid number formats in the cell, // date parsing with invalid input, // or attempting to unmarshal non-numeric text into a numeric field. // Defaults to UnmarshalErrorAbort. UnmarshalErrorHandling UnmarshalErrorHandling // If UnmarshalErrorHandling is configured as UnmarshalErrorCollect, // this option limits the number of errors which are collected before // parsing is aborted. // Configure a limit of 0 to collect all errors, without upper limit. // Defaults to 10. MaxUnmarshalErrors uint64 }
type ReadConfigurator ¶ added in v1.2.1
type ReadConfigurator interface{ ReadConfigure(rc *ReadConfig) }
type UnmarshalErrorHandling ¶ added in v1.3.0
type UnmarshalErrorHandling uint8
const ( // UnmarshalErrorIgnore // Ignore any errors during unmarshalling UnmarshalErrorIgnore UnmarshalErrorHandling = iota // UnmarshalErrorAbort // Abort reading when encountering the first unmarshalling error UnmarshalErrorAbort // UnmarshalErrorCollect // Collect unmarshalling errors up to a limit, but continue reading. // Collected errors are returned as one error at the end, of type UnmarshalErrorCollect )
type UnmarshalExcelFunc ¶ added in v1.3.0
type UnmarshalExcelFunc func(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func GetUnmarshalFunc ¶ added in v1.3.0
func GetUnmarshalFunc(destField reflect.Value) UnmarshalExcelFunc
type WriteConfig ¶ added in v1.2.1
type WriteConfig struct{ SheetName, TagName string }
type WriteConfigurator ¶ added in v1.2.1
type WriteConfigurator interface{ WriteConfigure(wc *WriteConfig) }
type Writer ¶ added in v1.2.0
type Writer struct {
// contains filtered or unexported fields
}
Writer define a writer for exl
func NewWriter ¶ added in v1.2.0
func NewWriter(options ...xlsx.FileOption) *Writer
NewWriter returns new exl writer