From eb9b3563da05feaea6d9eb3aabfb0354932ba0e8 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 20 Jul 2023 18:08:36 +0800 Subject: [PATCH] google/sheets: add GetFirstColumn method --- pkg/service/google/sheets.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/service/google/sheets.go b/pkg/service/google/sheets.go index 62698b2bb..6b15c2b0f 100644 --- a/pkg/service/google/sheets.go +++ b/pkg/service/google/sheets.go @@ -79,6 +79,7 @@ func (s *SpreadSheetService) NewSheet(title string) (*sheets.Sheet, error) { return s.LookupSheet(title) } +// LookupSheet looks up if there is a sheet matches the given title func (s *SpreadSheetService) LookupSheet(title string) (*sheets.Sheet, error) { spreadsheet, err := s.Get(false) if err != nil { @@ -94,6 +95,8 @@ func (s *SpreadSheetService) LookupSheet(title string) (*sheets.Sheet, error) { return nil, nil } +// LookupSheet looks up if there is a sheet matches the given title +// And if there is no such a sheet, a sheet with that given title will be created. func (s *SpreadSheetService) LookupOrNewSheet(title string) (*sheets.Sheet, error) { sheet, err := s.LookupSheet(title) if err != nil { @@ -107,6 +110,17 @@ func (s *SpreadSheetService) LookupOrNewSheet(title string) (*sheets.Sheet, erro return s.NewSheet(title) } +func (s *SpreadSheetService) GetFirstColumn(sheet *sheets.Sheet) ([][]interface{}, error) { + title := sheet.Properties.Title + readRange := title + "!A1:A" + resp, err := ReadSheetValuesRange(s.service, s.SpreadsheetID, readRange) + if err != nil { + return nil, err + } + + return resp.Values, nil +} + func ReadSheetValuesRange(srv *sheets.Service, spreadsheetId, readRange string) (*sheets.ValueRange, error) { log.Infof("ReadSheetValuesRange: %s", readRange) resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()