Documentation
¶
Overview ¶
Package gsheets is a wrapper package for `golang.org/x/oauth2` and `google.golang.org/api/sheets/v4`. This package is supported only for personal use.
Example ¶
package main
import (
"context"
"fmt"
"os"
"github.com/delve/gsheets"
)
func main() {
// https://docs.google.com/spreadsheets/d/1-kaYsJxISA2MJMKtTIzWwOFsMm9Wp21U3Nbqnfru65w
spreadsheetID := "1-kaYsJxISA2MJMKtTIzWwOFsMm9Wp21U3Nbqnfru65w"
ctx := gsheets.WithCache(context.Background())
client, err := gsheets.New(ctx,
os.Getenv("GOOGLE_API_CREDENTIALS"), os.Getenv("GOOGLE_API_TOKEN"))
if err != nil {
panic(err)
}
title, err := client.GetTitle(ctx, spreadsheetID)
if err != nil {
panic(err)
}
fmt.Printf("title: %s\n", title)
sheetNames, err := client.GetSheetNames(ctx, spreadsheetID)
if err != nil {
panic(err)
}
for _, sheetName := range sheetNames {
sheet, err := client.GetSheet(ctx, spreadsheetID, sheetName)
if err != nil {
panic(err)
}
fmt.Printf("sheetName: %s, A1: %s\n", sheetName, sheet.Value(0, 0))
}
}
Output: title: test-sheet sheetName: foo, A1: This value is written in A1 of the foo sheet. sheetName: bar, A1: This value is written in A1 of the bar sheet.
Index ¶
- func ClientWritable() func(c *Client) *Client
- func WithCache(ctx context.Context) context.Context
- type Client
- func (c *Client) BatchUpdate(ctx context.Context, spreadsheetID string, updateValues ...UpdateValue) error
- func (c *Client) GetSheet(ctx context.Context, spreadsheetID, sheetName string) (*Sheet, error)
- func (c *Client) GetSheetNames(ctx context.Context, spreadsheetID string) ([]string, error)
- func (c *Client) GetTitle(ctx context.Context, spreadsheetID string) (string, error)
- func (c *Client) Update(ctx context.Context, spreadsheetID, sheetName string, rowNo int, ...) error
- type ClientOption
- type Row
- type Sheet
- type UpdateValue
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientWritable ¶
ClientWritable is an option to change client writable.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a gsheets client.
func NewForCLI ¶
TODO: Convert this to the way New() works with direct google clientoptions NewForCLI returns a gsheets client. This function is intended for CLI tools.
func (*Client) BatchUpdate ¶
func (c *Client) BatchUpdate(ctx context.Context, spreadsheetID string, updateValues ...UpdateValue) error
BatchUpdate updates multiple rows.
Example ¶
package main
import (
"context"
"fmt"
"os"
"github.com/delve/gsheets"
)
func main() {
// https://docs.google.com/spreadsheets/d/1-kaYsJxISA2MJMKtTIzWwOFsMm9Wp21U3Nbqnfru65w
spreadsheetID := "1-kaYsJxISA2MJMKtTIzWwOFsMm9Wp21U3Nbqnfru65w"
ctx := context.Background()
client, err := gsheets.New(ctx,
os.Getenv("GOOGLE_API_CREDENTIALS"), os.Getenv("GOOGLE_API_TOKEN"),
gsheets.ClientWritable())
if err != nil {
panic(err)
}
// Update
data := []gsheets.UpdateValue{
{SheetName: "foo", RowNo: 2, Values: []interface{}{"a", "b", "c"}},
{SheetName: "bar", RowNo: 2, Values: []interface{}{"1", "2", "3"}},
}
err = client.BatchUpdate(ctx, spreadsheetID, data...)
if err != nil {
fmt.Println(err)
return
}
// Check
sheetNames, err := client.GetSheetNames(ctx, spreadsheetID)
if err != nil {
panic(err)
}
for _, sheetName := range sheetNames {
sheet, err := client.GetSheet(ctx, spreadsheetID, sheetName)
if err != nil {
panic(err)
}
fmt.Printf("%s: %s, %s, %s\n", sheetName, sheet.Value(2, 0), sheet.Value(2, 1), sheet.Value(2, 2))
}
// Clear
data = []gsheets.UpdateValue{
{SheetName: "foo", RowNo: 2, Values: []interface{}{"", "", ""}},
{SheetName: "bar", RowNo: 2, Values: []interface{}{"", "", ""}},
}
err = client.BatchUpdate(ctx, spreadsheetID, data...)
if err != nil {
fmt.Println(err)
return
}
}
Output: foo: a, b, c bar: 1, 2, 3
func (*Client) GetSheetNames ¶
GetSheetNames returns sheet name list.
type ClientOption ¶
Type and optionFunc are now only used in NewForCli, which i am not using atm ClientOption is an option function.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is a row of google spreadsheets.
type Sheet ¶
type Sheet struct {
// contains filtered or unexported fields
}
Sheet is a sheet of google spreadsheets.
type UpdateValue ¶
UpdateValue is data structure for BatchUpdate method. `RowNo` is started from zero.