小编典典

使用Google Data API通过C#访问Google Spreadsheets

c#

我在Google
Spreadsheets中有一些信息作为一个表格。有什么方法可以通过提供google凭据和电子表格地址从.NET读取此信息。是否可以使用Google
Data API。最终,我需要从DataTable中的Google电子表格中获取信息。我该怎么做?如果有人尝试过,请共享一些信息。


阅读 357

收藏
2020-05-19

共1个答案

小编典典

根据.NET用户指南

下载.NET客户端库

添加以下using语句:

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

认证:

SpreadsheetsService myService = new SpreadsheetsService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo@gmail.com", "mypassword");

获取电子表格列表:

SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = myService.Query(query);

Console.WriteLine("Your spreadsheets: ");
foreach (SpreadsheetEntry entry in feed.Entries)
{
    Console.WriteLine(entry.Title.Text);
}

给定一个已经获取的SpreadsheetEntry,您可以在此电子表格中获得所有工作表的列表,如下所示:

AtomLink link = entry.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, null);

WorksheetQuery query = new WorksheetQuery(link.HRef.ToString());
WorksheetFeed feed = service.Query(query);

foreach (WorksheetEntry worksheet in feed.Entries)
{
    Console.WriteLine(worksheet.Title.Text);
}

并获取基于单元格的供稿:

AtomLink cellFeedLink = worksheetentry.Links.FindService(GDataSpreadsheetsNameTable.CellRel, null);

CellQuery query = new CellQuery(cellFeedLink.HRef.ToString());
CellFeed feed = service.Query(query);

Console.WriteLine("Cells in this worksheet:");
foreach (CellEntry curCell in feed.Entries)
{
    Console.WriteLine("Row {0}, column {1}: {2}", curCell.Cell.Row,
        curCell.Cell.Column, curCell.Cell.Value);
}
2020-05-19