Documentation
¶
Index ¶
- Constants
- Variables
- type Axis
- type Container
- type Excel
- func (e *Excel) AddTable(table *Table) error
- func (e *Excel) DeleteTable(name string) error
- func (e *Excel) DeleteTableContent(name string) error
- func (e *Excel) GetActiveSheet() *Sheet
- func (e *Excel) GetSheet(name string) *Sheet
- func (e *Excel) GetSheetFromIndex(index int) *Sheet
- func (e *Excel) GetTable(name string) (*Table, error)
- func (e *Excel) GetTableSheet(name string) (*Sheet, error)
- func (e *Excel) GetTables() ([]Table, error)
- func (e *Excel) Marshal(container any, tags ...map[string]*Tags) error
- func (e *Excel) ResizeTable(table *Table, newRange string) error
- func (e *Excel) SetActiveSheet(sheet *Sheet)
- func (e *Excel) SetActiveSheetName(name string) error
- func (e *Excel) SetAxis(axis string)
- func (e *Excel) SetAxisCoordinates(col int, row int)
- func (e *Excel) SetSheet(sheet *Sheet)
- func (e *Excel) SetSheetFromIndex(index int)
- func (e *Excel) SetSheetFromName(name string)
- func (e *Excel) Sheet() *Sheet
- func (e *Excel) Unmarshal(container any, tags ...map[string]*Tags) error
- type Field
- func (f *Field) GetReadColumnName() string
- func (f *Field) GetReadDefault() interface{}
- func (f *Field) GetReadEncoding() string
- func (f *Field) GetReadFormat() string
- func (f *Field) GetReadIgnore() bool
- func (f *Field) GetReadRequired() bool
- func (f *Field) GetReadSplit() string
- func (f *Field) GetWriteColumnName() string
- func (f *Field) GetWriteDefault() interface{}
- func (f *Field) GetWriteEncoding() string
- func (f *Field) GetWriteFormat() string
- func (f *Field) GetWriteIgnore() bool
- func (f *Field) GetWriteRequired() bool
- func (f *Field) GetWriteSplit() string
- type Fields
- type IReadTags
- type IReader
- type ITags
- type IWriteTags
- type IWriter
- type MapWriter
- type Marshaller
- type Range
- func (r *Range) AddColumns(columns int) error
- func (r *Range) AddRows(rows int) error
- func (r *Range) ColumnAsRange(column int) (*Range, error)
- func (r *Range) Columns() int
- func (r *Range) FirstColumnAsRange() (*Range, error)
- func (r *Range) FirstRowAsRange() (*Range, error)
- func (r *Range) LastColumnAsRange() (*Range, error)
- func (r *Range) LastRowAsRange() (*Range, error)
- func (r *Range) RemoveColumns(columns int) error
- func (r *Range) RemoveRows(rows int) error
- func (r *Range) RowAsRange(row int) (*Range, error)
- func (r *Range) Rows() int
- func (r *Range) SetColumns(columns int) error
- func (r *Range) SetRows(rows int) error
- func (r *Range) ToRef() string
- func (r *Range) UpdateNames() error
- type Reader
- type ReaderResult
- type Sheet
- type SliceReader
- type SliceWriter
- type Struct
- type StructReader
- type StructWriter
- type Table
- func (t *Table) Delete() error
- func (t *Table) DeleteContent() error
- func (t *Table) GetColumn(title string) (int, error)
- func (t *Table) GetColumnAt(index int) (string, error)
- func (t *Table) GetDataRange() (*Range, error)
- func (t *Table) GetHeaderRange() (*Range, error)
- func (t *Table) GetRange() (*Range, error)
- func (t *Table) IsValid() bool
- func (t *Table) IsValidError() error
- func (t *Table) Resize(newRange string) error
- type Tags
- type Unmarshaller
- type Writer
- type WriterResult
Constants ¶
const ( TagKeyMain = "excel" TagKeyIn = TagKeyMain + "-in" TagKeyOut = TagKeyMain + "-out" TagColumn = "column" TagDefault = "default" TagFormat = "format" TagEncoding = "encoding" TagSplit = "split" TagRequired = "required" TagIgnore = "-" )
Variables ¶
var ( // File errors ErrFileIsNil = errors.New("excel: the file is nil") ErrAxisNotValid = errors.New("excel: the axis is not valid") ErrConfigNotValid = errors.New("excel: the configuration is not valid") // Sheet errors ErrSheetIsNil = errors.New("excel: the sheet is nil") ErrSheetNotValid = errors.New("excel: the sheet name is not valid") ErrSheetNotFound = errors.New("excel: the sheet is not found") ErrSheetNameEmpty = errors.New("excel: the sheet name is empty") ErrSheetIndex = errors.New("excel: the sheet index is not valid") // Table errors ErrTableNameEmpty = errors.New("excel: the table name is empty") ErrTableRange = errors.New("excel: the table range is not valid") // Container errors ErrMapKeyNotString = errors.New("excel: the map key must be a string") ErrNoReaderFound = errors.New("excel: unable to create an appropriate reader") ErrNoWriterFound = errors.New("excel: unable to create an appropriate writer") ErrContainerNotSlice = errors.New("excel: the Container must be a slice") ErrContainerNotMap = errors.New("excel: the Container must be a map") ErrContainerInvalid = errors.New("excel: the Container must be a slice or a pointer") // Column errors ErrColumnRequired = errors.New("excel: required colum") // General errors ErrNotImplemented = errors.New("excel: not implemented") )
Error definitions for the excel package. These errors are returned by various functions in the package to indicate specific failure conditions.
Functions ¶
This section is empty.
Types ¶
type Container ¶ added in v0.4.0
Container is a struct that contains the Value and type of the container it is used to create the appropriate reader or writer
type Excel ¶
Excel is the main structure that provides functionality for reading from and writing to Excel files. It contains references to the underlying excelize.File, Reader, Writer, and Struct components.
Thread Safety: The Excel struct is not thread-safe by default. If multiple goroutines need to access the same Excel instance concurrently, external synchronization must be provided. Each Excel instance should be used by only one goroutine at a time, or proper synchronization mechanisms (like sync.Mutex) should be used to coordinate access.
func NewReader ¶
NewReader creates a new Excel reader from an existing excelize.File. It returns an error if the file is nil. The returned Excel instance can be used to unmarshal Excel data into Go structures.
Note: The returned Excel instance is not thread-safe. If it needs to be used concurrently by multiple goroutines, external synchronization is required.
func NewWriter ¶
NewWriter creates a new Excel writer from an existing excelize.File. It returns an error if the file is nil. The returned Excel instance can be used to marshal Go structures into Excel data.
Note: The returned Excel instance is not thread-safe. If it needs to be used concurrently by multiple goroutines, external synchronization is required.
func (*Excel) DeleteTable ¶ added in v0.6.0
DeleteTable deletes the table in the Excel file
func (*Excel) DeleteTableContent ¶ added in v0.6.0
DeleteTableContent deletes the content of the table in the Excel file
func (*Excel) GetActiveSheet ¶ added in v0.6.0
GetActiveSheet returns the active sheet
func (*Excel) GetSheetFromIndex ¶ added in v0.6.0
GetSheetFromIndex returns the sheet object
func (*Excel) GetTableSheet ¶ added in v0.6.0
GetTableSheet returns the sheet where the table is located
func (*Excel) Marshal ¶
Marshal writes the provided container into the Excel file. The container must be a pointer to a slice of structs, maps, or slices. Optional tags can be provided to customize the marshaling process. It returns an error if the Excel configuration is invalid or if marshaling fails.
Thread Safety: This method is not thread-safe. If multiple goroutines need to call Marshal on the same Excel instance concurrently, external synchronization must be provided.
func (*Excel) ResizeTable ¶ added in v0.6.0
ResizeTable resize the table by changing the range
func (*Excel) SetActiveSheet ¶ added in v0.6.0
SetActiveSheet sets the active sheet
func (*Excel) SetActiveSheetName ¶ added in v0.8.0
SetActiveSheetName sets the name of the active sheet
func (*Excel) SetAxisCoordinates ¶
SetAxisCoordinates sets the axis coordinates to be used by the reader or writer
func (*Excel) SetSheetFromIndex ¶ added in v0.6.0
SetSheetFromIndex sets the sheet index to be used by the reader or writer
func (*Excel) SetSheetFromName ¶ added in v0.6.0
SetSheetFromName sets the sheet name to be used by the reader or writer
func (*Excel) Unmarshal ¶
Unmarshal reads the Excel file and unmarshals it into the provided container. The container must be a pointer to a slice of structs, maps, or slices. Optional tags can be provided to customize the unmarshaling process. It returns an error if the Excel configuration is invalid or if unmarshaling fails.
Thread Safety: This method is not thread-safe. If multiple goroutines need to call Unmarshal on the same Excel instance concurrently, external synchronization must be provided.
type Field ¶ added in v0.4.0
type Field struct {
Name string
Index int
Type reflect.Type
MainTags *Tags // Tags used by default
ReadTags *Tags // Tags for reading
WriteTags *Tags // Tags for writing
}
Field is a struct used to store the information of a field of a struct
func (*Field) GetReadColumnName ¶ added in v0.4.0
GetReadColumnName returns the column name to read from the excel file
func (*Field) GetReadDefault ¶ added in v0.4.0
func (f *Field) GetReadDefault() interface{}
GetReadDefault returns the default value to use if the cell is empty
func (*Field) GetReadEncoding ¶ added in v0.4.0
GetReadEncoding returns the encoding to use when reading the cell
func (*Field) GetReadFormat ¶ added in v0.4.0
GetReadFormat returns the format to use when reading the cell
func (*Field) GetReadIgnore ¶ added in v0.4.0
GetReadIgnore returns whether the field should be ignored when reading the cell
func (*Field) GetReadRequired ¶ added in v0.4.0
GetReadRequired returns whether the field is required when reading the cell
func (*Field) GetReadSplit ¶ added in v0.4.0
GetReadSplit returns the split to use when reading the cell
func (*Field) GetWriteColumnName ¶ added in v0.4.0
GetWriteColumnName returns the column name to write to the excel file
func (*Field) GetWriteDefault ¶ added in v0.4.0
func (f *Field) GetWriteDefault() interface{}
GetWriteDefault returns the default value to use if the cell is empty
func (*Field) GetWriteEncoding ¶ added in v0.4.0
GetWriteEncoding returns the encoding to use when writing the cell
func (*Field) GetWriteFormat ¶ added in v0.4.0
GetWriteFormat returns the format to use when writing the cell
func (*Field) GetWriteIgnore ¶ added in v0.4.0
GetWriteIgnore returns whether the field should be ignored when writing the cell
func (*Field) GetWriteRequired ¶ added in v0.4.0
GetWriteRequired returns whether the field is required when writing the cell
func (*Field) GetWriteSplit ¶ added in v0.4.0
GetWriteSplit returns the split to use when writing the cell
type Fields ¶ added in v0.4.0
type Fields []*Field
Fields is a list of Field
func (*Fields) CountReadIgnored ¶ added in v0.4.0
CountReadIgnored returns the number of ignored fields
func (*Fields) CountWriteIgnored ¶ added in v0.4.0
CountWriteIgnored returns the number of ignored fields
type IReadTags ¶ added in v0.4.0
The IReadTags interface can be used as a replacement of the mainTags parameters when importing an Excel file.
GetTagsIn is used when importing an Excel file and will be used if ITags is not implemented.
type IReader ¶ added in v0.4.0
type IReader interface {
// Unmarshall reads Excel data and converts it into a Go structure.
// Returns a ReaderResult containing information about the read operation
// and an error if the operation fails.
Unmarshall() (*ReaderResult, error)
// SetColumnsTags sets custom tags for columns to control the unmarshaling process.
SetColumnsTags(tags map[string]*Tags)
}
IReader interface defines the contract for all Excel readers. All readers must implement this interface to provide consistent functionality for unmarshaling Excel data into Go structures.
type ITags ¶ added in v0.4.0
The ITags interface can be used as a replacement of the mainTags parameters. The GetTags method must return a map of the mainTags. The key of the map is the name of the field and the value is a Tags structure.
Example:
type Named struct {
Column1 string `excel:"column=MyColumn1"`
Column2 string `excel:"column=MyColumn2;required"`
Column3 string `excel:"column=MyColumn3;default=Hello World"`
}
func (s *Named) GetTags() map[string]excel.MainTags {
return map[string]excel.MainTags{
"Column1": excel.MainTags{column: "MyColumn1"},
"Column2": excel.MainTags{column: "MyColumn2", Required: true},
"Column3": excel.MainTags{column: "MyColumn3", Default: "Hello World"},
}
}
In this example: the Column1 field will be mapped to the "MyColumn1" column of the Excel file. The Column2 field will be mapped to the "MyColumn2" column of the Excel file and it will be required. The Column3 field will be mapped to the "MyColumn3" column of the Excel file and it will have a default value of "Hello World".
type IWriteTags ¶ added in v0.4.0
The IWriteTags interface can be used as a replacement of the mainTags parameters when exporting an Excel file.
GetTagsOut is used when exporting an Excel file and will be used if ITags is not implemented.
type IWriter ¶ added in v0.4.0
type IWriter interface {
// Marshall converts a Go structure into Excel data and writes it to the file.
// Returns a WriterResult containing information about the write operation
// and an error if the operation fails.
Marshall(data any) (*WriterResult, error)
// SetColumnsTags sets custom tags for columns to control the marshaling process.
SetColumnsTags(tags map[string]*Tags)
}
IWriter interface defines the contract for all Excel writers. All writers must implement this interface to provide consistent functionality for marshaling Go structures into Excel data.
type MapWriter ¶ added in v0.4.0
type MapWriter struct {
Writer *Writer
// contains filtered or unexported fields
}
func (*MapWriter) Marshall ¶ added in v0.4.0
func (w *MapWriter) Marshall(data any) (*WriterResult, error)
func (*MapWriter) SetColumnsTags ¶ added in v0.4.0
type Marshaller ¶
type Marshaller interface {
Marshall() (interface{}, error)
}
Marshaller can be implemented by any Value that has a Marshal method This converter is used to convert the Value to the desired representation
type Range ¶ added in v0.6.0
type Range struct {
// StartColumn is the start column of the range
StartColumn int
// StartRow is the start row of the range
StartRow int
// StartName is the start name of the range
StartName string
// EndColumn is the end column of the range
EndColumn int
// EndRow is the end row of the range
EndRow int
// EndName is the end name of the range
EndName string
}
Range represent the range in the Excel file where data will read or write
func (*Range) AddColumns ¶ added in v0.6.0
AddColumns adds columns to the range
func (*Range) ColumnAsRange ¶ added in v0.6.0
ColumnAsRange returns the range of the column
func (*Range) FirstColumnAsRange ¶ added in v0.6.0
FirstColumnAsRange returns the range of the first column
func (*Range) FirstRowAsRange ¶ added in v0.6.0
FirstRowAsRange returns the range of the first row
func (*Range) LastColumnAsRange ¶ added in v0.6.0
LastColumnAsRange returns the range of the last column
func (*Range) LastRowAsRange ¶ added in v0.6.0
LastRowAsRange returns the range of the last row
func (*Range) RemoveColumns ¶ added in v0.6.0
RemoveColumns removes columns from the range
func (*Range) RemoveRows ¶ added in v0.6.0
RemoveRows removes rows from the range
func (*Range) RowAsRange ¶ added in v0.6.0
RowAsRange returns the range of the row
func (*Range) SetColumns ¶ added in v0.6.0
SetColumns sets the number of columns in the range
func (*Range) UpdateNames ¶ added in v0.6.0
UpdateNames updates the name of the range
type Reader ¶
type Reader struct {
Sheet Sheet
Axis Axis
Result *ReaderResult
// contains filtered or unexported fields
}
Reader is the base Excel reader that provides common functionality for all specific reader implementations (struct, slice, map).
type ReaderResult ¶ added in v0.5.0
ReaderResult contains information about the result of a read operation, including the number of rows and columns processed.
type Sheet ¶
Sheet represent the sheet in the Excel file where data will read or write
func (*Sheet) GetComment ¶ added in v0.6.0
GetComment returns the comment of the cell
func (*Sheet) IsValidError ¶ added in v0.6.0
IsValidError returns an error if the sheet is not valid
type SliceReader ¶ added in v0.4.0
type SliceReader struct {
Reader *Reader
// contains filtered or unexported fields
}
func (*SliceReader) SetColumnsTags ¶ added in v0.4.0
func (r *SliceReader) SetColumnsTags(_ map[string]*Tags)
func (*SliceReader) Unmarshall ¶ added in v0.4.0
func (r *SliceReader) Unmarshall() (*ReaderResult, error)
type SliceWriter ¶ added in v0.4.0
type SliceWriter struct {
Writer *Writer
// contains filtered or unexported fields
}
func (*SliceWriter) Marshall ¶ added in v0.4.0
func (w *SliceWriter) Marshall(data any) (*WriterResult, error)
func (*SliceWriter) SetColumnsTags ¶ added in v0.4.0
func (w *SliceWriter) SetColumnsTags(_ map[string]*Tags)
type StructReader ¶ added in v0.4.0
type StructReader struct {
Reader *Reader
Struct *Struct
// contains filtered or unexported fields
}
StructReader is the Excel reader for a struct It implements the IReader interface
func (*StructReader) SetColumnsTags ¶ added in v0.4.0
func (r *StructReader) SetColumnsTags(tags map[string]*Tags)
func (*StructReader) Unmarshall ¶ added in v0.4.0
func (r *StructReader) Unmarshall() (*ReaderResult, error)
Unmarshall reads the excel file and fill the container
type StructWriter ¶ added in v0.4.0
type StructWriter struct {
Writer *Writer
Struct *Struct
// contains filtered or unexported fields
}
StructWriter is the Excel writer for a struct It implements the IWriter interface
func (*StructWriter) Marshall ¶ added in v0.4.0
func (w *StructWriter) Marshall(data any) (*WriterResult, error)
Marshall writes the Excel file from the container
func (*StructWriter) SetColumnsTags ¶ added in v0.4.0
func (w *StructWriter) SetColumnsTags(tags map[string]*Tags)
type Table ¶ added in v0.6.0
Table represent the table in the Excel file
func (*Table) DeleteContent ¶ added in v0.6.0
DeleteContent deletes the content of the table
func (*Table) GetColumnAt ¶ added in v0.6.0
GetColumnAt returns the column name at the desired index
func (*Table) GetDataRange ¶ added in v0.6.0
GetDataRange returns the range of the data of the table
func (*Table) GetHeaderRange ¶ added in v0.6.0
GetHeaderRange returns the range of the header of the table
func (*Table) IsValidError ¶ added in v0.6.0
IsValidError returns an error if the table is not valid
type Tags ¶ added in v0.4.0
type Tags struct {
Column string
Default interface{}
Format string
Encoding string
Split string
Required bool
Ignore bool
// contains filtered or unexported fields
}
Tags is used to store the mainTags parameters of a field.
The mainTags parameters are defined in the struct definition and are prefixed by "excel" and are used to configure the import and export of an Excel file.
Example:
type Named struct {
Column1 string `excel:"column=MyColumn1"`
Column2 string `excel:"column=MyColumn2;required"`
Column3 string `excel:"column=MyColumn3;default=Hello World"`
}
In this example: the Column1 field will be mapped to the "MyColumn1" column of the Excel file. The Column2 field will be mapped to the "MyColumn2" column of the Excel file and it will be required. The Column3 field will be mapped to the "MyColumn3" column of the Excel file and it will have a default value of "Hello World".
type Unmarshaller ¶
Unmarshaller can be implemented by any Value that has an Unmarshall method This converter is used to convert the Value to the desired representation
type Writer ¶
type Writer struct {
Sheet Sheet
Axis Axis
Result *WriterResult
// contains filtered or unexported fields
}
Writer is the base Excel writer that provides common functionality for all specific writer implementations (struct, slice, map).
type WriterResult ¶ added in v0.5.0
WriterResult contains information about the result of a write operation, including the number of rows and columns processed.