spreadsheet

package
v0.0.0-...-ba18bbf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2020 License: MIT Imports: 12 Imported by: 0

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

View Source
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

View Source
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

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

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) Headers

func (p CsvParser) Headers() []string

Headers returns the headers found or set on the current parsed file

func (*CsvParser) Next

func (p *CsvParser) Next() (Row, error)

Next returns the next Row from the file, or errors if for example we reached the end

func (CsvParser) Path

func (p CsvParser) Path() string

Path returns the path used for the file being parsed

func (*CsvParser) SetHeaderNames

func (p *CsvParser) SetHeaderNames(names []string)

SetHeaderNames sets header names, allowing retrieval of columns by name

func (CsvParser) SetSeparator

func (p CsvParser) SetSeparator(r rune)

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

func (CsvRow) Col

func (r CsvRow) Col(index int) string

Col returns the string at the specified index from the CsvRow

func (CsvRow) Headers

func (r CsvRow) Headers() []string

Headers returns the headers from the CSV

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 Format

type Format int

Format represents the format of the spreadsheete, e.g. xls, csv, etc

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

type ParserInput struct {
	Path            string
	HasHeaders      bool
	RequiredHeaders []string
	Format          Format
}

ParserInput represents a spreadsheet and associated options/validations

type Row

type Row interface {
	Headers() []string
	Col(int) string
}

Row refers to a row in a spreadsheet, which has many columns

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) Headers

func (p XlsParser) Headers() []string

Headers returns the headers found or set on the current parsed file

func (*XlsParser) Next

func (p *XlsParser) Next() (Row, error)

Next returns the next Row from the sheet

func (XlsParser) Path

func (p XlsParser) Path() string

Path returns the path used for the file being parsed

func (*XlsParser) SetHeaderNames

func (p *XlsParser) SetHeaderNames(names []string)

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

func (XlsRow) Col

func (r XlsRow) Col(index int) string

Col returns the string in the specified column

func (XlsRow) Headers

func (r XlsRow) Headers() []string

Headers returns the headers from the XLS

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) Next

func (p *XlsxParser) Next() (Row, error)

Next returns the next Row

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

type XlsxRow

type XlsxRow struct {
	// contains filtered or unexported fields
}

XlsxRow is a spreadsheet.Row implementation for Xlsx files

func (XlsxRow) Col

func (r XlsxRow) Col(index int) string

Col returns the string in the specified column

func (XlsxRow) Headers

func (r XlsxRow) Headers() []string

Headers returns the headers from the XLSX

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL