sheetsmap

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 7 Imported by: 3

README

SheetsMap

SheetsMap treats a Google Sheet as a key-value store with typed columns and enum validation.

Overview

SheetsMap provides:

  • Key-value storage - First column is the key, second is display name, rest are data columns
  • Column schema - Define columns with names, aliases, abbreviations, and enum values
  • Enum validation - Validate and canonicalize values against allowed enums
  • CRUD operations - Get, create, update items with automatic synchronization

Sheet Structure

The first row defines the schema. Each cell follows this format:

column_name|alias1|alias2 - abbreviation - enum1|alt1, enum2|alt2 - info_text|info_url

Example header row:

Column 1 Column 2 Column 3
email name tshirt size|t-shirt size - Size - XS, S, M, L, XL

Usage

Initialize
import (
    "github.com/grokify/gogoogle/sheetsutil/v4/sheetsmap"
    "google.golang.org/api/sheets/v4"
)

// Using sheet index
sm, err := sheetsmap.NewSheetsMapIndex(httpClient, spreadsheetID, 0)

// Using sheet title
sm, err := sheetsmap.NewSheetsMapTitle(httpClient, spreadsheetID, "Sheet1")
Read Data
// Read columns and items
err := sm.FullRead()

// Access items by key
item, err := sm.GetItem("user@example.com")
fmt.Println(item.Display)      // Display name
fmt.Println(item.Data["size"]) // Column value
Write Data
// Get or create an item
item, err := sm.GetOrCreateItem("user@example.com")

// Update a field with enum validation
_, err = sm.UpdateItem(item, "tshirt size", "M", true)

// Set item display name
err = sm.SetItemKeyDisplay("user@example.com", "John Doe")
Column Schema
// Parse a column definition
col, err := sheetsmap.ParseColumn("size|t-shirt size - S - XS, S, M, L, XL")

// col.Name = "size"
// col.NameAliases = ["t-shirt size"]
// col.Abbreviation = "S"
// col.Enums = [{Canonical: "XS"}, {Canonical: "S"}, ...]

// Validate and canonicalize a value
canonical, err := col.ValueToCanonical("extra small") // Returns "XS"

Column Definition Format

name|alias1|alias2 - abbreviation - enum1|alt1|alt2, enum2|alt1 - label|url ~ label2|url2
Part Description Example
Name Primary column name tshirt size
Aliases Alternative names (pipe-separated) t-shirt size|shirt size
Abbreviation Short form Size
Enums Allowed values with aliases XS|extra small, S|small
Info URLs Reference links Size Chart|http://example.com

Authentication

Use any *http.Client with Google OAuth credentials:

import "github.com/grokify/goauth/google"

client, err := google.NewClientFromJWTJSON(
    ctx,
    []byte(serviceAccountJSON),
    sheets.DriveScope,
    sheets.SpreadsheetsScope,
)

sm, err := sheetsmap.NewSheetsMapIndex(client, spreadsheetID, 0)

Documentation

Index

Constants

View Source
const (
	ErrorColumnNotFound = "ErrorColumnNotFound"
	ErrorEnumNotMatched = "ErrorEnumNotMatched"
)

Variables

This section is empty.

Functions

func TrimSpaceToLower

func TrimSpaceToLower(s string) string

Types

type Column

type Column struct {
	Name               string
	NameAliases        []string
	Abbreviation       string
	Index              int
	Enums              []Enum
	AliasLcToCanonical map[string]string
	InfoURLs           []InfoURL
}

func NewColumn

func NewColumn() Column

func ParseColumn

func ParseColumn(input string) (Column, error)

ParseColumn tshirt size - XS, S, M, L, XL, XXL, XXXL colName | colAbbr | Enums | URLs

func (*Column) AddEnum

func (col *Column) AddEnum(enum Enum)

func (*Column) EnumsCanonical

func (col *Column) EnumsCanonical() []string

func (*Column) EnumsStrings

func (col *Column) EnumsStrings() []string

func (*Column) ValueToCanonical

func (col *Column) ValueToCanonical(val string) (string, error)

type Enum

type Enum struct {
	Canonical string
	Aliases   []string
}

func (*Enum) Values

func (enum *Enum) Values() []string

type InfoURL

type InfoURL struct {
	Text string
	URL  string
}

type Intent

type Intent struct {
	Name  string
	Slots map[string]string
}

type Item

type Item struct {
	Key     string
	Display string
	Row     int
	Data    map[string]string
}

func (*Item) ItemDisplayOrKey

func (item *Item) ItemDisplayOrKey() string

type SheetsMap

type SheetsMap struct {
	GoogleClient *http.Client
	Service      *spreadsheet.Service

	Spreadsheet spreadsheet.Spreadsheet
	Sheet       *spreadsheet.Sheet

	KeyColumnIndex uint
	Columns        []Column
	ColumnMapKeyLc map[string]Column
	ItemMap        map[string]Item
	// contains filtered or unexported fields
}

func NewSheetsMap

func NewSheetsMap() SheetsMap

func NewSheetsMapIndex

func NewSheetsMapIndex(googleClient *http.Client, spreadsheetID string, sheetIndex uint) (SheetsMap, error)

func NewSheetsMapTitle

func NewSheetsMapTitle(googleClient *http.Client, spreadsheetID string, sheetTitle string) (SheetsMap, error)

func (*SheetsMap) ColumnsKeys

func (sm *SheetsMap) ColumnsKeys() []string

func (*SheetsMap) CombinedStatsCol0Enum

func (sm *SheetsMap) CombinedStatsCol0Enum() ([]Stat, error)

func (*SheetsMap) DataColumnsKeys

func (sm *SheetsMap) DataColumnsKeys() []string

func (*SheetsMap) EmptyCols

func (sm *SheetsMap) EmptyCols(item Item) []string

func (*SheetsMap) FullRead

func (sm *SheetsMap) FullRead() error

func (*SheetsMap) GetItem

func (sm *SheetsMap) GetItem(key string) (Item, error)

func (*SheetsMap) GetItemProperty

func (sm *SheetsMap) GetItemProperty(key string, val string) (string, error)

func (*SheetsMap) GetOrCreateItem

func (sm *SheetsMap) GetOrCreateItem(itemKey string) (Item, error)

func (*SheetsMap) GetOrCreateItemWithName

func (sm *SheetsMap) GetOrCreateItemWithName(itemKey, itemName string) (Item, error)

func (*SheetsMap) IsItemComplete

func (sm *SheetsMap) IsItemComplete(item *Item) bool

func (*SheetsMap) IsItemPartial

func (sm *SheetsMap) IsItemPartial(item *Item) bool

func (*SheetsMap) ReadColumns

func (sm *SheetsMap) ReadColumns() error

func (*SheetsMap) ReadItems

func (sm *SheetsMap) ReadItems() error

func (*SheetsMap) SetItemKeyColValue

func (sm *SheetsMap) SetItemKeyColValue(itemKey, colKeyRaw, colValRaw string) (Item, error)

func (*SheetsMap) SetItemKeyDisplay

func (sm *SheetsMap) SetItemKeyDisplay(itemKey, itemDisplay string) error

func (*SheetsMap) SetItemKeyString

func (sm *SheetsMap) SetItemKeyString(itemKey, cmdRaw string) (Intent, error)

func (*SheetsMap) SheetTitle

func (sm *SheetsMap) SheetTitle() string

func (*SheetsMap) SynchronizeItem

func (sm *SheetsMap) SynchronizeItem(item Item) error

func (*SheetsMap) UpdateItem

func (sm *SheetsMap) UpdateItem(item Item, key, val string, synchronize bool) (string, error)

type Stat

type Stat struct {
	Name  string
	Names []string
	Count int64
}

Jump to

Keyboard shortcuts

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