33 lines
888 B
Go
33 lines
888 B
Go
package rota
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"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),
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init sheets service: %w", err)
|
|
}
|
|
|
|
resp, err := srv.Spreadsheets.Values.
|
|
Get(spreadsheetID, readRange).
|
|
ValueRenderOption("UNFORMATTED_VALUE"). // surowe wartości, bez formatowania komórek
|
|
Context(ctx).
|
|
Do()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get values %q: %w", readRange, err)
|
|
}
|
|
|
|
return resp.Values, nil
|
|
}
|