Documentation
¶
Overview ¶
Package fileio moves data between sheets and other file formats: CSV and TSV, Excel workbooks, SQLite databases, Parquet files and Lotus 1-2-3 worksheets. Everything is pure Go. The sheet package knows none of these formats: importers build a sheet through its public API (Load, LoadColWidth, RecalcAll) and exporters read a Snapshot of one. Each format is a row of the table in formats.go and a file of its own.
Index ¶
- Variables
- func AppendTable(ctx context.Context, s *sheet.Sheet, last, cols int, data []byte) (rows, allCols int, err error)
- func CellValue(c SnapCell) nuon.Value
- func CodeKind(code string) sheet.FormatKind
- func ColumnFormats(snap *Snapshot) map[string]sheet.Format
- func HTMLChart(c SnapChart) string
- func HTMLDocument(title, body, extra string) string
- func HTMLGrid(snap *Snapshot) (string, string)
- func HTMLPanel(title, name, where string) string
- func HTMLStyle() string
- func KeepFormats(rows *TailRows, formats map[string]sheet.Format)
- func NUONCell(v nuon.Value) sheet.LiveCell
- func NumInput(v float64) string
- func OutputFormats(w *sheet.Workbook, o *notebook.Output, resolve RangeResolver) map[string]sheet.Format
- func SnapColumns(snap *Snapshot) []string
- func TableName(s string) string
- func TextInput(s string) string
- func WriteHTML(w io.Writer, p HTMLPage) (note string, err error)
- type Dialect
- type ErrNeedTable
- type ExportOptions
- type ExportResult
- type HTMLPage
- type Kind
- func (k Kind) About() string
- func (k Kind) CanExport() bool
- func (k Kind) CanImport() bool
- func (k Kind) Ext() string
- func (k Kind) Grows() bool
- func (k Kind) HasTables() bool
- func (k Kind) HoldsSheets() bool
- func (k Kind) IsText() bool
- func (k Kind) Label() string
- func (k Kind) MenuTitle() string
- func (k Kind) Noun() string
- func (k Kind) String() string
- type LineFormat
- type NumberSort
- type Options
- type Progress
- type RangeResolver
- type Result
- type SnapCell
- type SnapChart
- type SnapName
- type Snapshot
- type SortedNumbers
- type Source
- type SourceColumn
- type SourceFilter
- type SourceOrder
- type SourceSort
- type SourceSpec
- type SourceView
- type TableInfo
- type Tail
- type TailRows
Constants ¶
This section is empty.
Variables ¶
var ErrNotSource = errors.New("only Parquet files and SQLite tables or queries can be linked as sources")
ErrNotSource is what OpenSource says of a file that is neither Parquet nor SQLite.
var ErrUnsupported = errors.New("not a format 012 can import")
ErrUnsupported is returned for a file whose extension isn't known.
Functions ¶
func AppendTable ¶ added in v0.5.0
func AppendTable(ctx context.Context, s *sheet.Sheet, last, cols int, data []byte) (rows, allCols int, err error)
AppendTable reads data, a NUON table, into s as more rows of the table importing a table put there: under its row last, each value in the column its header row (row 0, cols columns) names, a column named for the first time added after the others with its name in the header, the cells as importing stores them. It leaves the widths alone and recalculates nothing, the cells being values, and returns the rows and the columns the table has now.
func CellValue ¶ added in v0.3.0
CellValue is a cell as a NUON value, typed by its format as a table written as NUON or JSON types it: what 012 get writes for one cell.
func CodeKind ¶ added in v0.6.0
func CodeKind(code string) sheet.FormatKind
CodeKind says whether a number format code shows a date, a time, both or a duration; FmtCustom for anything else.
func ColumnFormats ¶ added in v0.6.0
ColumnFormats are the formats of snap's columns, by the columns' names: the format of each column's first number under the header, when it isn't Automatic.
func HTMLDocument ¶ added in v0.4.0
HTMLDocument is a whole page: title, style sheet, body and script, with extra added to the script (the MCP view's messages).
func HTMLGrid ¶ added in v0.4.0
HTMLGrid draws the snapshot as 012's grid: column letters and row numbers, the cells as they show with their styles, borders and the looks of rules, and the charts over them. It returns the fragment and a note when rows were left out.
func HTMLPanel ¶ added in v0.4.0
HTMLPanel is the control panel's three lines: the title with the mode indicator, the name box (holding name until a cell is pointed at) and formula bar, and the context line saying what the page shows.
func HTMLStyle ¶ added in v0.4.0
func HTMLStyle() string
HTMLStyle is the style sheet of 012's HTML pages, for a page built around HTMLGrid or HTMLChart; the root element takes the class dark or light to choose a palette over the reader's preference.
func KeepFormats ¶ added in v0.6.0
KeepFormats gives each number under a header named in formats that format, when NUON writes both as the same type: a plain number takes its column's currency, a date its column's pattern, a size its column's decimals.
func NUONCell ¶ added in v0.3.0
NUONCell is the cell a NUON value makes: its value, text as the value's own, and its format (a file size's Size, a date's Date time, in the local time zone), as importing it would store it.
func OutputFormats ¶ added in v0.6.0
func OutputFormats(w *sheet.Workbook, o *notebook.Output, resolve RangeResolver) map[string]sheet.Format
OutputFormats are the formats KeepFormats keeps for output o of w: those of the columns of the ranges its run read, found by resolve, and of its selection.
func SnapColumns ¶ added in v0.6.0
SnapColumns are the names of snap's columns, from its range's first row, as a table written as NUON or JSON names them.
func TableName ¶
TableName makes a table name from a file or sheet name: letters, digits and underscores, not starting with a digit.
Types ¶
type ErrNeedTable ¶
type ErrNeedTable struct{ Tables []TableInfo }
ErrNeedTable is returned for a SQLite database with several tables when Options names neither a table nor a query; Tables lists them.
func (*ErrNeedTable) Error ¶
func (e *ErrNeedTable) Error() string
type ExportOptions ¶
type ExportOptions struct {
Table string // SQLite: the table to write; replaced if it exists
}
ExportOptions tune an export.
type ExportResult ¶
ExportResult says what an export wrote and what it couldn't keep.
func Encode ¶ added in v0.3.0
Encode writes a snapshot to w in the text format k (see Kind.IsText): what 012 --pipe writes to standard output.
func Export ¶
func Export(ctx context.Context, name string, k Kind, snap *Snapshot, opt ExportOptions) (*ExportResult, error)
Export writes a snapshot of a sheet to name in format k.
type HTMLPage ¶ added in v0.4.0
type HTMLPage struct {
Title string // the page's title, and the first line's
Snap *Snapshot // the cells, when Chart is nil
Chart *SnapChart
}
HTMLPage is what an HTML page shows: a sheet or range, or one chart.
type Kind ¶
type Kind int
Kind is an external file format.
func ExportKindOf ¶ added in v0.4.0
ExportKindOf recognizes a file's format by its extension, including formats 012 only writes (HTML).
func SourceKind ¶ added in v0.6.0
func SourceKind(spec SourceSpec) (Kind, error)
SourceKind is the format spec reads, Parquet or SQLite, by its Format or else the file's extension.
func (Kind) About ¶
About says what downloading in this format writes; empty for formats that can't be exported.
func (Kind) Grows ¶ added in v0.3.0
Grows reports whether a file of kind k can be followed as it grows, its new rows read without reading it again: text tables, which Tail reads.
func (Kind) HasTables ¶
HasTables reports whether a file of this kind is a database of tables: importing picks one (see Tables), and a download writes the sheet or the selection as a named table (ExportOptions.Table).
func (Kind) HoldsSheets ¶
HoldsSheets reports whether a file of this kind holds several named sheets, so a download writes the whole workbook (see SnapBook).
func (Kind) IsText ¶ added in v0.3.0
IsText reports whether this kind is text Encode writes to a stream: CSV, TSV, JSON or NUON.
func (Kind) MenuTitle ¶
MenuTitle is the format's entry in the Download menu, e.g. "Microsoft Excel (.xlsx)"; empty for formats that can't be exported.
type LineFormat ¶
LineFormat is the format and style of a whole column or row.
type NumberSort ¶ added in v0.8.0
type NumberSort struct {
// contains filtered or unexported fields
}
NumberSort sorts numbers in bounded memory, the external merge sort of a view's rows (extsort.go) over 16-byte records: each number and its position in the order it was read. The order statistics of a source's column (MEDIAN, PERCENTILE, MODE) read them back in ascending order, ties in that order, holding at most a run of them, sortRunBytes, while the rest wait on disk.
func NewNumberSort ¶ added in v0.8.0
func NewNumberSort(dir string) *NumberSort
NewNumberSort is a sort spilling its runs to files in dir, "" for the system's temporary directory.
func (*NumberSort) Add ¶ added in v0.8.0
func (n *NumberSort) Add(v float64, pos int64) error
Add takes a number read at position pos.
func (*NumberSort) Close ¶ added in v0.8.0
func (n *NumberSort) Close()
Close removes what the sort spilled, when Sorted wasn't asked for.
func (*NumberSort) Len ¶ added in v0.8.0
func (n *NumberSort) Len() int
Len counts the numbers added.
func (*NumberSort) Sorted ¶ added in v0.8.0
func (n *NumberSort) Sorted() (*SortedNumbers, error)
Sorted reads the numbers back in ascending order, once. Closing it removes the runs.
type Options ¶
type Options struct {
// Table is the SQLite table to import, or Query a SELECT to run.
// With neither, a database with one table imports it.
Table, Query string
Progress *Progress
// MaxCells is the most cells to keep, whole rows at a time; 0 is the
// max-cells setting (sheet.MaxCells). WK1 files keep their own limits.
MaxCells int
// Locale is what CSV and TSV fields are read in (see numberLocale);
// nil is en-US.
Locale *locale.Locale
}
Options tune an import.
type Progress ¶
type Progress struct {
// contains filtered or unexported fields
}
Progress reports how far an import has got. The importer updates it from its goroutine; the UI reads it on a timer.
func NewProgress ¶
func NewProgress() *Progress
NewProgress returns a progress with an unknown total.
type RangeResolver ¶ added in v0.6.0
RangeResolver finds the sheet and range a $sheet reference names.
type Result ¶
type Result struct {
Sheet *sheet.Sheet
Kind Kind
Rows int // rows of the file that were read, including a header
Notes []string
}
Result is an imported sheet and what the import had to leave out or change, in sentences for the context line.
func Import ¶
Import reads the file name into a new sheet. It checks ctx between rows, so a long import can be cancelled.
func ImportReader ¶ added in v0.3.0
ImportReader reads a table from r into a new sheet named name (as a file's sheet is named after the file), telling its format from the text; Result.Kind is NUON, JSON, CSV or TSV. It checks ctx between rows and updates opt.Progress.
type SnapCell ¶
type SnapCell struct {
Input string
Value sheet.Value
Format sheet.Format // as displayed, including one inferred from a formula
Own sheet.Format // the cell's own format
Style sheet.Style
Formula bool
Sheets []string // the sheets a formula names, as written
Tables []string // the tables a formula reads, as written
}
SnapCell is one non-blank cell of a snapshot.
type SnapChart ¶ added in v0.4.0
SnapChart is a chart with the values it draws, read when the snapshot is taken.
type Snapshot ¶
type Snapshot struct {
// Range is what's exported. Whole-sheet exports start at A1, as
// Sheets' downloads do, so cells keep their addresses.
Range sheet.Rect
Cells map[sheet.Addr]SnapCell
Widths map[int]int // non-default column widths
Name string // what to call the data: a sheet or table name
// Locale is the sheet's, which text formats (CSV) are written in.
Locale *locale.Locale
// ColFormats and RowFormats are the formats of whole columns and
// rows, for formats that keep them (XLSX).
ColFormats, RowFormats map[int]LineFormat
// Sheets are every sheet of the workbook, in order, for formats that
// hold several (XLSX); the snapshot itself is one of them, the one
// shown. Nil exports just this snapshot. Names are the workbook's
// named ranges.
Sheets []*Snapshot
Names []SnapName
Hidden bool // a hidden sheet of Sheets, written hidden
// FrozenRows and FrozenCols are the frozen panes; Filter is the
// sheet's filter, if any, and HiddenRows the rows of Range it hides,
// for formats that keep them (XLSX).
FrozenRows, FrozenCols int
Filter *sheet.Filter
HiddenRows map[int]bool
// Spills are the cells each formula whose array spills covers, by
// the formula's cell, for formats that keep formulas (XLSX).
Spills map[sheet.Addr]sheet.Rect
// CondFormats and Validations are the sheet's rules, for formats
// that keep them (XLSX).
CondFormats []sheet.CondFormat
Validations []sheet.Validation
// Notes are the cells' notes in the range, for formats that keep
// them (XLSX, as comments). A note may be on a cell with no contents.
Notes map[sheet.Addr]string
// Heights are the rows' heights set by hand, in lines, and Merges
// the merged cells in the range, for formats that keep them (XLSX).
Heights map[int]int
Merges []sheet.Rect
// Tables are the tables wholly in the range, for formats that keep
// them (XLSX).
Tables []sheet.Table
// Looks are how the sheet's rules draw the range's cells, and Charts
// the charts whose top-left cell is in it, for formats that show
// what the screen shows (HTML); see snapdrawn.go.
Looks map[sheet.Addr]sheet.Look
Charts []SnapChart
}
Snapshot is a copy of the part of a sheet being exported, taken on the UI goroutine so the file can be written in the background while the sheet keeps changing.
type SortedNumbers ¶ added in v0.8.0
type SortedNumbers struct {
// contains filtered or unexported fields
}
SortedNumbers is a NumberSort's numbers in ascending order.
func (*SortedNumbers) Close ¶ added in v0.8.0
func (s *SortedNumbers) Close() error
Close removes the runs.
func (*SortedNumbers) Err ¶ added in v0.8.0
func (s *SortedNumbers) Err() error
Err is the error reading the runs met, if any.
func (*SortedNumbers) Len ¶ added in v0.8.0
func (s *SortedNumbers) Len() int
Len counts the numbers.
type Source ¶ added in v0.6.0
type Source interface {
// Columns are the source's columns, left to right.
Columns() []SourceColumn
// Rows counts its rows, the header not among them.
Rows() int64
// Scan streams the rows from row from on (counting from 0) in the
// source's order, calling fn with each row's number and the values
// of cols (every column when cols is nil, in that order; the slice
// is reused) until fn returns false, the rows end or ctx is done.
Scan(ctx context.Context, from int64, cols []int, fn func(row int64, vals []sheet.LiveCell) bool) error
// Fetch reads the rows numbered rows, in the order given, with the
// values of cols (every column when nil).
Fetch(ctx context.Context, rows []int64, cols []int) ([][]sheet.LiveCell, error)
// View orders the rows as o says. Building it may read the whole
// source once, which the caller does in the background.
View(ctx context.Context, o SourceOrder) (SourceView, error)
// Close lets the file go, and what its views built.
Close() error
}
Source is a table read in place. It is safe for use by several goroutines at once.
func OpenSource ¶ added in v0.6.0
func OpenSource(ctx context.Context, spec SourceSpec) (Source, error)
OpenSource opens what spec names.
type SourceColumn ¶ added in v0.6.0
type SourceColumn struct {
Name string
// Format is what its values show in when the column's type says (a
// Parquet DATE column's FmtDate); a value's own may differ.
Format sheet.Format
// Numeric is set when its type holds numbers, dates and times
// included.
Numeric bool
}
SourceColumn is one of a source's columns.
type SourceFilter ¶ added in v0.6.0
SourceFilter is a condition a view's rows meet on a column.
type SourceOrder ¶ added in v0.6.0
type SourceOrder struct {
Sort []SourceSort
Filter []SourceFilter
}
SourceOrder is how a view sorts and filters a source's rows.
func (SourceOrder) IsZero ¶ added in v0.6.0
func (o SourceOrder) IsZero() bool
IsZero reports whether the order is the source's own, every row.
type SourceSort ¶ added in v0.6.0
SourceSort is a column a view is sorted by.
type SourceSpec ¶ added in v0.6.0
type SourceSpec struct {
// Path is the file.
Path string
// Format is "Parquet" or "SQLite", or "" to tell by the extension.
Format string
// Table is a SQLite table or view to read, or Query a query to run;
// both empty reads a database's only table.
Table, Query string
// TempDir is where views keep what they build (sorted row orders,
// SQLite's temporary tables); "" is the system's.
TempDir string
}
SourceSpec names what a source reads.
type SourceView ¶ added in v0.6.0
type SourceView interface {
// Rows counts the rows the view shows.
Rows() int64
// Page reads n rows from position from of the view: each one's
// number in the source and its values.
Page(ctx context.Context, from int64, n int) ([]int64, [][]sheet.LiveCell, error)
// Close lets go of what the view built.
Close() error
}
SourceView is a source's rows as a sort and a filter order them.
type Tail ¶ added in v0.3.0
type Tail struct {
// contains filtered or unexported fields
}
A Tail reads a table of kind k from text that arrives in pieces.
type TailRows ¶ added in v0.3.0
type TailRows struct {
// Header is the table's first row when it's new or changed: a CSV
// file's first record, or NUON's column names as they're seen.
Header sheet.LiveRow
Rows []sheet.LiveRow
}
TailRows are the rows a piece of text completed.
func NUONRows ¶ added in v0.3.0
NUONRows reads NUON, a notebook cell's output, as a region's rows: a table's header and rows, a record as one row, a list of values that aren't records as one column named value, and any other value as that column with one row. It keeps at most maxCells cells, whole rows (0 for the max-cells setting), and says what it left out.
Source Files
¶
- builder.go
- csv.go
- csvlocale.go
- extsort.go
- fileio.go
- formats.go
- formula.go
- html.go
- htmlcell.go
- htmlgrid.go
- htmlstyle.go
- kept.go
- numfmt.go
- numsort.go
- nuon.go
- nuonrows.go
- nuonwrite.go
- parquet.go
- snapdrawn.go
- snapshot.go
- source.go
- sourcecell.go
- sourceparquet.go
- sourcepqview.go
- sourcesort.go
- sourcesqlite.go
- sourcesqlview.go
- sourcesqlwork.go
- sqlite.go
- stream.go
- tail.go
- wk1.go
- wk1formula.go
- xlsx.go
- xlsxarray.go
- xlsxbook.go
- xlsximport.go
- xlsxlayout.go
- xlsxlines.go
- xlsxnotes.go
- xlsxpkg.go
- xlsxrules.go
- xlsxrulesimport.go
- xlsxrulesmore.go
- xlsxrulesread.go
- xlsxsheet.go
- xlsxshift.go
- xlsxstyles.go
- xlsxtables.go
- xlsxview.go
- xlsxviewread.go
- xlsxwrite.go