google/sheets: add GetFirstColumn method

This commit is contained in:
c9s 2023-07-20 18:08:36 +08:00
parent aa689b4c61
commit eb9b3563da
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -79,6 +79,7 @@ func (s *SpreadSheetService) NewSheet(title string) (*sheets.Sheet, error) {
return s.LookupSheet(title) 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) { func (s *SpreadSheetService) LookupSheet(title string) (*sheets.Sheet, error) {
spreadsheet, err := s.Get(false) spreadsheet, err := s.Get(false)
if err != nil { if err != nil {
@ -94,6 +95,8 @@ func (s *SpreadSheetService) LookupSheet(title string) (*sheets.Sheet, error) {
return nil, nil 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) { func (s *SpreadSheetService) LookupOrNewSheet(title string) (*sheets.Sheet, error) {
sheet, err := s.LookupSheet(title) sheet, err := s.LookupSheet(title)
if err != nil { if err != nil {
@ -107,6 +110,17 @@ func (s *SpreadSheetService) LookupOrNewSheet(title string) (*sheets.Sheet, erro
return s.NewSheet(title) 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) { func ReadSheetValuesRange(srv *sheets.Service, spreadsheetId, readRange string) (*sheets.ValueRange, error) {
log.Infof("ReadSheetValuesRange: %s", readRange) log.Infof("ReadSheetValuesRange: %s", readRange)
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do() resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()