https://github.com/jo-hoe/google-sheets
idiomatic way to read and write data from google sheets
https://github.com/jo-hoe/google-sheets
golang google-sheets google-spreadsheet idiomatic reader-writer sheets
Last synced: 5 months ago
JSON representation
idiomatic way to read and write data from google sheets
- Host: GitHub
- URL: https://github.com/jo-hoe/google-sheets
- Owner: jo-hoe
- License: apache-2.0
- Created: 2021-10-23T07:41:42.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-05T23:34:10.000Z (about 1 year ago)
- Last Synced: 2025-05-06T00:33:21.301Z (about 1 year ago)
- Topics: golang, google-sheets, google-spreadsheet, idiomatic, reader-writer, sheets
- Language: Go
- Homepage:
- Size: 224 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Google Sheets
[](https://pkg.go.dev/github.com/jo-hoe/google-sheets)
[](https://github.com/jo-hoe/google-sheets/actions?workflow=test)
[](https://coveralls.io/github/jo-hoe/google-sheets?branch=main)
[](https://github.com/jo-hoe/google-sheets/actions?workflow=lint)
[](https://github.com/jo-hoe/google-sheets/actions?workflow=CodeQL)
[](https://goreportcard.com/report/github.com/jo-hoe/google-sheets)
Provides an idiomatic way to read and write data from google sheets.
## Example Useage
```golang
// open the key file for your GCP service account (see below how to create that key file)
jsonServiceAccount, err := ioutil.ReadFile("path\\to\\service_account_key.json")
if err != nil {
log.Print(err.Error())
return
}
// spreadsheet id can be taken from the URL
// example URL: https://docs.google.com/spreadsheets/d/c8ACvfAd4X09Hi9mCl4qcBidP635S8z5luk-vGG54N5T/edit#gid=0
// the spreadsheet ID would be "c8ACvfAd4X09Hi9mCl4qcBidP635S8z5lukxvGG54N5T"
sheet, err := gs.OpenSheet(context.Background(), "c8ACvfAd4X09Hi9mCl4qcBidP635S8z5luk-vGG54N5T", "Sheet1", gs.O_CREATE|gs.O_RDWR, jsonServiceAccount)
if err != nil {
log.Print(err.Error())
return
}
csvWriter := csv.NewWriter(sheet)
err = csvWriter.WriteAll([][]string{
{"0", "1"},
{"2", "3"},
})
csvReader := csv.NewReader(sheet)
csvResult, err := csvReader.ReadAll()
if err != nil {
log.Print(err.Error())
return
}
fmt.Printf("results: %v", csvResult)
gs.Remove(context.Background(), gs.SpreadSheetId(), gs.Id(), jsonServiceAccount)
```
### Incomplete Lines
Your google sheet may include non complete lines.
|Title A|Title B|
|-------|-------|
|0|1|
|2| |
In this case you should deactivate the field length validation.
```golang
csvReader := csv.NewReader(sheet)
csvReader.FieldsPerRecord = -1
csvResult, err := csvReader.ReadAll()
if err != nil {
log.Print(err.Error())
return
}
fmt.Printf("results: %v", csvResult)
```
The output will be as follows
```bash
result: [["Title A" "Title B"][0 1] [2]]
```
## Google Sheets AuthN/AuthZ
### General
The offical documentation can be found here: .
### Creating the key file
1. [create a gcp service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating)
2. after creating the service account, ensure that google project in which the service account resides, is enabled to use the sheet api. You verifiy or enable the API using this url scheme [my gcp project id]
3. after the service account is created, take the mail address of that account and [share your spreadsheet with that mail address](https://support.google.com/a/users/answer/9305987?hl=en#)
4. [create a json key for your GCP service account](https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating)
## Linting
Project used `golangci-lint` for linting.
### Installation
### Execution
Run the linting locally by executing
```cli
golangci-lint run ./...
```
in the working directory
## Testing
The project contains both unit and integrations tests.
### Unit Test Execution
The unit test can be excuted using the default golang commands. To run all test execute the following in the parent folder of the repository.
```powershell
go test ./...
```
### Integration Test Execution
A credentials file and a google spreadsheet needed as prerequisite for the integration tests. You may use the following launch.json file in VSCode to run the tests.
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "API Wrapper Integration Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/internal/apiwrapper/apiwrapper_integration_test.go",
"env": {
"CREDENTIALS_FILE_PATH": "C:\\Folder\\file-name-352919-3f8fa23b9bba.json",
"SPREADSHEET_ID": "1yxmv2lTtOtvpkBi-5hSMq86CHFMfYq6kdjfasudfasih"
},
},{
"name": "Sheets Integration Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/gs/gs_integraton_test.go",
"env": {
"CREDENTIALS_FILE_PATH": "C:\\Folder\\file-name-352919-3f8fa23b9bba.json",
"SPREADSHEET_ID": "1yxmv2lTtOtvpkBi-5hSMq86CHFMfYq6kdjfasudfasih"
},
},
]
}
```