Feat: Sheet parsing
This commit is contained in:
parent
335e698e5e
commit
32f07ce90b
|
|
@ -1,20 +1,29 @@
|
|||
package rota
|
||||
package dyzurbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/sheets/v4"
|
||||
)
|
||||
|
||||
// ReadRota czyta zadany zakres z arkusza i zwraca wiersze.
|
||||
// spreadsheetID to ID z URL-a arkusza, readRange np. "Dyżury!A1:C30".
|
||||
func ReadSheet(ctx context.Context, credsPath, spreadsheetID, readRange string) ([][]interface{}, error) {
|
||||
srv, err := sheets.NewService(ctx,
|
||||
option.WithAuthCredentialsFile(option.ServiceAccount, credsPath),
|
||||
option.WithScopes(sheets.SpreadsheetsReadonlyScope),
|
||||
)
|
||||
// Klucz API jest wczytywany z pliku .env (zmienna GOOGLE_API_KEY).
|
||||
func ReadSheet(ctx context.Context, spreadsheetID, readRange string) ([][]interface{}, error) {
|
||||
// godotenv.Load nie nadpisuje już ustawionych zmiennych środowiskowych;
|
||||
// brak pliku .env nie jest błędem (na produkcji zmienne mogą być ustawione inaczej).
|
||||
_ = godotenv.Load()
|
||||
|
||||
apiKey := os.Getenv("GOOGLE_API_KEY")
|
||||
if apiKey == "" {
|
||||
return nil, fmt.Errorf("missing GOOGLE_API_KEY in environment/.env")
|
||||
}
|
||||
|
||||
srv, err := sheets.NewService(ctx, option.WithAPIKey(apiKey))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init sheets service: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
package dyzurbot
|
||||
Loading…
Reference in New Issue