package main
import (
"context"
"fmt"
"log"
"os"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func main() {
// Set your Google Sheets API credentials file path
credentialsFilePath := "path/to/your/credentials.json"
// Set your Google Sheets spreadsheet ID and range
spreadsheetID := "your-spreadsheet-id"
readRange := "Sheet1!A1" // Update with your sheet and cell information
// Create a context
ctx := context.Background()
// Read Google Sheets API credentials file
credentialsFile, err := os.Open(credentialsFilePath)
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// Create a Google Sheets service
srv, err := sheets.NewService(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
// Make a request to retrieve the cell value
resp, err := srv.Spreadsheets.Values.Get(spreadsheetID, readRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
}
// Check if there are values in the response
if len(resp.Values) == 0 {
fmt.Println("No data found.")
return
}
// Extract and print the cell value
cellValue := resp.Values[0][0].(string)
fmt.Printf("Cell value: %s\n", cellValue)
}