Skip to content

takuoki/gsheets

Repository files navigation

gsheets

CircleCI codecov GoDoc MIT License

A golang wrapper package for golang.org/x/oauth2 and google.golang.org/api/sheets/v4. You can easily manipulate spreadsheets.

!!! Only for personal use !!!

Installation

go get github.com/takuoki/gsheets

Requirement

This package uses Google OAuth2.0. So before executing tool, you have to prepare credentials.json. See Go Quickstart, or Blog (Japanese) for the details.

Usage

Create Cache

If you want to use the cache, initialize the context. If you are updating sheets, you should not use Cache.

ctx := gsheets.WithCache(ctx)

Create New Client

client, err := gsheets.New(ctx, `{"credentials": "json"}`, `{"token": "json"}`)
client, err := gsheets.NewForCLI(ctx, "credentials.json")

If you are updating sheets, create a client with ClientWritable option.

client, err := gsheets.New(ctx, `{"credentials": "json"}`, `{"token": "json"}`, gsheets.ClientWritable())

Get Sheet Information

func (*Client) GetTitle(ctx context.Context, spreadsheetID string) (string, error)
func (*Client) GetSheetNames(ctx context.Context, spreadsheetID string) ([]string, error)
func (*Client) GetSheet(ctx context.Context, spreadsheetID, sheetName string) (Sheet, error)

Update Sheet Values

func (c *Client) Update(ctx context.Context, spreadsheetID, sheetName string, rowNo int, values []interface{}) error
func (c *Client) BatchUpdate(ctx context.Context, spreadsheetID string, updateValues ...UpdateValue) error

Manipulate Sheet Values

If the index is out of range, Value method returns empty string.

s, err := client.GetSheet(ctx, "spreadsheetID", "sheetName")
if err != nil {
  return err
}

fmt.Println(s.Value(row, clm))

for _, r := range s.Rows() {
  fmt.Println(r.Value(clm))
}

About

A golang wrapper package for "golang.org/x/oauth2" and "google.golang.org/api/sheets/v4"

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages