value

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package value holds what a cell computes to and how it is shown: the Value of a cell, its number Format, and the parsing of typed text into numbers, dates and times. It sits below the function library (internal/functions) and the engine (internal/sheet), which both work in these terms; the engine's API names them too, so callers see one package.

Index

Constants

View Source
const MaxDecimals = 15

MaxDecimals caps Increase decimal places.

Variables

View Source
var (
	ErrDiv0  = Value{Kind: Error, Str: "#DIV/0!"}
	ErrValue = Value{Kind: Error, Str: "#VALUE!"}
	ErrName  = Value{Kind: Error, Str: "#NAME?"}
	ErrNA    = Value{Kind: Error, Str: "#N/A"}
	ErrNum   = Value{Kind: Error, Str: "#NUM!"}
	ErrRef   = Value{Kind: Error, Str: "#REF!"} // also circular references
)

Error values, using Google Sheets codes.

View Source
var Now = time.Now

Now is the clock used by TODAY() and NOW(), and for the year of dates typed without one; tests replace it.

Functions

func AsText

func AsText(v Value) string

AsText converts a value for string concatenation.

func Compare

func Compare(l, r Value) int

Compare orders values like Sheets: numbers < text < booleans, text is case-insensitive, and a blank equals 0 or "".

func ParseNumber

func ParseNumber(s string) (float64, bool)

ParseNumber recognizes numbers the way Google Sheets does on entry: optional sign, optional leading currency symbol, thousands separators, decimals, exponent, and a trailing percent sign ("12%" is 0.12).

Types

type Format

type Format struct {
	Kind     FormatKind
	Decimals int // for Number, Percent, Scientific, Accounting, Financial, Currency
	// Pattern is the pattern of a FmtCustom, or overrides the default
	// pattern of a date or time kind (a date typed as 2026-09-26 keeps
	// that style, as in Sheets).
	Pattern string
}

Format is a cell's number format. It is a plain value so cells can be copied freely.

func ParseDateTime

func ParseDateTime(s string) (float64, Format, bool)

ParseDateTime recognizes dates and times typed into a cell the way Sheets does in the en-US locale: 9/26/2026, 9/26/26, 9/26 (this year), 2026-09-26, Sep 26, 2026, 26 Sep 2026, 14:30, 2:30 PM, 2pm, 25:30 (a duration) and a date followed by a time. It returns the serial and the format Sheets would apply.

func ParseValue

func ParseValue(s string) (float64, Format, bool)

ParseValue recognizes everything Sheets turns into a number on entry: numbers, currency, percentages, dates and times. It also returns the format Sheets applies, e.g. Currency for "$1,200" or Date for "9/26/2026"; plain numbers get Automatic.

func Preset

func Preset(k FormatKind) Format

Preset returns kind with Sheets' default decimals: two for the number kinds.

func (Format) Code

func (f Format) Code() string

Code returns the Sheets-style format pattern f renders with, or "" for Automatic and Plain text.

func (Format) IsZero

func (f Format) IsZero() bool

IsZero reports whether f is Automatic.

func (Format) WithDecimals

func (f Format) WithDecimals(delta int, v float64) Format

WithDecimals returns f showing delta more (or fewer) decimal places, as Sheets' Increase and Decrease decimal places do. v is the value shown, used when f is Automatic to start from the decimals currently visible. Formats without decimals (dates, plain text) are returned unchanged.

type FormatKind

type FormatKind uint8

FormatKind is a number format from Sheets' Format > Number menu.

const (
	FmtAuto       FormatKind = iota // General; formulas may infer a format
	FmtText                         // Plain text: entries stay as typed
	FmtNumber                       // 1,000.12
	FmtPercent                      // 10.12%
	FmtScientific                   // 1.01E+03
	FmtAccounting                   // $ (1,000.12)
	FmtFinancial                    // (1,000.12)
	FmtCurrency                     // $1,000.12
	FmtDate                         // 9/26/2026
	FmtTime                         // 3:59:00 PM
	FmtDateTime                     // 9/26/2026 15:59:00
	FmtDuration                     // 24:01:00
	FmtCustom                       // Pattern, e.g. from Increase decimal places on Automatic
)

func ParseFormatKind

func ParseFormatKind(s string) (FormatKind, bool)

ParseFormatKind is the inverse of FormatKind.String.

func (FormatKind) HasDecimals

func (k FormatKind) HasDecimals() bool

HasDecimals reports whether the kind takes a number of decimal places.

func (FormatKind) IsTime

func (k FormatKind) IsTime() bool

IsTime reports whether the kind shows a date, time or duration.

func (FormatKind) String

func (k FormatKind) String() string

String returns the kind's name as stored in files.

type Kind

type Kind int

Kind is the type of a computed cell value.

const (
	Empty Kind = iota
	Number
	Text
	Bool
	Error
	// Array marks an array or LAMBDA while a formula is evaluated
	// (internal/functions keeps them; Num says which). No cell ever holds
	// one: a formula computing an array shows its first value and spills
	// the rest.
	Array
)

type Value

type Value struct {
	Kind Kind
	Num  float64 // numbers, and booleans as 1 or 0
	Str  string  // text, or the error code such as #DIV/0!
}

Value is the computed contents of a cell.

func Boolean

func Boolean(b bool) Value

Boolean is TRUE or FALSE.

func ErrOf

func ErrOf(v Value) *Value

ErrOf returns a pointer to a copy of v, for the *Value error results of argument helpers. Taking &v of a parameter directly would move it to the heap on every call, erroneous or not: one allocation per cell read by SUM over a range.

func Num

func Num(v float64) Value

Num is the number v, or #NUM! for NaN and infinities.

func Str

func Str(s string) Value

Str is the text s.

func ToNum

func ToNum(v Value) (float64, *Value)

ToNum coerces v for arithmetic as Sheets does: blanks are 0, booleans 1 or 0, numeric text (including dates such as "2026-09-26") is its number, other text is #VALUE!.

func (Value) String

func (v Value) String() string

String renders a value the way a cell shows it in General format.

Jump to

Keyboard shortcuts

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