Quick Start

Create a Spreadsheet

package main

import (
	"log"

	"github.com/jpoz/werkbook"
)

func main() {
	// Create a new workbook with a sheet named "Sales"
	wb := werkbook.New(werkbook.FirstSheet("Sales"))
	sheet := wb.Sheet("Sales")

	// Add headers
	sheet.SetValue("A1", "Product")
	sheet.SetValue("B1", "Qty")
	sheet.SetValue("C1", "Price")
	sheet.SetValue("D1", "Total")

	// Add data
	sheet.SetValue("A2", "Widget")
	sheet.SetValue("B2", 50)
	sheet.SetValue("C2", 9.99)
	sheet.SetFormula("D2", "B2*C2")

	sheet.SetValue("A3", "Gadget")
	sheet.SetValue("B3", 30)
	sheet.SetValue("C3", 24.99)
	sheet.SetFormula("D3", "B3*C3")

	// Add a summary formula
	sheet.SetFormula("D5", "SUM(D2:D3)")

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

Read a Spreadsheet

package main

import (
	"fmt"
	"log"

	"github.com/jpoz/werkbook"
)

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

	sheet := wb.Sheet("Sales")

	// Read a single value
	v, _ := sheet.GetValue("A1")
	fmt.Println(v.Raw()) // "Product"

	// Iterate over all rows
	for row := range sheet.Rows() {
		for _, cell := range row.Cells() {
			name, _ := werkbook.CoordinatesToCellName(cell.Col(), row.Num())
			fmt.Printf("%s = %v\n", name, cell.Value().Raw())
		}
	}
}

Use the CLI

# Inspect a workbook
wb info sales.xlsx

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

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

# Recalculate formulas and print results
wb calc sales.xlsx

What’s Next