book

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 22 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"
	KeywordKeyname  = "@keyname"
)
View Source
const BookNameInMetasheet = "#"

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

View Source
const MetaSign = "@"

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

View Source
const SheetKey = "@sheet"

Variables

View Source
var MetasheetName = "@TABLEAU"

MetasheetName is the name of metasheet which defines the metadata of each worksheet. Default is "@TABLEAU".

Functions

func ExtractFromCell

func ExtractFromCell(cell string, line int32) string

func MetasheetOptions added in v0.9.12

func MetasheetOptions() *tableaupb.WorksheetOptions

func SetMetasheetName added in v0.10.6

func SetMetasheetName(name string)

SetMetasheetName change the metasheet name to the specified name.

NOTE: If will not change MetasheetName value if the specified name is empty.

Types

type Book

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

func NewBook

func NewBook(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) 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) Metabook added in v0.10.6

func (b *Book) Metabook() *internalpb.Metabook

Metabook returns the metadata of this 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 ColumnLookupTable added in v0.10.7

type ColumnLookupTable = map[string]uint32

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
}

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) GetMetaKeyname added in v0.11.0

func (n *Node) GetMetaKeyname() string

GetMetaKeyname returns this node's @keyname defined in schema sheet.

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 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) String added in v0.11.0

func (n *Node) String() string

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

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 RowCell

type RowCell struct {
	Col  int     // cell column index (0-based)
	Name *string // cell name
	Type *string // cell type
	Data string  // cell data
	// contains filtered or unexported fields
}

func (*RowCell) GetName added in v0.10.7

func (r *RowCell) GetName() string

func (*RowCell) GetType added in v0.10.7

func (r *RowCell) GetType() string

type RowCells

type RowCells struct {
	SheetName string

	Row int // row number
	// contains filtered or unexported fields
}

func NewRowCells

func NewRowCells(row int, prev *RowCells, sheetName string) *RowCells

func (*RowCells) Cell

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

func (*RowCells) CellDebugKV added in v0.10.6

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

func (*RowCells) Free added in v0.10.7

func (rc *RowCells) Free()

func (*RowCells) GetCellCountWithPrefix

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

func (*RowCells) NewCell added in v0.10.7

func (r *RowCells) NewCell(col int, name, typ *string, data string, needPopulateKey bool)

func (*RowCells) SetColumnLookupTable added in v0.10.7

func (r *RowCells) SetColumnLookupTable(table ColumnLookupTable)

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) 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 {
	MaxRow int
	MaxCol int
	Rows   [][]string // 2D array strings
}

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) Cell added in v0.11.0

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

Cell returns the cell at (row, col).

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) ExtractBlock added in v0.12.0

func (t *Table) ExtractBlock(startRow int) (rows [][]string, endRow int)

ExtractBlock extracts a block of rows.

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

func (*Table) FindBlockEndRow added in v0.12.0

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

FindBlockEndRow finds the end row of the block. If the start row is empty, it will just return the start row. Otherwise, it will return the last none-empty row.

NOTE: A block is a series of contiguous none-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) IsRowEmpty added in v0.12.0

func (t *Table) IsRowEmpty(row int) bool

IsRowEmpty checks whether the whole row is empty.

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.

Jump to

Keyboard shortcuts

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