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