Skip to content

googlesheetsparser is a library for dynamically parsing Google Sheets into Golang structs.

License

Notifications You must be signed in to change notification settings

Tobi696/googlesheetsparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

googlesheetsparser

googlesheetsparser is a library for dynamically parsing Google Sheets into Golang structs.

Installation

go get github.com/Tobi696/googlesheetsparser

Requirements

This library requires Go >= 1.18 as generics are used.

Usage

Example Sheet The Image shows the sheet called "Users" which is contained in the example spreadsheet.

To Parse it, we would utilize following code:

// Define your structs to be parsed
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 `sheets:"Created At"` // <- Custom Column Name, optional, will be prioritized over the Struct Field Name
}


// Acutal usage of the Library
users, err := googlesheetsparser.ParseSheetIntoStructSlice[User](googlesheetsparser.Options{
    Service:       srv, // <- Google Sheets Service (*sheets.Service)
    SpreadsheetID: "15PTbwnLdGJXb4kgLVVBtZ7HbK3QEj-olOxsY7XTzvCc",
    // SheetName: "app-user", <- Optional, by default the library will pluralize the struct name provided as generic, e.g. User -> Users
    DatetimeFormats: []string{ // <- Optional, here you can provide further Datetime Formats
        "2.1.2006",
        "02.01.2006",
        "02.01.2006 15:04:05",
    },
}.Build()) // <- Don't forget to call Build() on the Options struct you provide
if err != nil {
    log.Fatalf("Unable to parse page: %v", err)
}

// Do anything you want with the result
fmt.Println(users)

Authenticating a Google Sheets Service

There are different ways to authenticate a Google Sheets Service.

Please refer to the Google Sheets Go Quickstart for more information.

Example

To try out the example yourself, check out the example/-Directory.

Intention

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.

The initial need that this library was created for is for seeding a database with data from a Google Sheets.
That's very useful because often people who don't have the knowledge to work directly on Databases want to seed their Applications (Mobile/Desktop or whatever) with data. With this library, these people can enter their seed data into a Google sheet and the Developer only has to seed the Database with the structs that the library parsed for him.

Future

In the future, this library will be extended with test cases to ensure that the library is working as expected.

When the library has reached a reasonable test coverage, it will be extended to support more types, flexible sheets and other things users would like to see.

Contributing

Contributions are welcome! Please open an issue or pull request if you have any suggestions or want to contribute.

Support the development by donating.

Donate with PayPal