I’ve got a C# program that can read Google Sheets into a DataTable. It works fine when I use a username and password. But now I want to read public sheets without logging in.
Here’s what I’m doing now:
var sheetService = new SheetService("MyApp");
sheetService.Login(user, pass);
var query = new SheetQuery();
var feed = sheetService.RunQuery(query);
var mySheet = feed.Sheets.FirstOrDefault(s => s.Title == sheetName);
This method doesn’t work for public sheets. I tried using the public sheet URL, but no luck. Is there a way to get the sheet data just from the public URL? Maybe something like:
var publicSheet = SheetService.GetPublicSheet("SHEET_ID_HERE");
Does anyone know how to do this? Thanks for any help!
I’ve found a straightforward method for accessing public Google Sheets without authentication. You can use the Google Sheets API v4 client library for .NET. Here’s a concise code snippet:
using Google.Apis.Sheets.v4;
using Google.Apis.Services;
var service = new SheetsService(new BaseClientService.Initializer());
var spreadsheetId = "YOUR_SHEET_ID";
var range = "Sheet1!A1:Z";
var request = service.Spreadsheets.Values.Get(spreadsheetId, range);
var response = request.Execute();
var values = response.Values;
This approach doesn’t require API keys or login credentials for public sheets. Just replace ‘YOUR_SHEET_ID’ with the actual ID from your sheet’s URL. The range parameter can be adjusted as needed. Remember to add the necessary NuGet package to your project.
I’ve actually tackled this issue before. For public Google Sheets, you don’t need authentication - just the sheet ID. Here’s what worked for me:
First, grab the Google.Apis.Sheets.v4 NuGet package. Then use something like this:
var service = new SheetsService(new BaseClientService.Initializer());
var spreadsheetId = “YOUR_SHEET_ID”;
var range = “Sheet1!A1:Z1000”;
var request = service.Spreadsheets.Values.Get(spreadsheetId, range);
var response = request.Execute();
var values = response.Values;
This approach is simpler and more efficient for public sheets. Just make sure you’re using the correct spreadsheet ID from the URL. Let me know if you run into any snags!