C# 从特定单元格读取google电子表格

C# 从特定单元格读取google电子表格,c#,google-sheets,google-docs,C#,Google Sheets,Google Docs,我正在尝试在我们的应用程序中阅读谷歌电子表格,到目前为止,它运行良好 但是,如何根据提供的值和读数查找特定单元格 该单元格中的电子表格。 例如,电子表格包含100行,其中有2列(名称、城市)的数据 所以我需要从名称列中找到单元格id。例如,Name='John'将CellID返回为'A50'。然后我将开始从A50读取电子表格 下面是我的代码,我从硬编码的单元格id/范围读取数据 static string[] DocsScopes = { SheetsService.Scope.Spreadsh

我正在尝试在我们的应用程序中阅读谷歌电子表格,到目前为止,它运行良好

但是,如何根据提供的值和读数查找特定单元格 该单元格中的电子表格。 例如,电子表格包含100行,其中有2列(名称、城市)的数据

所以我需要从名称列中找到单元格id。例如,Name='John'将CellID返回为'A50'。然后我将开始从A50读取电子表格

下面是我的代码,我从硬编码的单元格id/范围读取数据

static string[] DocsScopes = { SheetsService.Scope.SpreadsheetsReadonly };
static string ApplicationName = "Other client 2";

private static void ReadExcelSheet()
        {
            UserCredential credential;

            using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {

                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    DocsScopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define request parameters.
            string spreadsheetId = "1_kJmQsQ2tQxTe0K8upz3byrfSfQ9db_jIitvq3wqsaza8Yzo";
            string range = "Sheet1!A2:B";
            SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);

            ValueRange response = request.Execute();
            IList<IList<object>> values = response.Values;
            if (values != null && values.Count > 0)
            {
                foreach (var row in values)
                {
                    Console.WriteLine("{0}, {1}", row[0], row[1]);
                }
            }
            else
            {
                Console.WriteLine("No data found.");
            }

            Console.Read();
        }
static string[]DocsScopes={SheetsService.Scope.spreadsheetsreadoly};
静态字符串ApplicationName=“其他客户端2”;
私有静态无效ReadExcelSheet()
{
用户凭证;
使用(var stream=newfilestream(“credentials.json”、FileMode.Open、FileAccess.Read))
{
字符串credPath=“token.json”;
凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
船坞,
“用户”,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
Console.WriteLine(“凭证文件保存到:”+credPath);
}
//创建GoogleSheetsAPI服务。
var service=new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=ApplicationName,
});
//定义请求参数。
字符串spreadsheetId=“1_kJmQsQ2tQxTe0K8upz3byrfSfQ9db_jIitvq3wqsaza8Yzo”;
字符串范围=“Sheet1!A2:B”;
电子表格sresource.ValuesResource.GetRequest request=service.Spreadsheets.Values.Get(电子表格ID,范围);
ValueRange响应=请求。执行();
IList值=响应值;
if(值!=null&&values.Count>0)
{
foreach(值中的var行)
{
WriteLine(“{0},{1}”,行[0],行[1]);
}
}
其他的
{
Console.WriteLine(“未找到数据”);
}
Console.Read();
}