book

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeywordSheet    = "@sheet"
	KeywordType     = "@type"
	KeywordStruct   = "@struct"
	KeywordKey      = types.DefaultDocumentMapKeyOptName   // @key
	KeywordValue    = types.DefaultDocumentMapValueOptName // @value
	KeywordIncell   = "@incell"
	KeywordVariable = "@variable"
	KeywordNote     = "@note"
)
View Source
const BookNameInMetasheet = "#"

BookNameInMetasheet is the special sign which represents workbook itself in metasheet. Default is "#".

View Source
const MacroIgnore = "#IGNORE" // bool: whether to ignore row

Macro command definitions

View Source
const MetaSign = "@"

MetaSign signifies the name starts with leading "@" is meta name.

View Source
const SheetKey = "@sheet"

Variables

This section is empty.

Functions

func ExtractFromCell

func ExtractFromCell(cell string, line int) string

func MetabookOptions added in v0.13.0

func MetabookOptions() *tableaupb.WorkbookOptions

func MetasheetOptions added in v0.9.12

func MetasheetOptions(context context.Context) *tableaupb.WorksheetOptions

Types

type Book

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

func NewBook

func NewBook(ctx context.Context, bookName, filename string, parser SheetParser) *Book

NewBook creates a new book. Example:

  • bookName: Test
  • filename: testdata/Test.xlsx

func (*Book) AddSheet

func (b *Book) AddSheet(sheet *Sheet)

AddSheet adds a sheet to the book and keep the sheet order.

func (*Book) BookName

func (b *Book) BookName() string

BookName returns this book's name.

func (*Book) Clear added in v0.9.8

func (b *Book) Clear()

Clear clears all sheets in the book.

func (*Book) DelSheet

func (b *Book) DelSheet(sheetName string)

DelSheet deletes a sheet from the book.

func (*Book) ExportCSV

func (b *Book) ExportCSV() error

func (*Book) ExportExcel

func (b *Book) ExportExcel() error

func (*Book) Filename

func (b *Book) Filename() string

Filename returns this book's original filename.

func (*Book) Format added in v0.11.0

func (b *Book) Format() format.Format

Format returns this book's format.

func (*Book) GetBookOptions added in v0.13.0

func (b *Book) GetBookOptions() *tableaupb.WorkbookOptions

GetBookOptions creates a new tableaupb.WorkbookOptions based on this special sheet(#)'s info.

func (*Book) GetSheet

func (b *Book) GetSheet(name string) *Sheet

GetSheet returns a Sheet of the specified sheet name.

func (*Book) GetSheets

func (b *Book) GetSheets() []*Sheet

GetSheets returns all sheets in order in the book.

func (*Book) ParseMetaAndPurge added in v0.10.7

func (b *Book) ParseMetaAndPurge() (err error)

ParseMetaAndPurge parses metasheet to Metabook and purge needless sheets which is not in parsed Metabook.

func (*Book) Squeeze

func (b *Book) Squeeze(sheetNames []string)

Squeeze keeps only the inputed sheet names and removes other sheets from the book.

func (*Book) String added in v0.11.0

func (b *Book) String() string

type Cell added in v0.15.0

type Cell struct {
	*Column
	Data string // cell data
	// contains filtered or unexported fields
}

Cell represents a cell in the row of sheet.

func (*Cell) GetName added in v0.15.0

func (c *Cell) GetName() string

func (*Cell) GetType added in v0.15.0

func (c *Cell) GetType() string

type Column added in v0.15.0

type Column struct {
	Col  int    // column index (0-based)
	Name string // column name
	Type string // column type
}

type ColumnLookupTable added in v0.10.7

type ColumnLookupTable = map[string]int

column name -> column index (started with 0)

type Kind added in v0.11.0

type Kind int
const (
	ScalarNode Kind = iota
	ListNode
	MapNode
	DocumentNode
)

func (Kind) String added in v0.11.0

func (k Kind) String() string

type Node added in v0.11.0

type Node struct {
	Kind     Kind
	Name     string
	Value    string
	Children []*Node

	// Line and Column hold the node position in the file.
	NamePos  Position
	ValuePos Position

	// Note is an optional comment attached to this node in the source
	// document (e.g. YAML `#` line comment, XML `@note` attribute),
	// extracted so that protogen can emit it as the generated proto
	// field's comment.
	Note string
}

Node represents an element in the tree document hierarchy.

References:

func (*Node) DebugKV added in v0.11.0

func (n *Node) DebugKV() []any

func (*Node) DebugNameKV added in v0.11.0

func (n *Node) DebugNameKV() []any

func (*Node) DebugValueKV added in v0.11.0

func (n *Node) DebugValueKV() []any

func (*Node) FindChild added in v0.11.0

func (n *Node) FindChild(name string) *Node

FindChild finds the child with specified name.

func (*Node) GetChildrenWithoutMeta added in v0.11.0

func (n *Node) GetChildrenWithoutMeta() (nodes []*Node)

GetChildrenWithoutMeta returns this node's children without meta nodes defined in schema sheet.

func (*Node) GetDataSheetName added in v0.11.0

func (n *Node) GetDataSheetName() string

GetDataSheetName returns original data sheet name by removing leading symbol "@" from meta sheet name.

e.g.: "@SheetName" -> "SheetName"

func (*Node) GetMetaIncell added in v0.11.0

func (n *Node) GetMetaIncell() bool

GetMetaIncell returns this node's @incell defined in schema sheet.

func (*Node) GetMetaKey added in v0.11.0

func (n *Node) GetMetaKey() string

GetMetaKey returns this node's key defined in schema sheet.

func (*Node) GetMetaKeyNode added in v0.16.2

func (n *Node) GetMetaKeyNode() *Node

GetMetaKeyNode returns the @key node of a vertical-map struct, or nil if the struct has no explicit key (e.g. an incell map). The @key node carries the key field's name as its Value, and may carry a Note extracted from the source document that should be attached to the generated key field.

func (*Node) GetMetaSheet added in v0.11.0

func (n *Node) GetMetaSheet() string

GetMetaSheet returns this node's @sheet defined in document node.

func (*Node) GetMetaStructNode added in v0.11.0

func (n *Node) GetMetaStructNode() *Node

GetMetaStructNode returns this node's @struct node defined in schema sheet.

func (*Node) GetMetaType added in v0.11.0

func (n *Node) GetMetaType() string

GetMetaType returns this node's @type defined in schema sheet.

func (*Node) GetMetaTypeNode added in v0.11.0

func (n *Node) GetMetaTypeNode() *Node

GetMetaTypeNode returns this node's @type node defined in schema sheet.

func (*Node) GetMetaVariable added in v0.11.0

func (n *Node) GetMetaVariable() string

GetMetaVariable returns this node's variable name defined in schema sheet.

func (*Node) GetValue added in v0.11.0

func (n *Node) GetValue() string

GetValue returns node's value. It will return empty string if node is nil.

func (*Node) IsMeta added in v0.11.0

func (n *Node) IsMeta() bool

IsMeta checks whether this node is meta (name starts with leading "@") or not.

func (*Node) ScalarValue added in v0.14.0

func (n *Node) ScalarValue() string

ScalarValue returns node's value. XML importer will convert a scalar node to an 1-elem list node and this returns the elem's value as node's value.

func (*Node) String added in v0.11.0

func (n *Node) String() string

String returns hierarchy representation of the Node, mainly for debugging.

func (*Node) StructNode added in v0.14.0

func (n *Node) StructNode() *Node

StructNode returns node's as a struct node. XML importer will convert a struct node to an 1-elem list node and this returns the elem node as struct node.

type Position added in v0.11.0

type Position struct {
	// Line and Column hold the node position in the file.
	Line   int
	Column int
}

type Row added in v0.15.0

type Row struct {
	Row int // row number
	// contains filtered or unexported fields
}

Row represents a row in the sheet.

func NewRow added in v0.15.0

func NewRow(row int, prev *Row, lookupTable ColumnLookupTable) *Row

NewRow creates a new row.

func (*Row) AddCell added in v0.15.0

func (r *Row) AddCell(col *Column, data string, needPopulateKey bool)

AddCell adds a cell to the row.

func (*Row) Cell added in v0.15.0

func (r *Row) Cell(name string, optional bool) (*Cell, error)

Cell returns the cell with the given name.

func (*Row) CellDebugKV added in v0.15.0

func (r *Row) CellDebugKV(name string) []any

CellDebugKV returns a list of key-value pairs for debugging.

func (*Row) Free added in v0.15.0

func (r *Row) Free()

Free frees the row.

func (*Row) GetCellCountWithPrefix added in v0.15.0

func (r *Row) GetCellCountWithPrefix(prefix string) int

GetCellCountWithPrefix returns the cell count with the given prefix.

func (*Row) Ignored added in v0.15.0

func (r *Row) Ignored() (bool, error)

Ignored checkes whether this row is ignored.

type Sheet

type Sheet struct {
	Name string
	// Table represents the data structure of 2D flat table formats.
	// E.g.: Excel, CSV.
	Table *Table
	// Document represents the data structure of tree document formats.
	// E.g.: XML, YAML.
	Document *Node
	// Meta represents sheet's metadata, containing sheet’s layout,
	// parser options, loader options, and so on.
	Meta *internalpb.Metasheet
}

func NewDocumentSheet added in v0.11.0

func NewDocumentSheet(name string, doc *Node) *Sheet

NewDocumentSheet creates a new Sheet with a document.

func NewTableSheet added in v0.11.0

func NewTableSheet(name string, rows [][]string) *Sheet

NewTableSheet creates a new Sheet with a table.

func (*Sheet) GetDataName added in v0.11.0

func (s *Sheet) GetDataName() string

GetDataName returns original data sheet name by removing leading symbol "@" from meta sheet name. For example: "@SheetName" -> "SheetName".

func (*Sheet) GetDebugName added in v0.11.0

func (s *Sheet) GetDebugName() string

GetDebugName returns sheet name with alias if specified.

func (*Sheet) GetProtoName added in v0.11.0

func (s *Sheet) GetProtoName() string

GetDebugName returns this sheet's corresponding protobuf message name.

func (*Sheet) ParseMetasheet added in v0.11.0

func (s *Sheet) ParseMetasheet(parser SheetParser) (*internalpb.Metabook, error)

ParseMetasheet parses a sheet to Metabook by the specified parser.

func (*Sheet) String

func (s *Sheet) String() string

String returns the string representation of the Sheet, mainly for debugging.

  • Table: CSV form
  • Document: hierachy form

func (*Sheet) SubTableSheet added in v0.15.0

func (s *Sheet) SubTableSheet(options ...TableOption) *Sheet

SubTableSheet creates a new sub table sheet with the specified options.

func (*Sheet) Tabler added in v0.15.0

func (s *Sheet) Tabler() Tabler

Tabler returns the table of the Sheet. If the sheet is not a table, returns nil. If the sheet is transposed, returns the transposed table, otherwise returns the original table.

func (*Sheet) ToWorkseet added in v0.11.0

func (s *Sheet) ToWorkseet() *internalpb.Worksheet

ToWorkseet creates a new basic internalpb.Worksheet without fields populated, based on this sheet's info.

type SheetParser

type SheetParser interface {
	Parse(protomsg proto.Message, sheet *Sheet) error
}

type Table added in v0.11.0

type Table struct {
	Rows [][]string // 2D array strings
	// contains filtered or unexported fields
}

Table represents a 2D array table.

func NewTable added in v0.11.0

func NewTable(rows [][]string) *Table

NewTable creates a new Table.

func (*Table) BeginCol added in v0.14.0

func (t *Table) BeginCol() int

BeginCol returns the begin column of this table.

func (*Table) BeginRow added in v0.14.0

func (t *Table) BeginRow() int

BeginRow returns the begin row of this table.

func (*Table) Cell added in v0.11.0

func (t *Table) Cell(row, col int) (string, error)

Cell returns the cell value at (row, col).

func (*Table) ColSize added in v0.14.0

func (t *Table) ColSize() int

ColSize returns the column size of this table.

func (*Table) EndCol added in v0.14.0

func (t *Table) EndCol() int

EndCol returns the end column (column after the last column) of this table.

For loop example:

for col := t.BeginCol(); row < t.EndCol(); col++ {
	fmt.Println(col)
}

func (*Table) EndRow added in v0.14.0

func (t *Table) EndRow() int

EndRow returns the end row (row after the last row) of this table.

For loop example:

for row := t.BeginRow(); row < t.EndRow(); row++ {
	fmt.Println(row)
}

func (*Table) ExportCSV added in v0.11.0

func (t *Table) ExportCSV(writer io.Writer) error

ExportCSV exports Table to writer in CSV format.

func (*Table) ExportExcel added in v0.11.0

func (t *Table) ExportExcel(file *excelize.File, sheetName string) error

ExportExcel exports Table to excel sheet.

func (*Table) FindBlockEndRow added in v0.12.0

func (t *Table) FindBlockEndRow(startRow int) int

FindBlockEndRow finds the end row (row after the last non-empty row) of the block. If the start row is empty, it will just return the start row. Otherwise, it will return past-the-last non-empty row.

NOTE: A block is a series of contiguous non-empty rows. So different blocks are seperated by one or more empty rows.

func (*Table) GetRow added in v0.11.0

func (t *Table) GetRow(row int) []string

GetRow returns the row data by row index (started with 0). It will return nil if not found.

func (*Table) Position added in v0.15.0

func (t *Table) Position(row, col int) string

Position generate the position (row, col) (e.g.: A1) in a table.

NOTE: row and col are both 0-based.

func (*Table) RowSize added in v0.14.0

func (t *Table) RowSize() int

RowSize returns the row size of this table.

func (*Table) String added in v0.11.0

func (t *Table) String() string

String converts Table to CSV string. It is mainly used for debugging.

func (*Table) SubTable added in v0.15.0

func (t *Table) SubTable(options ...TableOption) *Table

SubTable creates a sub table of the table with specified options.

func (*Table) Transpose added in v0.15.0

func (t *Table) Transpose() *TransposedTable

Transpose interchanges the rows and columns of the table.

type TableOption added in v0.14.0

type TableOption func(*TableOptions)

TableOption is the functional option type for table.

func Cols added in v0.14.0

func Cols(begin, end int) TableOption

Cols sets the table-occupied column range of the whole sheet: [begin, end).

func Rows added in v0.14.0

func Rows(begin, end int) TableOption

Rows sets the table-occupied row range of the whole sheet: [begin, end).

type TableOptions added in v0.14.0

type TableOptions struct {
	BeginRow, EndRow int
	BeginCol, EndCol int
}

type Tabler added in v0.15.0

type Tabler interface {
	// BeginRow returns the begin row of this table.
	BeginRow() int
	// EndRow returns the end row (row after the last row) of this table.
	EndRow() int
	// BeginCol returns the begin column of this table.
	BeginCol() int
	// EndCol returns the end column (column after the last column) of this table.
	EndCol() int
	// RowSize returns the row size of this table.
	RowSize() int
	// ColSize returns the column size of this table.
	ColSize() int
	// Cell returns the cell value at (row, col).
	Cell(row, col int) (string, error)
	// GetRow returns the row data by row index (started with 0). It will return
	// nil if not found.
	GetRow(row int) []string
	// Position generate the position (row, col) (e.g.: A1) in a table.
	//
	// NOTE: row and col are both 0-based.
	Position(row, col int) string
	// FindBlockEndRow finds the end row (row after the last non-empty row) of
	// the block. If the start row is empty, it will just return the start row.
	// Otherwise, it will return past-the-last non-empty row.
	//
	// NOTE: A block is a series of contiguous non-empty rows. So different blocks
	// are seperated by one or more empty rows.
	FindBlockEndRow(startRow int) int
	// SubTable creates a sub-table of the table with specified options.
	SubTable(options ...TableOption) *Table
}

type TransposedTable added in v0.15.0

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

func (*TransposedTable) BeginCol added in v0.15.0

func (t *TransposedTable) BeginCol() int

func (*TransposedTable) BeginRow added in v0.15.0

func (t *TransposedTable) BeginRow() int

func (*TransposedTable) Cell added in v0.15.0

func (t *TransposedTable) Cell(row, col int) (string, error)

func (*TransposedTable) ColSize added in v0.15.0

func (t *TransposedTable) ColSize() int

func (*TransposedTable) EndCol added in v0.15.0

func (t *TransposedTable) EndCol() int

func (*TransposedTable) EndRow added in v0.15.0

func (t *TransposedTable) EndRow() int

func (*TransposedTable) FindBlockEndRow added in v0.15.0

func (t *TransposedTable) FindBlockEndRow(startRow int) int

func (*TransposedTable) GetRow added in v0.15.0

func (t *TransposedTable) GetRow(row int) []string

func (*TransposedTable) Position added in v0.15.0

func (t *TransposedTable) Position(row, col int) string

func (*TransposedTable) RowSize added in v0.15.0

func (t *TransposedTable) RowSize() int

func (*TransposedTable) SubTable added in v0.15.0

func (t *TransposedTable) SubTable(options ...TableOption) *Table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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