sheets

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sheets is an in-memory fake of the parts of the Google Sheets v4 and Drive v3 APIs that leadscore uses: spreadsheet create and get, batchUpdate with the requests the Sheets store sends, values get and batchGet, and Drive permissions. It is an http.Handler, so a test serves it with httptest and points a store's `base_url` at it.

It keeps the real API's rules that the store depends on: a batchUpdate is all-or-nothing, tab names are unique, appendCells writes after the last row with data, a tab cannot lose all its non-frozen rows or freeze all its rows, a cell holds at most 50,000 characters, a workbook at most 10 million cells, and a request body at most 10 MB. Reads trim trailing empty rows and cells, a range naming a missing tab is "Unable to parse range", and a range starting below the grid "exceeds grid limits". Tab names match ignoring case.

Formulas are not evaluated: a formula cell reads back as its formula text.

Index

Constants

View Source
const (
	MaxCellChars = 50_000
	MaxCells     = 10_000_000
	MaxBodyBytes = 10 << 20
)

Limits the real API enforces.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the fake. The zero value is not usable; call New.

func New

func New() *Server

New returns an empty fake.

func (*Server) AddPermission

func (s *Server) AddPermission(id, email, role string)

AddPermission shares a spreadsheet with an account in a role, by hand.

func (*Server) BlockSharing

func (s *Server) BlockSharing(id, msg string)

BlockSharing makes sharing the spreadsheet (every spreadsheet, for an empty id) fail with Drive's 403 and msg, as a Workspace sharing policy does. An empty msg allows sharing again.

func (*Server) Calls

func (s *Server) Calls(kind string) int

Calls returns how many calls of a kind were served: "create", "get", "batchUpdate", "values" (get and batchGet) or "permissions".

func (*Server) Cell

func (s *Server) Cell(id, title, a1 string) string

Cell returns the text of one cell given in A1 form ("H1"), a formula as its formula text; "" when empty or missing.

func (*Server) ConditionalFormats

func (s *Server) ConditionalFormats(id, title string) []*sheetsapi.ConditionalFormatRule

ConditionalFormats returns a tab's conditional format rules.

func (*Server) DeleteTab

func (s *Server) DeleteTab(id, title string)

DeleteTab removes a tab.

func (*Server) Deny

func (s *Server) Deny(id string, denied bool)

Deny makes every call on the spreadsheet answer 403, as when it is not shared with the caller.

func (*Server) FailAbout

func (s *Server) FailAbout(fail bool)

FailAbout makes Drive's about.get fail.

func (*Server) IDs

func (s *Server) IDs() []string

IDs returns every spreadsheet id, in creation order.

func (*Server) InsertRow

func (s *Server) InsertRow(id, title string, at int, values []string)

InsertRow inserts a row of text at a zero-based row index, as a person inserting a row (or sorting) would, shifting the rows below down.

func (*Server) IsFormula

func (s *Server) IsFormula(id, title, a1 string) bool

IsFormula reports whether a cell (A1 form) holds a formula rather than text.

func (*Server) NewSpreadsheet

func (s *Server) NewSpreadsheet(title string) string

NewSpreadsheet creates an empty spreadsheet with one tab, "Sheet1", as the real API does, and returns its id.

func (*Server) Note

func (s *Server) Note(id, title, a1 string) string

Note returns a cell's note (A1 form).

func (*Server) OnBatchUpdate

func (s *Server) OnBatchUpdate(f func())

OnBatchUpdate sets f to run before each batchUpdate is applied, outside the fake's lock, so it can change the spreadsheet as a person editing it at that moment would.

func (*Server) OnValuesRead

func (s *Server) OnValuesRead(f func())

OnValuesRead sets f to run before each values get or batchGet, outside the fake's lock.

func (*Server) Permissions

func (s *Server) Permissions(id string) []*drive.Permission

Permissions returns the Drive permissions made on a spreadsheet.

func (*Server) Put

func (s *Server) Put(id, title string, rows [][]any) error

Put writes rows into a tab as a person typing would, creating the tab when missing and growing it to fit. A value is a string, a float64, an int or a bool; a string starting with "=" is a formula.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP routes the Sheets v4 and Drive v3 paths the store uses.

func (*Server) SetCaller

func (s *Server) SetCaller(email string)

SetCaller sets the account Drive's about.get reports as signed in.

func (*Server) SlowDown

func (s *Server) SlowDown(n int)

SlowDown makes the next n calls answer 429 RESOURCE_EXHAUSTED, Google's "slow down".

func (*Server) Spreadsheet

func (s *Server) Spreadsheet(id string) *sheetsapi.Spreadsheet

Spreadsheet returns a copy of a spreadsheet's properties and tabs (with their protected ranges), as spreadsheets.get returns them; nil when missing.

func (*Server) SwapRows

func (s *Server) SwapRows(id, title string, a, b int)

SwapRows swaps two rows (zero-based), as a person sorting a tab would.

func (*Server) ValueReads

func (s *Server) ValueReads() []string

ValueReads returns every range read through values get and batchGet, as "<valueRenderOption> <range>", in order.

func (*Server) WritersCanShare

func (s *Server) WritersCanShare(id string) bool

WritersCanShare reports Drive's writersCanShare setting for a spreadsheet.

Jump to

Keyboard shortcuts

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