You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sheets API报NotFound错误:无法读取表格的解决方案求助

解决Google Sheets API调用NotFound异常的方案

核心排查点

  • 表格权限配置:必须将目标表格设置为「任何知道链接的人都能查看」。操作:打开表格→右上角「共享」→点击「更改」→选择「任何知道链接的人」→权限设为「查看者」→保存。
  • Spreadsheet ID正确性:从表格URL中提取正确ID,格式为https://docs.google.com/spreadsheets/d/[这里是SPREADSHEET_ID]/edit,确保ID无多余字符。
  • 工作表名称匹配:确认代码中Sheet1!A2:C的Sheet1和表格内的工作表名称完全一致(区分大小写、空格),比如表格里是「产品表」就必须改成「产品表!A2:C」。
  • API服务启用状态:登录Google Cloud Console→找到你的项目→搜索「Sheets API」→确认服务已启用(未启用则点击「启用」)。

代码修正建议

你的代码中GoogleSheetsService的初始化方式存在冗余,建议直接使用官方SheetsService初始化逻辑,避免自定义封装可能带来的问题:

public partial class MainWindow : Window
{
    private readonly SheetsService _sheetsService;
    private readonly List<CartItem> _cart = new List<CartItem>();

    public MainWindow()
    {
        InitializeComponent();

        // 初始化官方SheetsService
        string apiKey = "你的API Key";
        _sheetsService = new SheetsService(new BaseClientService.Initializer()
        {
            ApiKey = apiKey,
            ApplicationName = "Google Sheets API"
        });

        LoadProducts();
    }

    private async void LoadProducts()
    {
        try
        {
            string spreadsheetId = "你的表格ID";
            string range = "Sheet1!A2:C";

            // 使用官方ValuesResource.Get方法
            var request = _sheetsService.Spreadsheets.Values.Get(spreadsheetId, range);
            var response = await request.ExecuteAsync();
            
            var values = response.Values;
            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}\n详细错误:{ex.Error}");
        }
    }

    // 以下AddToCart_Click和ViewCartButton_Click代码保持不变
    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(_sheetsService, _cart);
        cartWindow.Show();
    }
}

额外验证步骤

  1. 用浏览器直接访问API测试链接,确认是否能获取数据:
    https://sheets.googleapis.com/v4/spreadsheets/[你的表格ID]/values/Sheet1!A2:C?key=[你的API Key]
    
    如果浏览器返回数据,说明权限和ID没问题,问题出在代码;如果返回NotFound,说明表格权限或ID有误。
  2. 检查API Key的限制:确保API Key没有设置IP限制或仅允许特定服务使用,若有限制需调整为允许Sheets API访问。

内容的提问来源于stack exchange,提问作者LUCIFER_ LUCIFER_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:31:16