68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package sheet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"recipe-manager/config"
|
|
|
|
"google.golang.org/api/option"
|
|
"google.golang.org/api/sheets/v4"
|
|
)
|
|
|
|
type SheetService interface {
|
|
GetSheet(ctx context.Context, sheetID string) [][]interface{}
|
|
}
|
|
|
|
type sheetService struct {
|
|
service *sheets.Service
|
|
}
|
|
|
|
func NewSheetService(ctx context.Context, cfg *config.ServerConfig) (SheetService, error) {
|
|
|
|
if cfg.APIKey == "" {
|
|
return nil, errors.New("API_KEY is empty")
|
|
}
|
|
|
|
service, err := sheets.NewService(ctx, option.WithAPIKey(cfg.APIKey))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &sheetService{
|
|
service: service,
|
|
}, nil
|
|
}
|
|
|
|
func (s *sheetService) GetSheet(ctx context.Context, sheetID string) [][]interface{} {
|
|
spreadSheet, err := s.service.Spreadsheets.Get(sheetID).Do()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var sheetIndex int
|
|
for i, sheet := range spreadSheet.Sheets {
|
|
// print data collumn C
|
|
if sheet.Properties.Title == "menu-name" {
|
|
sheetIndex = i
|
|
break
|
|
}
|
|
}
|
|
|
|
sheet, err := s.service.Spreadsheets.Values.Get(sheetID, spreadSheet.Sheets[sheetIndex].Properties.Title).Do()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
result := [][]interface{}{}
|
|
|
|
for _, row := range sheet.Values {
|
|
if len(row) >= 6 {
|
|
result = append(result, row)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|