README
¶
spreadsheet
Functions
func AssertColumnNamed
func AssertColumnNamed(t *testing.T, row Row, name, want string)
AssertColumnNamed assets the column with the given name matches the expected output
func AssertHeadersExist
func AssertHeadersExist(p Parser, expectedHeaders []string) error
AssertHeadersExist ensures the provided headers exist and exits if they don't
func ColByName
func ColByName(r Row, name string) string
ColByName returns the string in the cell at the specified column
func CountRows
func CountRows(input ParserInput) int
CountRows returns the number of rows in a spreadsheet
func CreateIndex
func CreateIndex(i ParserInput, colName string, rowKeyCreator func(string) string) (map[string][]Row, error)
CreateIndex returns a map of rowKey => []Row. rowKey is created by the keyCreator function, which takes a cell value and returns a rowKey
func EachParserRow
func EachParserRow(p Parser, f func(Row)) error
EachParserRow calls func for each of the rows provided by a Parser Automatically closes the parser
func EachRow
func EachRow(input ParserInput, f func(Row)) error
EachRow takes the path of a spreadsheet and executes the func once for each row
func FloatColByName
func FloatColByName(r Row, name string) float32
FloatColByName returns the float32 in the cell at the specified column
Documentation
¶
Index ¶
- Constants
- Variables
- func AssertColumnNamed(t *testing.T, row Row, name, want string)
- func AssertHeadersExist(p Parser, expectedHeaders []string) error
- func ColByName(r Row, name string) string
- func CountRows(input ParserInput) int
- func CreateIndex(i ParserInput, colName string, rowKeyCreator func(string) string) (map[string][]Row, error)
- func EachParserRow(p Parser, f func(Row)) error
- func EachRow(input ParserInput, f func(Row)) error
- func FloatColByName(r Row, name string) float32
- type CsvParser
- type CsvRow
- type ErrMissingHeader
- type ErrUnableToParse
- type ErrUnknownFormat
- type Format
- type Parser
- type ParserInput
- type Row
- type XlsParser
- type XlsRow
- type XlsxParser
- type XlsxRow
Constants ¶
const ( // Auto to auto-detect the format. Based on extension and supports xls, xlsx and falls back to csv Auto = iota // Csv is CSV format Csv Format = iota // Ssv is space spearated Ssv = iota // Xls is Xls excel files, from excel up to 2004 Xls = iota // Xlsx is a modern xlsx excel file Xlsx = iota )
Variables ¶
var ( // ErrEOF is returned when the parser has reached the end of the file ErrEOF = io.EOF // ErrColumnOutOfBounds is returned when the given column index is too low or too high ErrColumnOutOfBounds = errors.New("Given column is out of bounds") )
Functions ¶
func AssertColumnNamed ¶
AssertColumnNamed assets the column with the given name matches the expected output
func AssertHeadersExist ¶
AssertHeadersExist ensures the provided headers exist and exits if they don't
func CountRows ¶
func CountRows(input ParserInput) int
CountRows returns the number of rows in a spreadsheet
func CreateIndex ¶
func CreateIndex(i ParserInput, colName string, rowKeyCreator func(string) string) (map[string][]Row, error)
CreateIndex returns a map of rowKey => []Row. rowKey is created by the keyCreator function, which takes a cell value and returns a rowKey
func EachParserRow ¶
EachParserRow calls func for each of the rows provided by a Parser Automatically closes the parser
func EachRow ¶
func EachRow(input ParserInput, f func(Row)) error
EachRow takes the path of a spreadsheet and executes the func once for each row
func FloatColByName ¶
FloatColByName returns the float32 in the cell at the specified column
Types ¶
type CsvParser ¶
type CsvParser struct {
// contains filtered or unexported fields
}
CsvParser is a Parser implementation that handles CSVs
func NewCsvParser ¶
func NewCsvParser(input ParserInput) (*CsvParser, error)
NewCsvParser creates a CsvParser with the given path, opening the file and preparing it for reading
func (CsvParser) Close ¶
func (p CsvParser) Close()
Close closes the CSV file. No further operations will be possible.
func (*CsvParser) Next ¶
Next returns the next Row from the file, or errors if for example we reached the end
func (*CsvParser) SetHeaderNames ¶
SetHeaderNames sets header names, allowing retrieval of columns by name
func (CsvParser) SetSeparator ¶
SetSeparator changes the delimiter parsed in the provided file. Default is a comma.
type CsvRow ¶
type CsvRow struct {
// contains filtered or unexported fields
}
CsvRow represents a row in a CSV file
type ErrMissingHeader ¶
type ErrMissingHeader struct {
// contains filtered or unexported fields
}
ErrMissingHeader is an error message representing which header in which file was missing
func (ErrMissingHeader) Error ¶
func (e ErrMissingHeader) Error() string
type ErrUnableToParse ¶
type ErrUnableToParse struct {
// contains filtered or unexported fields
}
ErrUnableToParse represents an error opening/parsing the given file
func (ErrUnableToParse) Error ¶
func (e ErrUnableToParse) Error() string
type ErrUnknownFormat ¶
type ErrUnknownFormat struct {
// contains filtered or unexported fields
}
ErrUnknownFormat represents an error detecting the format of a file
func (ErrUnknownFormat) Error ¶
func (e ErrUnknownFormat) Error() string
type Parser ¶
type Parser interface { Next() (Row, error) Close() SetHeaderNames([]string) Headers() []string Path() string }
Parser is an interface for types that can parse a spreadsheet by Row
func NewParser ¶
func NewParser(input ParserInput) (Parser, error)
NewParser creates a parser appropriate for the spreadsheet at the given path. Supports:
- CSV
- xls
- xlsx
type ParserInput ¶
ParserInput represents a spreadsheet and associated options/validations
type XlsParser ¶
type XlsParser struct {
// contains filtered or unexported fields
}
XlsParser is a Parser implementation that handles xls spreadsheets
func NewXlsParser ¶
func NewXlsParser(input ParserInput) (*XlsParser, error)
NewXlsParser creates an XlsParser from a given file path
func (XlsParser) Close ¶
func (p XlsParser) Close()
Close closes the spreadsheet, making it unavailable for further operations
func (*XlsParser) SetHeaderNames ¶
SetHeaderNames sets header names, allowing retrieval of columns by name
type XlsRow ¶
type XlsRow struct {
// contains filtered or unexported fields
}
XlsRow represents a row in an Xls sheet
type XlsxParser ¶
type XlsxParser struct {
// contains filtered or unexported fields
}
XlsxParser is a spreadsheet.Parser implementation for Xlsx files
func NewXlsxParser ¶
func NewXlsxParser(input ParserInput) (*XlsxParser, error)
NewXlsxParser returns an XlsxParser for the file at the given path
func (XlsxParser) Close ¶
func (p XlsxParser) Close()
Close is unimplemented and unnecessary for xlsx files
func (XlsxParser) Headers ¶
func (p XlsxParser) Headers() []string
Headers returns the headers found or set on the current parsed file
func (XlsxParser) Path ¶
func (p XlsxParser) Path() string
Path returns the path used for the file being parsed
func (*XlsxParser) SetHeaderNames ¶
func (p *XlsxParser) SetHeaderNames(names []string)
SetHeaderNames sets header names, allowing retrieval of columns by name