Documentation ¶
Overview ¶
table provides a struct to handle tabular data.
Index ¶
- Constants
- Variables
- func SimpleHTMLPage(body string) string
- func SimpleTable(table Table) string
- func SimpleTablePreescaped(table Table) string
- func StreamSimpleTable(qw422016 *qt422016.Writer, table Table)
- func StreamSimpleTablePreescaped(qw422016 *qt422016.Writer, table Table)
- func WriteCSVSimple(cols []string, rows [][]string, filename string) error
- func WriteSimpleTable(qq422016 qtio422016.Writer, table Table)
- func WriteSimpleTablePreescaped(qq422016 qtio422016.Writer, table Table)
- func WriteXLSX(path string, tbls []*Table) error
- func WriteXLSXInterface(filename string, sheetdatas ...SheetData) error
- type ColumnDefinition
- type ColumnDefinitions
- type Columns
- func (cols Columns) CellFloat64(colName string, row []string, defaultIfEmpty bool, def float64) (float64, error)
- func (cols Columns) CellInt(colName string, row []string, defaultIfEmpty bool, def int) (int, error)
- func (cols Columns) CellString(colName string, row []string, defaultIfEmpty bool, def string) (string, error)
- func (cols Columns) CellTime(colName, timeFormat string, row []string, defaultIfEmpty bool, def time.Time) (time.Time, error)
- func (cols Columns) CellUint(colName string, row []string, defaultIfEmpty bool, def uint) (uint, error)
- func (cols Columns) CellsString(colNames []string, row []string, useDefault bool, def string) ([]string, error)
- func (cols Columns) Equal(c Columns) bool
- func (cols Columns) Index(colName string) int
- func (cols Columns) MustCellString(colName string, row []string) string
- func (cols Columns) MustCellsString(colNames []string, row []string) []string
- func (cols Columns) RowMap(row []string, omitEmpty bool) map[string]string
- type ParseOptions
- type SheetData
- type Table
- func NewTable(name string) Table
- func NewTableMapStringInt(tableName, colNameKey, colNameVal string, m map[string]int) Table
- func ParseReadSeeker(opts *ParseOptions, rs io.ReadSeeker) (Table, error)
- func ParseTableXLSX(f *excelize.File, sheetName string, headerRowCount uint, trimSpace bool) (*Table, error)
- func ParseTableXLSXIndex(f *excelize.File, sheetIdx uint, headerRowCount uint, trimSpace bool) (*Table, error)
- func ReadFile(opts *ParseOptions, filenames ...string) (Table, error)
- func ReadTableSimple(filename, sep string) (*Table, error)
- func ReadTableXLSXFile(filename, sheetName string, headerRowCount uint, trimSpace bool) (*Table, error)
- func ReadTableXLSXIndexFile(filename string, sheetIdx uint, headerRowCount uint, trimSpace bool) (*Table, error)
- func (tbl *Table) BuildFloat64(skipEmpty bool) error
- func (tbl *Table) ColumnExpandPivot(colIdx uint, split bool, ...) (map[int]string, error)
- func (tbl *Table) ColumnSumFloat64(colIdx uint) (float64, error)
- func (tbl *Table) ColumnValues(colIdx uint, unique, sortResults bool) ([]string, error)
- func (tbl *Table) ColumnValuesCounts(colIdx uint, trimSpace, includeEmpty, lowerCase bool) map[string]int
- func (tbl *Table) ColumnValuesCountsByName(colName string, trimSpace, includeEmpty, lowerCase bool) (map[string]int, error)
- func (tbl *Table) ColumnValuesForColumnName(colName string, dedupeValues, sortValues bool) ([]string, error)
- func (tbl *Table) ColumnValuesMinMax(colIdx uint) (string, string, error)
- func (tbl *Table) ColumnValuesName(colName string, unique, sortResults bool) ([]string, error)
- func (tbl *Table) ColumnValuesSplit(colIdx uint, split bool, sep string, unique, sortResults bool) ([]string, map[string]int, error)
- func (tbl *Table) ColumnsValuesDistinct(wantColNames []string, stripSpace bool) (map[string]int, error)
- func (tbl *Table) FilterColumnDistinctFirstTable(colIdx int) *Table
- func (tbl *Table) FilterColumnValuesRows(wantColNameValues map[string]string) ([][]string, error)
- func (tbl *Table) FilterColumnValuesTable(wantColNameValues map[string]string) (Table, error)
- func (tbl *Table) FormatColumn(colIdx uint, conv func(cellVal string) (string, error), ...) error
- func (tbl *Table) FormatColumns(colIdxMin uint, colIdxMax int, conv func(cellVal string) (string, error), ...) error
- func (tbl *Table) FormatRows(colIdxMinInc, colIdxMaxInc int, conv func(cellVal string) (string, error)) error
- func (tbl *Table) FormatterFunc() func(val string, colIdx uint) (any, error)
- func (tbl *Table) IsWellFormed() (isWellFormed bool, columnCount int, mismatchRows []int)
- func (tbl *Table) LoadColumnDefinitions(colDefs *ColumnDefinitions)
- func (tbl *Table) LoadMergedRows(data [][]string)
- func (tbl *Table) Markdown(newline string, escPipe bool) string
- func (tbl *Table) Pivot(colCount uint, haveColumns bool) (Table, error)
- func (tbl *Table) RowCellCounts() map[uint]uint
- func (tbl *Table) RowsToMap(keyIdx, valIdx uint) (map[string]string, error)
- func (tbl *Table) String(comma rune, useCRLF bool) (string, error)
- func (tbl *Table) ToDocuments() []map[string]any
- func (tbl *Table) ToHTML(escapeHTML bool) string
- func (tbl *Table) ToSliceMSS() []map[string]string
- func (tbl *Table) Transpose() (Table, error)
- func (tbl *Table) TrimColumnsRight()
- func (tbl *Table) Unmarshal(funcRow func(row []string) error) error
- func (tbl *Table) UpsertRowColumnValue(rowIdx, colIdx uint, value string)
- func (tbl *Table) WriteCSV(path string) error
- func (tbl *Table) WriteJSON(path string, perm os.FileMode, jsonPrefix, jsonIndent string) error
- func (tbl *Table) WriteMarkdown(filename string, perm os.FileMode, newline string, escPipe bool) error
- func (tbl *Table) WriteXLSX(path, sheetname string) error
- type TableSet
Constants ¶
const ( FormatFloat = "float" FormatInt = "int" FormatString = "string" FormatDate = "date" FormatTime = "time" FormatURL = "url" StyleSimple = "border:1px solid #000;border-collapse:collapse" )
const ( ColNameNone = "(none)" ColNameCount = "(count)" )
Variables ¶
var ( ErrSheetNameCollision = errors.New("sheet name collision") ErrTablesCannotBeEmpty = errors.New("tables cannot be empty") )
var ErrTableCannotBeNil = errors.New("table cannot be nil")
Functions ¶
func SimpleHTMLPage ¶ added in v2.10.0
func SimpleTable ¶
func SimpleTablePreescaped ¶ added in v2.8.11
func StreamSimpleTable ¶
func StreamSimpleTablePreescaped ¶ added in v2.8.11
func WriteCSVSimple ¶
WriteCSVSimple writes a file with cols and rows data.
func WriteSimpleTable ¶
func WriteSimpleTable(qq422016 qtio422016.Writer, table Table)
func WriteSimpleTablePreescaped ¶ added in v2.8.11
func WriteSimpleTablePreescaped(qq422016 qtio422016.Writer, table Table)
func WriteXLSXInterface ¶
Types ¶
type ColumnDefinition ¶ added in v2.10.0
type ColumnDefinitions ¶ added in v2.10.0
type ColumnDefinitions struct { DefaultFormat string Definitions []ColumnDefinition }
ColumnDefinitions provides a way to load definitions with their format types using the `Table.LoadColumnDefinitions()` method.
type Columns ¶
type Columns []string
Columns represents a slice of string with tabular functions.
func (Columns) CellFloat64 ¶
func (cols Columns) CellFloat64(colName string, row []string, defaultIfEmpty bool, def float64) (float64, error)
CellFloat64 returns a single row value.
func (Columns) CellInt ¶
func (cols Columns) CellInt(colName string, row []string, defaultIfEmpty bool, def int) (int, error)
CellInt returns a single row value.
func (Columns) CellString ¶
func (cols Columns) CellString(colName string, row []string, defaultIfEmpty bool, def string) (string, error)
CellString returns a single row value.
func (Columns) CellTime ¶
func (cols Columns) CellTime(colName, timeFormat string, row []string, defaultIfEmpty bool, def time.Time) (time.Time, error)
CellTime returns a single row value. If no `timeFormat` is provided `time.RFC3339` is used.
func (Columns) CellUint ¶ added in v2.15.0
func (cols Columns) CellUint(colName string, row []string, defaultIfEmpty bool, def uint) (uint, error)
CellUint returns a single row value.
func (Columns) CellsString ¶
func (cols Columns) CellsString(colNames []string, row []string, useDefault bool, def string) ([]string, error)
CellsString returns a slice of values.
func (Columns) Equal ¶
Equal returns true if the number of elements or the element values of the Columns do not match.
func (Columns) Index ¶
Index returns the column index of the requested column name. A value of `-1` is returned if the coliumn name is not found.
func (Columns) MustCellString ¶
MustCellString returns a single row value or empty string if the column name doesn't exist.
func (Columns) MustCellsString ¶
MustCellsString returns a slice of values.
type ParseOptions ¶
type ParseOptions struct { UseComma bool Comma rune NoHeader bool // HasHeader is default (false) FieldsPerRecord int FilterColNames []string FilterColIndices []uint TrimSpace bool }
ParseOptions provides a set of configuraation parameters for parsing a CSV file. If an empty or nil `ParseOptions` is provided to `ReadFile`, default options for reading files will be used.
func (*ParseOptions) CommaValue ¶
func (opts *ParseOptions) CommaValue() rune
func (*ParseOptions) HasFilter ¶
func (opts *ParseOptions) HasFilter() bool
type Table ¶
type Table struct { Name string Columns Columns Rows [][]string RowsFloat64 [][]float64 IsFloat64 bool FormatMap map[int]string FormatFunc func(val string, colIdx uint) (any, error) FormatAutoLink bool BackgroundColorFunc func(colIdx, rowIdx uint) string ID string Class string Style string }
Table is useful for working on CSV data. It stores records as `[]string` with typed formatting information per-column to facilitate transformations.
func NewTable ¶
NewTable returns a new empty `Table` struct with slices and maps set to empty (non-nil) values.
func NewTableMapStringInt ¶ added in v2.10.0
func ParseReadSeeker ¶
func ParseReadSeeker(opts *ParseOptions, rs io.ReadSeeker) (Table, error)
ParseReadSeeker parses an `io.ReadSeeker` and returns a `Table` struct.
func ParseTableXLSX ¶ added in v2.18.5
func ParseTableXLSXIndex ¶ added in v2.18.5
func ReadFile ¶
func ReadFile(opts *ParseOptions, filenames ...string) (Table, error)
ReadFile reads one or more delimited files and returns a merged `Table` struct. An error is returned if the columns count differs between files.
func ReadTableSimple ¶ added in v2.19.3
ReadTableSimple is used for CSVs without standard escaping. For example, when `"` is used within a string, but is not used for encapsulating strings.
func ReadTableXLSXFile ¶ added in v2.18.5
func ReadTableXLSXIndexFile ¶ added in v2.18.5
func (*Table) BuildFloat64 ¶ added in v2.17.1
BuildFloat64 populates `RowsFloat64` from `Rows`. It is an experimental feature for machine learning. Most features use `Rows`.
func (*Table) ColumnExpandPivot ¶ added in v2.3.0
func (tbl *Table) ColumnExpandPivot(colIdx uint, split bool, sep, colNamePrefix, colNameNone, existString, notExistString string, addCounts bool, colNameCounts string) (map[int]string, error)
ColumnExpandPivot adds columns to the table representing each value in the provided column.
func (*Table) ColumnValues ¶
func (*Table) ColumnValuesCounts ¶ added in v2.11.0
func (*Table) ColumnValuesCountsByName ¶ added in v2.19.0
func (*Table) ColumnValuesForColumnName ¶
func (*Table) ColumnValuesMinMax ¶
func (*Table) ColumnValuesName ¶ added in v2.14.0
func (*Table) ColumnValuesSplit ¶
func (*Table) ColumnsValuesDistinct ¶
func (*Table) FilterColumnDistinctFirstTable ¶ added in v2.8.11
func (*Table) FilterColumnValuesRows ¶ added in v2.8.11
FilterRecordsColumnValues returns a set of records filtered by column names and column values.
func (*Table) FilterColumnValuesTable ¶ added in v2.8.11
FilterColumnValuesTable returns a Table filtered by column names and column values.
func (*Table) FormatColumn ¶
func (tbl *Table) FormatColumn(colIdx uint, conv func(cellVal string) (string, error), skipRowLengthMismatch bool) error
FormatColumn takes a function to format all cell values.
func (*Table) FormatColumns ¶ added in v2.19.3
func (*Table) FormatRows ¶ added in v2.4.2
func (tbl *Table) FormatRows(colIdxMinInc, colIdxMaxInc int, conv func(cellVal string) (string, error)) error
FormatRows formats row cells using a start and ending column index and a convert function. The `format.ConvertDecommify()` and `format.ConvertRemoveControls()` functions are available to use.
func (*Table) FormatterFunc ¶
FormatterFunc returns a formatter function. A custom format func is returned if it is supplied and `FormatMap` is empty. If FormatMap is not empty, a function for it is returned.`
func (*Table) IsWellFormed ¶
IsWellFormed returns true when the number of columns equals the length of each row. If columns is empty, the length of the first row is used for comparison.
func (*Table) LoadColumnDefinitions ¶ added in v2.10.0
func (tbl *Table) LoadColumnDefinitions(colDefs *ColumnDefinitions)
LoadColumnDefinitions loads a set of column definitions with names and formats. It can be used to add columns to a table without any existing columns or to add to a table with existing columns.
func (*Table) LoadMergedRows ¶
LoadMergedRows is used to load data including both column names and rows from `[][]string` sources like `csv.ReadAll()`.
func (*Table) Pivot ¶
Pivot takes a "straight table" where the columnn names and values are in a single column and lays it out as a standard tabular data.
func (*Table) RowCellCounts ¶
RowCellCounts returns a `map[int]int` where the key is the cell count and the value is the number of rows.
func (*Table) RowsToMap ¶ added in v2.12.0
RowsToMap converts two columns to a map, given a `keyIdx` and `valIdx` for the keys and values.
func (*Table) ToDocuments ¶
func (*Table) ToSliceMSS ¶
func (*Table) Transpose ¶ added in v2.4.2
Transpose creates a new table by transposing the matrix data. In the new table, it does not set anything other than than `Name`, `Columns`, and `Rows`.
func (*Table) TrimColumnsRight ¶ added in v2.4.2
func (tbl *Table) TrimColumnsRight()
func (*Table) Unmarshal ¶
Unmarshal is a convenience function to provide a simple interface to unmarshal table contents into any desired output.
func (*Table) UpsertRowColumnValue ¶
func (*Table) WriteMarkdown ¶ added in v2.8.11
type TableSet ¶
type TableSet struct { Name string Columns []string FormatMap map[int]string FormatFunc func(val string, colIdx uint) (any, error) TableMap map[string]*Table Order []string }
func NewTableSet ¶
func ReadTableSetXLSXFile ¶ added in v2.18.5
ReadTableSetXLSXFile reads in an entire XLSX file as a `TableSet`. Warning: this can be resource intensive if there's a lot of data. If you just want one sheet of many, it is better to extract an individual sheet or sheets from an `excelize.File`, such as using `ParseTableXLSX()` or `ParseTableXLSXIndex()`.