Google Sheets Parser is a library for dynamically parsing Google Sheets into Golang structs.
go get github.com/esome/google-sheets-parser
This library requires Go >= 1.23 as range over function mechanics are used.
The Image shows the sheet called "Users" which is contained in the example spreadsheet.
To Parse it, we would utilize following code:
package main
import (
"fmt"
"log"
"time"
"github.com/esome/google-sheets-parser"
"google.golang.org/api/sheets/v4"
)
// User is a struct that represents a row in the Users Sheet
type User struct {
ID uint // <- By default, columns will be parsed into the equally named struct fields.
Username string
Name string
Password *string
Weight *uint
CreatedAt *time.Time `gsheets:"Created At"` // <- Custom Column Name, optional, will be prioritized over the Struct Field Name
}
func main() {
var svc *sheets.Service // <- You need to create a Google Sheets Service first, see below
users, err := gsheets.ParseSheetIntoStructSlice[User](
// minimal Config only containing the Google Sheets service (*sheets.Service)
// Have a look in the example to learn how to create a reusable configuration
gsheets.Config{Service: svc},
// Mandatory! you must define the SpreadsheetID, or an error will be returned
gsheets.WithSpreadsheetID("15PTbwnLdGJXb4kgLVVBtZ7HbK3QEj-olOxsY7XTzvCc"),
// Optional: you can pass an arbitrary amount of ConfigOptions for further customization for this call
gsheets.WithDatetimeFormats( // <- in this case we provide further Datetime Formats to be recognized
"2.1.2006",
"02.01.2006",
"02.01.2006 15:04:05",
),
)
if err != nil {
log.Fatalf("Unable to parse page: %v", err)
}
// Do anything you want with the result
fmt.Println(users)
}
There are different ways to authenticate a Google Sheets Service.
Please refer to the Google Sheets Go Quickstart for more information.
To try out the example yourself, check out the example/-Directory.
In there you will find an example that demonstrates how to create a common config for multiple parse calls.
This library is intended to be used as a library for parsing Google Sheets into Golang structs. It is not intended to be used as a library for generating Google Sheets from Golang structs.
At esome we use Google Sheets in some cases to communicate data with external partners. The data of these sheets sometimes needs to be imported into our data-warehouse. This library helps us to parse the data from the Google Sheets into Golang structs, which then can be written to the databases.
This library was originally a fork of the awesome work from Tobias Wimmer. All credits go to him for the initial implementation. Please consider checking out his repository as well, and support him.
Since the way the library is configured has changed significantly, we decided to create a complete separate/independent repository for it. This also allows us to maintain the library in a way that fits our needs better, and accept/make contributions that may have been rejected in the original project.
Contributions are welcome! Please open an issue or pull request if you have any suggestions or want to contribute.