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

使用C#通过API KEY(非OAuth2)实现Google Sheets表格内搜索的问询

Google Sheets API KEY搜索问题解答

API KEY是否支持搜索功能

Google Sheets V4 官方API没有提供原生的服务端搜索端点,无论使用API KEY还是OAuth 2.0认证,都无法直接调用接口完成搜索。但API KEY认证可以正常拉取公开可查看的表格全量数据,你可以在本地对拉取到的数据做过滤实现搜索需求,这个方案是完全支持的。如果表格是私有权限,你无法用API KEY访问,必须改用OAuth 2.0或者服务账号认证。

C# 实现示例

实现逻辑:通过API KEY拉取目标工作表数据,在内存中做关键词匹配过滤,支持全表搜索或指定列搜索。

  • 前置要求:你的Google Sheet已设置为「任何人都可以查看」
  • 替换代码中的占位参数为你自己的实际值即可运行
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Linq;

public class GoogleSheetsSearchDemo
{
    private static readonly HttpClient httpClient = new HttpClient();
    private const string BaseUrl = "https://sheets.googleapis.com/v4/spreadsheets";

    /// <summary>
    /// 搜索指定工作表内容
    /// </summary>
    /// <param name="spreadsheetId">表格ID</param>
    /// <param name="apiKey">你的API KEY</param>
    /// <param name="sheetName">工作表名称,默认第一个工作表为1或Sheet1</param>
    /// <param name="keyword">搜索关键词</param>
    /// <returns>匹配的行数据</returns>
    public static List<List<string>> SearchSheet(string spreadsheetId, string apiKey, string sheetName, string keyword)
    {
        // 如需限定拉取列,可把sheetName改为 {sheetName}!A:C 格式,代表仅拉取A到C列,减少数据量
        var requestUrl = $"{BaseUrl}/{spreadsheetId}/values/{Uri.EscapeDataString(sheetName)}?alt=json&key={apiKey}";
        
        var response = httpClient.GetAsync(requestUrl).Result;
        response.EnsureSuccessStatusCode();
        
        var jsonContent = response.Content.ReadAsStringAsync().Result;
        var sheetData = JsonSerializer.Deserialize<SheetValueResponse>(jsonContent);
        
        if (sheetData?.Values == null || !sheetData.Values.Any())
            return new List<List<string>>();
        
        // 当前为全表搜索,任意单元格包含关键词即匹配
        return sheetData.Values
            .Where(row => row.Any(cell => cell?.ToString().IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0))
            .Select(row => row.Select(c => c.ToString()).ToList())
            .ToList();
    }

    // 适配Google Sheets API返回结构的实体类
    private class SheetValueResponse
    {
        public List<List<object>> Values { get; set; }
    }

    // 测试调用
    public static void Main()
    {
        string yourSpreadsheetId = "替换为你的表格ID";
        string yourApiKey = "替换为你的API KEY";
        string yourSheetName = "1";
        string searchKeyword = "要搜索的关键词";

        var resultRows = SearchSheet(yourSpreadsheetId, yourApiKey, yourSheetName, searchKeyword);

        Console.WriteLine($"共找到{resultRows.Count}条匹配结果:");
        foreach (var row in resultRows)
        {
            Console.WriteLine(string.Join(" | ", row));
        }
    }
}

如需限定搜索指定列,修改过滤逻辑即可,比如仅搜索第二列(索引为1):

.Where(row => row.Count > 1 && row[1]?.ToString().IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 01:45:02