Documentation
¶
Overview ¶
Package sheetsutil provides utilities for working with the Google Sheets API v4.
This package includes:
- URL parsing for Google Sheets URLs (extracting spreadsheet IDs, sheet GIDs, ranges)
- Cell value types for representing typed spreadsheet data
- Grid parsing utilities for converting API responses to Go-friendly formats
URL Parsing ¶
Parse Google Sheets URLs to extract spreadsheet IDs and other metadata:
id, err := sheetsutil.ParseSpreadsheetURL("https://docs.google.com/spreadsheets/d/abc123/edit#gid=0")
// id = "abc123"
info, err := sheetsutil.ParseSpreadsheetURLFull("https://docs.google.com/spreadsheets/d/abc123/edit#gid=456&range=A1:D10")
// info.SpreadsheetID = "abc123"
// info.SheetGID = 456
// info.Range = "A1:D10"
Build URLs from IDs:
url := sheetsutil.BuildSpreadsheetURL("abc123")
// url = "https://docs.google.com/spreadsheets/d/abc123/edit"
url := sheetsutil.BuildSpreadsheetURLWithSheet("abc123", 456)
// url = "https://docs.google.com/spreadsheets/d/abc123/edit#gid=456"
Cell Value Types ¶
CellValue provides a JSON-friendly representation of spreadsheet cell data:
cv := sheetsutil.ParseCellValue(123.45, "$123.45") // cv.Type = CellTypeNumber // cv.NumberValue = 123.45 // cv.FormattedValue = "$123.45"
TypedCellValue extends CellValue with Go-native types:
opts := sheetsutil.ValueParseOptions{PreferInt64: true}
tcv := sheetsutil.ParseTypedCellValue(42.0, "42", opts)
// tcv.Int64 = 42
Grid Parsing ¶
Convert Sheets API responses to typed grids:
vr := &sheets.ValueRange{...}
grid := sheetsutil.ParseValueRange(vr)
// grid is [][]CellValue
Or extract just the formatted values:
values := sheetsutil.ExtractFormattedValues(vr) // values is [][]string
Index ¶
- Variables
- func BuildSpreadsheetURL(spreadsheetID string) string
- func BuildSpreadsheetURLWithSheet(spreadsheetID string, sheetGID int64) string
- func ExtractFormattedValues(vr *sheets.ValueRange) [][]string
- func ExtractRawValues(vr *sheets.ValueRange) [][]string
- func GridToStringSlice(grid SimpleGrid) [][]string
- func IsSpreadsheetURL(s string) bool
- func NormalizeSpreadsheetInput(urlOrID string) (string, error)
- func ParseSpreadsheetURL(urlOrID string) (string, error)
- type CellType
- type CellValue
- type SimpleGrid
- type SpreadsheetURLInfo
- type TypedCellValue
- type TypedGrid
- type ValueParseOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidURL is returned when a URL cannot be parsed. ErrInvalidURL = errors.New("invalid Google Sheets URL") // ErrEmptyInput is returned when input is empty. ErrEmptyInput = errors.New("input cannot be empty") )
Functions ¶
func BuildSpreadsheetURL ¶ added in v0.11.0
BuildSpreadsheetURL constructs a Google Sheets URL from a spreadsheet ID.
func BuildSpreadsheetURLWithSheet ¶ added in v0.11.0
BuildSpreadsheetURLWithSheet constructs a Google Sheets URL with a sheet GID.
func ExtractFormattedValues ¶ added in v0.11.0
func ExtractFormattedValues(vr *sheets.ValueRange) [][]string
ExtractFormattedValues extracts just the formatted value strings from a ValueRange. This is useful when you want a simple [][]string representation.
func ExtractRawValues ¶ added in v0.11.0
func ExtractRawValues(vr *sheets.ValueRange) [][]string
ExtractRawValues extracts raw values as strings from a ValueRange. Numbers are converted without formatting.
func GridToStringSlice ¶ added in v0.11.0
func GridToStringSlice(grid SimpleGrid) [][]string
GridToStringSlice converts a SimpleGrid to [][]string using formatted values.
func IsSpreadsheetURL ¶ added in v0.11.0
IsSpreadsheetURL checks if the given string is a Google Sheets spreadsheet URL.
func NormalizeSpreadsheetInput ¶ added in v0.11.0
NormalizeSpreadsheetInput accepts either a spreadsheet ID or URL and returns the spreadsheet ID. This is useful for APIs that accept either format.
func ParseSpreadsheetURL ¶ added in v0.11.0
ParseSpreadsheetURL extracts the spreadsheet ID from a Google Sheets URL. If the input is already a plain ID (no slashes or protocol), it returns it as-is. Returns the spreadsheet ID or an error if the URL is invalid.
Types ¶
type CellType ¶ added in v0.11.0
type CellType string
CellType represents the type of data in a cell.
const ( CellTypeEmpty CellType = "empty" CellTypeString CellType = "string" CellTypeNumber CellType = "number" CellTypeBool CellType = "boolean" CellTypeDate CellType = "date" CellTypeTime CellType = "time" CellTypeDateTime CellType = "datetime" CellTypeDuration CellType = "duration" CellTypeError CellType = "error" )
type CellValue ¶ added in v0.11.0
type CellValue struct {
Type CellType `json:"type"`
FormattedValue string `json:"formatted_value"`
StringValue *string `json:"string_value,omitempty"`
NumberValue *float64 `json:"number_value,omitempty"`
BoolValue *bool `json:"bool_value,omitempty"`
ErrorValue *string `json:"error_value,omitempty"`
}
CellValue represents a simple, JSON-friendly cell value.
func ParseCellValue ¶ added in v0.11.0
ParseCellValue converts a raw API value to a CellValue. The value parameter is the raw value from the Sheets API (typically any type). The formattedValue is the user-visible formatted string.
type SimpleGrid ¶ added in v0.11.0
type SimpleGrid = [][]CellValue
SimpleGrid is a 2D array of CellValue for JSON serialization.
func ParseValueRange ¶ added in v0.11.0
func ParseValueRange(vr *sheets.ValueRange) SimpleGrid
ParseValueRange converts a Sheets API ValueRange to a SimpleGrid.
type SpreadsheetURLInfo ¶ added in v0.11.0
type SpreadsheetURLInfo struct {
SpreadsheetID string `json:"spreadsheet_id"`
SheetGID *int64 `json:"sheet_gid,omitempty"`
Range string `json:"range,omitempty"`
}
SpreadsheetURLInfo contains parsed information from a Google Sheets URL.
func ParseSpreadsheetURLFull ¶ added in v0.11.0
func ParseSpreadsheetURLFull(urlOrID string) (SpreadsheetURLInfo, error)
ParseSpreadsheetURLFull extracts all available info from a Google Sheets URL. This includes the spreadsheet ID, optional sheet GID, and optional range.
type TypedCellValue ¶ added in v0.11.0
type TypedCellValue struct {
CellValue
Time *time.Time `json:"-"`
Duration *time.Duration `json:"-"`
Int64 *int64 `json:"-"`
}
TypedCellValue extends CellValue with Go-native types for dates and durations.
func ParseTypedCellValue ¶ added in v0.11.0
func ParseTypedCellValue(value any, formattedValue string, opts ValueParseOptions) TypedCellValue
ParseTypedCellValue converts a raw API value to a TypedCellValue with richer type information.
type TypedGrid ¶ added in v0.11.0
type TypedGrid = [][]TypedCellValue
TypedGrid is a 2D array of TypedCellValue for Go-native processing.
func ParseTypedValueRange ¶ added in v0.11.0
func ParseTypedValueRange(vr *sheets.ValueRange, opts ValueParseOptions) TypedGrid
ParseTypedValueRange converts a Sheets API ValueRange to a TypedGrid.
type ValueParseOptions ¶ added in v0.11.0
ValueParseOptions configures how values are parsed.
func DefaultValueParseOptions ¶ added in v0.11.0
func DefaultValueParseOptions() ValueParseOptions
DefaultValueParseOptions returns default parsing options.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
iwark_oauth
command
Go example that covers: Quickstart: https://developers.google.com/slides/quickstart/go Basic writing: adding a text box to slide: https://developers.google.com/slides/samples/writing Using SDK: https://github.com/google/google-api-go-client/blob/master/slides/v1/slides-gen.go Creating and Managing Presentations https://developers.google.com/slides/how-tos/presentations Adding Shapes and Text to a Slide: https://developers.google.com/slides/how-tos/add-shape#example
|
Go example that covers: Quickstart: https://developers.google.com/slides/quickstart/go Basic writing: adding a text box to slide: https://developers.google.com/slides/samples/writing Using SDK: https://github.com/google/google-api-go-client/blob/master/slides/v1/slides-gen.go Creating and Managing Presentations https://developers.google.com/slides/how-tos/presentations Adding Shapes and Text to a Slide: https://developers.google.com/slides/how-tos/add-shape#example |
|
map
command
|
|