Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang google sheets API V4 - Write/Update example?

Trying to write a simple three column table ([][]string) with Go, but can't. The quick start guide is very nice, I now can read sheets, but there no any example of how to write data to a sheet, maybe it is trivial, but not for me it seems. The Golang library for my brains is just too complicated to figure out. And there not a single example I could google...

This C# example very looks close, but I am not sure I clearly understand C#

like image 421
MikeKlemin Avatar asked Sep 25 '16 19:09

MikeKlemin


2 Answers

Well after some tryouts, there is an answer. Everything is same as in https://developers.google.com/sheets/quickstart/go Just changes in the main function

func write() {
    ctx := context.Background()
    b, err := ioutil.ReadFile("./Google_Sheets_API_Quickstart/client_secret.json")
    if err != nil {
       log.Fatalf("Unable to read client secret file: %v", err)
    }

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-go-quickstart.json
    config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets")
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

    srv, err := sheets.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve Sheets Client %v", err)
    }

    spreadsheetId := "YOUR SPREADSHEET ID"

    writeRange := "A1"

    var vr sheets.ValueRange

    myval := []interface{}{"One", "Two", "Three"}
    vr.Values = append(vr.Values, myval)

    _, err = srv.Spreadsheets.Values.Update(spreadsheetId, writeRange, &vr).ValueInputOption("RAW").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve data from sheet. %v", err)
    }

}
like image 151
MikeKlemin Avatar answered Sep 23 '22 06:09

MikeKlemin


Well if you are looking for Service Account based authentication, then the following worked for me.

Download the client secret file for service account from https://console.developers.google.com

import (
        "fmt"
        "golang.org/x/net/context"
        "google.golang.org/api/option"
        "google.golang.org/api/sheets/v4"
        "log"
    )


const (
    client_secret_path = "./credentials/client_secret.json"
)


func NewSpreadsheetService() (*SpreadsheetService, error) {
    // Service account based oauth2 two legged integration
    ctx := context.Background()
    srv, err := sheets.NewService(ctx, option.WithCredentialsFile(client_secret_path), option.WithScopes(sheets.SpreadsheetsScope))

    if err != nil {
        log.Fatalf("Unable to retrieve Sheets Client %v", err)
    }

    c := &SpreadsheetService{
        service: srv,
    }

    return c, nil
}


func (s *SpreadsheetService) WriteToSpreadsheet(object *SpreadsheetPushRequest) error {

    var vr sheets.ValueRange
    vr.Values = append(vr.Values, object.Values)

    res, err := s.service.Spreadsheets.Values.Append(object.SpreadsheetId, object.Range, &vr).ValueInputOption("RAW").Do()

    fmt.Println("spreadsheet push ", res)

    if err != nil {
        fmt.Println("Unable to update data to sheet  ", err)
    }

    return err
}   

type SpreadsheetPushRequest struct {
    SpreadsheetId string        `json:"spreadsheet_id"`
    Range         string        `json:"range"`
    Values        []interface{} `json:"values"`
}
like image 21
Sahil Garg Avatar answered Sep 19 '22 06:09

Sahil Garg