I have a problem, GoogleSheetsApi does not see the table, despite the fact that it is configured and should work. I tried everything and couldn’t solve the problem. Tell me, maybe someone knows a solution to the problem?
I tried different options, changed the Api key, tried inserting another table and even changed the architecture code, but it didn’t get me anywhere.
public partial class MainWindow : Window
{
private readonly GoogleSheetsService _googleSheetsService;
private readonly List<CartItem> _cart = new List<CartItem>();
public MainWindow(GoogleSheetsService googleSheetsService)
{
InitializeComponent();
// API
string apiKey = "";
_googleSheetsService = new GoogleSheetsService(new SheetsService(new BaseClientService.Initializer()
{
ApiKey = apiKey,
ApplicationName = "Google Sheets API"
}), apiKey);
LoadProducts();
}
private async void LoadProducts()
{
try
{
string spreadsheetId = "";
var values = await _googleSheetsService.GetValuesAsync("Sheet1!A2:C", spreadsheetId);
if (values != null && values.Count() > 0)
{
var products = values.Select(v => new Product
{
Name = v[0].ToString(),
Quantity = int.Parse(v[1].ToString()),
Price = decimal.Parse(v[2].ToString())
}).ToList();
ProductsDataGrid.ItemsSource = products;
}
}
catch (Google.GoogleApiException ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
private void AddToCart_Click(object sender, RoutedEventArgs e)
{
var product = (sender as FrameworkElement).DataContext as Product;
if (product != null)
{
var cartItem = _cart.FirstOrDefault(c => c.Name == product.Name);
if (cartItem != null)
{
cartItem.Quantity++;
}
else
{
_cart.Add(new CartItem { Name = product.Name, Quantity = 1, Price = product.Price });
}
MessageBox.Show("Added to cart");
}
}
private void ViewCartButton_Click(object sender, RoutedEventArgs e)
{
CartWindow cartWindow = new CartWindow(_googleSheetsService, _cart);
cartWindow.Show();
}
}
New contributor
LUCIFER_ LUCIFER_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.