Feat: ReadSheet implemented
This commit is contained in:
parent
7828e11046
commit
335e698e5e
|
|
@ -0,0 +1,32 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue