LLM Use with AI

Quick Start

This guide creates a workbook, writes a few formulas, saves it, then reopens it and reads the results back.

Create and Save a Workbook

package main

import (
	"log"

	"github.com/jpoz/werkbook"
)

func must(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	wb := werkbook.New(werkbook.FirstSheet("Sales"))
	sales := wb.Sheet("Sales")

	must(sales.SetValue("A1", "Product"))
	must(sales.SetValue("B1", "Qty"))
	must(sales.SetValue("C1", "Price"))
	must(sales.SetValue("D1", "Total"))

	must(sales.SetValue("A2", "Widget"))
	must(sales.SetValue("B2", 50))
	must(sales.SetValue("C2", 10))
	must(sales.SetFormula("D2", "B2*C2"))

	must(sales.SetValue("A3", "Gadget"))
	must(sales.SetValue("B3", 30))
	must(sales.SetValue("C3", 20))
	must(sales.SetFormula("D3", "B3*C3"))

	// Formulas are written without the leading '='.
	must(sales.SetFormula("D4", "SUM(D2:D3)"))

	if err := wb.SaveAs("sales.xlsx"); err != nil {
		log.Fatal(err)
	}
}

SetValue accepts strings, booleans, numbers, time.Time, nil, and werkbook.Value.

Reopen and Read Values

package main

import (
	"fmt"
	"log"

	"github.com/jpoz/werkbook"
)

func main() {
	wb, err := werkbook.Open("sales.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	sales := wb.Sheet("Sales")
	if sales == nil {
		log.Fatal("missing Sales sheet")
	}

	formula, _ := sales.GetFormula("D4")
	total, _ := sales.GetValue("D4")

	fmt.Println(formula)      // SUM(D2:D3)
	fmt.Println(total.Number) // 1100

	for row := range sales.Rows() {
		for _, cell := range row.Cells() {
			ref, _ := werkbook.CoordinatesToCellName(cell.Col(), row.Num())
			v, _ := sales.GetValue(ref)
			fmt.Printf("%s = %v\n", ref, v.Raw())
		}
	}
}

Use GetValue when you want formula-aware reads. Cell.Value() exposes the cached cell payload directly.

Work in Memory

// Using the wb value from the first example:
var buf bytes.Buffer

if err := wb.WriteTo(&buf); err != nil {
	log.Fatal(err)
}

wb2, err := werkbook.OpenReader(&buf)
if err != nil {
	log.Fatal(err)
}

fmt.Println(wb2.SheetNames())

Use the CLI

# Inspect a workbook
wb info sales.xlsx

# Read data as JSON
wb read --sheet Sales --format json sales.xlsx

# Read as a markdown table
wb read --sheet Sales --headers --format markdown sales.xlsx

# Trace formula dependencies
wb dep --sheet Sales --cell D4 sales.xlsx

# Recalculate formulas and print results
wb calc sales.xlsx

What’s Next