Skip to content

Commit

Permalink
service/google: support reflect conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 7, 2023
1 parent 9bec294 commit e41d720
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
66 changes: 66 additions & 0 deletions pkg/service/google/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package google

import (
"fmt"
"reflect"
"time"

"github.com/sirupsen/logrus"
"google.golang.org/api/sheets/v4"

"github.com/c9s/bbgo/pkg/fixedpoint"
)

func ReadSheetValuesRange(srv *sheets.Service, spreadsheetId, readRange string) (*sheets.ValueRange, error) {
Expand Down Expand Up @@ -43,6 +47,12 @@ func ValuesToCellData(values []interface{}) (cells []*sheets.CellData) {
cells = append(cells, &sheets.CellData{
UserEnteredValue: &sheets.ExtendedValue{NumberValue: &typedValue},
})
case int:
v := float64(typedValue)
cells = append(cells, &sheets.CellData{UserEnteredValue: &sheets.ExtendedValue{NumberValue: &v}})
case int64:
v := float64(typedValue)
cells = append(cells, &sheets.CellData{UserEnteredValue: &sheets.ExtendedValue{NumberValue: &v}})
case bool:
cells = append(cells, &sheets.CellData{
UserEnteredValue: &sheets.ExtendedValue{BoolValue: &typedValue},
Expand All @@ -57,6 +67,62 @@ func GetSpreadSheetURL(spreadsheetId string) string {
return fmt.Sprintf("https://docs.google.com/spreadsheets/d/%s/edit#gid=0", spreadsheetId)
}

func WriteStructHeader(srv *sheets.Service, spreadsheetId string, sheetId int64, structTag string, st interface{}) (*sheets.BatchUpdateSpreadsheetResponse, error) {
typeOfSt := reflect.TypeOf(st)
typeOfSt = typeOfSt.Elem()

var headerTexts []interface{}
for i := 0; i < typeOfSt.NumField(); i++ {
tag := typeOfSt.Field(i).Tag
tagValue := tag.Get(structTag)
if len(tagValue) == 0 {
continue
}

headerTexts = append(headerTexts, tagValue)
}

return AppendRow(srv, spreadsheetId, sheetId, headerTexts)
}

func WriteStructValues(srv *sheets.Service, spreadsheetId string, sheetId int64, structTag string, st interface{}) (*sheets.BatchUpdateSpreadsheetResponse, error) {
typeOfSt := reflect.TypeOf(st)
typeOfSt = typeOfSt.Elem()

valueOfSt := reflect.ValueOf(st)
valueOfSt = valueOfSt.Elem()

var texts []interface{}
for i := 0; i < typeOfSt.NumField(); i++ {
tag := typeOfSt.Field(i).Tag
tagValue := tag.Get(structTag)
if len(tagValue) == 0 {
continue
}

valueInf := valueOfSt.Field(i).Interface()

switch typedValue := valueInf.(type) {
case string:
texts = append(texts, typedValue)
case float64:
texts = append(texts, typedValue)
case int64:
texts = append(texts, typedValue)
case *float64:
texts = append(texts, typedValue)
case fixedpoint.Value:
texts = append(texts, typedValue.String())
case *fixedpoint.Value:
texts = append(texts, typedValue.String())
case time.Time:
texts = append(texts, typedValue.Format(time.RFC3339))
}
}

return AppendRow(srv, spreadsheetId, sheetId, texts)
}

func AppendRow(srv *sheets.Service, spreadsheetId string, sheetId int64, values []interface{}) (*sheets.BatchUpdateSpreadsheetResponse, error) {
row := &sheets.RowData{}
row.Values = ValuesToCellData(values)
Expand Down
24 changes: 15 additions & 9 deletions utils/google-spreadsheet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"

"github.com/c9s/bbgo/pkg/fixedpoint"
googleservice "github.com/c9s/bbgo/pkg/service/google"
"github.com/c9s/bbgo/pkg/types"
)

// Retrieve a token, saves the token, then returns the generated client.
Expand Down Expand Up @@ -100,20 +102,24 @@ func main() {

googleservice.DebugBatchUpdateSpreadsheetResponse(batchUpdateResp)

appendCellsResp, err := googleservice.AppendRow(srv, spreadsheetId, 0, []interface{}{
"Date",
"Net Profit",
"Profit",
"Gross Profit",
"Gross Loss",
"Total Profit",
"Total Loss",
stats := types.NewProfitStats(types.Market{
Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
})
stats.TodayNetProfit = fixedpoint.NewFromFloat(100.0)
stats.TodayPnL = fixedpoint.NewFromFloat(100.0)
stats.TodayGrossLoss = fixedpoint.NewFromFloat(-100.0)

_, err = googleservice.WriteStructHeader(srv, spreadsheetId, 0, "json", stats)
if err != nil {
log.Fatal(err)
}

logrus.Infof("appendCellsResp: %+v", appendCellsResp)
_, err = googleservice.WriteStructValues(srv, spreadsheetId, 0, "json", stats)
if err != nil {
log.Fatal(err)
}

readRange := "Sheet1!A2:E"
resp, err := googleservice.ReadSheetValuesRange(srv, spreadsheetId, readRange)
Expand Down

0 comments on commit e41d720

Please sign in to comment.