xl

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Core XL package

Only contains the most "core" data representations used by other algorithms built atop, such as:

  • Cell
  • Sheet
  • Range
  • RawSheet
  • CVal
  • CType etc.

Don't include algos in there, or algo-specific structs.

Documentation

Index

Constants

View Source
const MAX_COLS = 16_384
View Source
const MAX_ROWS = 1_048_576

Variables

View Source
var CValEmpty = CVal{Type: CTEmpty}

Functions

func IsAddress

func IsAddress(s string) bool

Types

type Address

type Address string

func ParseAddress

func ParseAddress(address string) (Address, error)

type CType

type CType uint8
const (
	CTEmpty CType = iota
	CTString
	CTFormula
	CTNumber
	CTBool
)

func (CType) String

func (c CType) String() string

type CVal

type CVal struct {
	Type CType `json:"-"`

	ValString string `json:"str,omitempty"`
	// We don't need the precision of float64
	ValNumber float32 `json:"num,omitempty"`
	ValBool   bool    `json:"bool,omitempty"`

	// If a formula, this is the formula string
	ValFormula Formula `json:"formula,omitempty"`
	// If the formula underwent computation, this is true
	HasComputed bool `json:"-"`
	// If the formula underwent computation, this is the type of the computed value.
	ComputedType CType `json:"-"`
}

CellVal Represents the value of a cell in a sheet. The json tags are minimal to reduce file size

The computed value can be a string, number, or bool, but not a formula, and so its value can be found in the other fields. E.g. the computed CVal of =SUM(A1:A2) would be Before computation:

{
	Type: CTFormula,
	HasComputed: false,
	ComputedType: CTFormula,
    ValFormula: "=SUM(A1:A2)",
}

After computation:

{
	Type: CTFormula,
	ValNumber: 3,
	HasComputed: true,
	ComputedType: CTNumber,
    ValFormula: "=SUM(A1:A2)",
}

Another possibility is the spill/empty cells. let's say A1:B2 contains [[1,2],[3,4]]; D4 is empty, and C3 contains =A1:B2, then D4 will contain the spill value of B2, which is 4. In this case, we will have the following CVal in D4:

{
	Type: CTEmpty,
	ValNumber: 4,
	HasComputed: true,
	ComputedType: CTNumber,
}

aka, an empty cell has a computed number value of 4! This is because of the formula =A1:B2 spilling into D4.

func (CVal) Computed

func (c CVal) Computed() CVal

func (CVal) String

func (cell CVal) String() string

func (CVal) ToValue

func (cell CVal) ToValue() any

type Cell

type Cell struct {
	Sheet string `json:"sheet"`

	/* Max 2**20 = 1 048 576 */
	Row uint32 `json:"row"`
	/* Max 2**14 == 16 384 */
	Col uint16 `json:"col"`

	RowRel bool `json:"rowRel"`
	ColRel bool `json:"colRel"`
}

func NewCell

func NewCell(row int, col int, sheet string) (Cell, error)

func ParseCell

func ParseCell(s string, defaultSheet string) (Cell, error)

func ToCell

func ToCell(address Address, defaultSheet string) (Cell, error)

func (Cell) IsEq

func (c Cell) IsEq(other Cell) bool

func (Cell) IsInBounds

func (c Cell) IsInBounds(s *Sheet) bool

func (Cell) Shift

func (c Cell) Shift(rowDiff int, colDiff int) (Cell, error)

func (Cell) ShiftIfRel

func (c Cell) ShiftIfRel(rowDiff int, colDiff int) (shifted Cell, err error)

func (Cell) StripDollars

func (c Cell) StripDollars() Cell

func (Cell) ToAddress

func (c Cell) ToAddress() Address

func (Cell) ToAddressNoSheet

func (c Cell) ToAddressNoSheet() Address

func (Cell) ToAddressRel

func (c Cell) ToAddressRel(relativeSheetName string) Address

func (Cell) WithDollars

func (c Cell) WithDollars() Cell

type Formula

type Formula string

func (Formula) RemoveCommaSpaces

func (f Formula) RemoveCommaSpaces() Formula

func (Formula) RemoveDollars

func (f Formula) RemoveDollars() Formula

func (Formula) String

func (f Formula) String() string

type LocalAddress

type LocalAddress string

type Range

type Range struct {
	Start Cell
	End   Cell
}

Make sure that start and end are in the same sheet!

func ParseRange

func ParseRange(s string, sheetName string) (Range, error)

func (Range) Cells

func (r Range) Cells() (cells []Cell)

Returns a list of all cells in the range.

func (Range) Shift

func (r Range) Shift(rowDiff int, colDiff int) (Range, error)

func (Range) ShiftIfRel

func (r Range) ShiftIfRel(rowDiff int, colDiff int) (Range, error)

func (Range) String

func (r Range) String() string

func (Range) StringRel

func (r Range) StringRel(relativeSheetName string) string

type RawSheet

type RawSheet struct {
	Name    string  `json:"name" binding:"required"`
	Content [][]any `json:"content" binding:"required"`
	// The computed values of formulas in the sheet, if known.
	// If not provided, the computed values will be of size 0.
	Computed [][]any `json:"computed"`
}

func (*RawSheet) ToSheet

func (s *RawSheet) ToSheet() (Sheet, error)

type RawWorkbook

type RawWorkbook struct {
	Name   string     `json:"name"`
	Sheets []RawSheet `json:"sheets"`
}

type Relativeness

type Relativeness struct {
	Row bool
	Col bool
}

type Sheet

type Sheet struct {
	Name    string   `json:"name"`
	Content [][]CVal `json:"content"`
}

func (Sheet) Cells

func (s Sheet) Cells() []Cell

func (*Sheet) ContentValues

func (s *Sheet) ContentValues() [][]any

func (*Sheet) Get

func (s *Sheet) Get(c Cell) (CVal, error)

func (*Sheet) GetRange

func (s *Sheet) GetRange(c Range) ([][]CVal, error)

func (Sheet) PrettyPrint

func (s Sheet) PrettyPrint(length ...int)

func (*Sheet) UsedRange

func (s *Sheet) UsedRange() UsedRange

type Shiftable

type Shiftable interface {
	Shift(rowDiff int, colDiff int) (Shiftable, error)
}

type SplitAddress

type SplitAddress struct {
	Sheet        string       // e.g. Sheet1
	LocalAddress LocalAddress // e.g. A1
}

type UsedRange

type UsedRange struct {
	RowCount int
	ColCount int
}

type Workbook

type Workbook struct {
	Name   string  `json:"name"`
	Sheets []Sheet `json:"sheets"`
}

Jump to

Keyboard shortcuts

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