Skip to content

Controlling Google Sheets using Go - Create, Edit and Manage Spreadsheets

Notifications You must be signed in to change notification settings

marcusbello/google-sheets-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Create, Edit and Manage Google Sheets using Go (Golang)

Tools

  • Install Go
  • Google sheets package. google.golang.org/api/sheets/v4
  • Google cloud console

Configure Google Cloud Credentials

  1. Login to Google cloud console
  2. Navigate to APIs & Services
  3. Add Google Sheets API
  4. Goto Credentials, Add New
  5. Create service account, add Key
  6. Download the JSON file.
  7. Give service account Owner Role.

Connect Go app to Google

  • Setup

  1. Create project directory. mkdir google-sheets-go
  2. Change into the directory cd google-sheets-go
  3. Init the Go application go mod init github.com/username/google-sheets-go
  4. Install Go Google Sheets package. go get google.golang.org/api/sheets/v4
  5. Add a main.go file. touch main.go
  6. Open the main.go file with your favorite IDE, I'm using vscode. code . -r
  • Authenticate using Go

  1. Load the credential (config.json) in to our code;
    package main
    
    import (
        "google.golang.org/api/option"
        "google.golang.org/api/sheets/v4"
    )
    
    func main() {
    
        // Set up Google Sheets API client
        options := []option.ClientOption{
            option.WithCredentialsFile("./config/config.json"),
            option.WithScopes(sheet.SpreadsheetsScope),
        }
    }
  2. Make a Google Sheets Service;
    ctx := context.Background()
    // Create a new Sheets service
    srv, err := sheets.NewService(ctx, options...)
    if err != nil {
        log.Fatalf("Unable to create Sheets service: %v", err)
    }
  • Create a New Google Sheet Spreadsheet

  1. Create a new Spreadsheet;
    sheet, err := srv.Spreadsheets.Create(&sheets.Spreadsheet{
    	Properties: &sheets.SpreadsheetProperties{
    		Title: "Financial Data",
    	},
    }).Do()
    if err != nil {
    	log.Fatalf("Unable to create spreadsheet: %v", err)
    }
    
    log.Printf("Created spreadsheet with ID: %s", sheet.SpreadsheetId)
  • Edit an existing Google Sheets Spreadsheet

  1. Copy the service account email and share the Spreadsheet with the service account email using Google Sheet UI.
  2. Copy the Spreadsheet ID from the sheets URL.
  3. Define the readRange. e.g "Sheet1!A1:D1" ... Read Google sheets API docs
  4. Update or Append data into the sheet.
    // Access a specific spreadsheet
    spreadsheetId := "spreadsheet-ID"
    readRange := "Sheet1!A1:D1" // represents first row
    // Append data to the spreadsheet
    _, err = srv.Spreadsheets.Values.Append(spreadsheetId, readRange, &sheets.ValueRange{
    	Values: [][]interface{}{
    		{"A", "B", "C", "D"},
    	},
    }).ValueInputOption("RAW").Do()
    if err != nil {
    	log.Fatalf("Unable to append data to sheet: %v", err)
    }

Conclusion

This is a basic and simple example showcasing how to manage Google sheets spreadsheets using Go, you can do a lot more with the google sheets API. Read the full documentation at https://developers.google.com/workspace/sheets/api/guides.

About

Controlling Google Sheets using Go - Create, Edit and Manage Spreadsheets

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages