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

bli*_*egz 102 c# google-sheets google-data-api google-api-dotnet-client google-sheets-api

我在Google Spreadsheets中有一些信息作为单页.有什么办法可以通过提供谷歌凭据和电子表格地址从.NET读取这些信息.是否可以使用Google Data API.最后,我需要从DataTable中获取Google电子表格中的信息.我该怎么做?如果有人尝试过,请分享一些信息.

Kel*_*lly 175

根据.NET用户指南:

下载.NET客户端库:

添加这些使用语句:

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;
Run Code Online (Sandbox Code Playgroud)

认证:

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

获取电子表格列表:

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

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

给定您已检索到的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);
}
Run Code Online (Sandbox Code Playgroud)

并获得基于细胞的饲料:

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);
}
Run Code Online (Sandbox Code Playgroud)

  • _https://developers.google.com/google-apps/spreadsheets/authorize_***重要提示:不再支持OAuth 1.0,并且将于2015年5月5日停用.如果您的应用使用OAuth 1.0,则必须迁移到OAuth 2.0或您的申请将停止运作.*** (5认同)
  • 我应该将什么用于新SpreadsheetsService("`exampleCo-exampleApp-1`")的字符串值?我放在那里的重要性是什么?谢谢! (3认同)

Mau*_*fer 22

Google的.Net客户端库中编写了一个简单的包装器,它公开了一个更简单的类似数据库的接口,具有强类型的记录类型.这是一些示例代码:

public class Entity {
    public int IntProp { get; set; }
    public string StringProp { get; set; }
}

var e1 = new Entity { IntProp = 2 };
var e2 = new Entity { StringProp = "hello" };
var client = new DatabaseClient("you@gmail.com", "password");
const string dbName = "IntegrationTests";
Console.WriteLine("Opening or creating database");
db = client.GetDatabase(dbName) ?? client.CreateDatabase(dbName); // databases are spreadsheets
const string tableName = "IntegrationTests";
Console.WriteLine("Opening or creating table");
table = db.GetTable<Entity>(tableName) ?? db.CreateTable<Entity>(tableName); // tables are worksheets
table.DeleteAll();
table.Add(e1);
table.Add(e2);
var r1 = table.Get(1);
Run Code Online (Sandbox Code Playgroud)

还有一个LINQ提供程序转换为谷歌的结构化查询运算符:

var q = from r in table.AsQueryable()
        where r.IntProp > -1000 && r.StringProp == "hello"
        orderby r.IntProp
        select r;
Run Code Online (Sandbox Code Playgroud)


wes*_*cpy 16

(2016年6月至11月)问题及其答案现在已经过时了:1)GData API是上一代Google API.虽然不是所有的GData API与已弃用,所有最新的谷歌的API没有使用谷歌数据协议 ; 2)有一个新的Google表格API v4(也不是GData).

从这里开始,您需要获取适用于.NET的Google API客户端库并使用最新的Sheets API,它比以前的API更强大,更灵活.这是一个C#代码示例,可帮助您入门.另请查看Sheets API.NET Google API客户端库开发人员指南.NET参考文档.

如果你对Python没有过敏(如果你是,只是假装它是伪代码;)),我制作了几个视频,其中包含更长,更"真实"的使用你可以学习的API的例子,如果需要,可以迁移到C# :