首页 文章

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

提问于
浏览
97

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

7 回答

  • 3

    这个Twilio博客页面于2017年3月24日由Marcos Placona制作可能会对您有所帮助 .

    Google Spreadsheets and .NET Core

    它引用了Google.Api.Sheets.v4OAuth2 .

  • 2

    根据.NET user guide

    下载.NET client library

    添加这些使用语句:

    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);
    }
    
  • 22

    我_579045_将成为Google Code上的一些C#SDK /工具包 . 我找到了this one,但可能还有其他人因此值得浏览一下 .

  • 13

    (Jun-Nov 2016) 问题及其答案现在已经过时了:1)GData APIs是上一代Google API . 虽然并非所有GData API都已弃用,但all the latest Google APIs不使用the Google Data Protocol; 2)有new Google Sheets API v4(也不是GData) .

    从这里开始,你需要得到the Google APIs Client Library for .NET并使用最新的Sheets API,它比以前的API更强大,更灵活 . 这是一个C# code sample来帮助你入门 . 另请检查.NET reference docs for the Sheets API.NET Google APIs Client Library developers guide .

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

  • 0

    I wrote a simple wrapper围绕Google's .Net client library,它暴露了一个更简单的类似数据库的接口,具有强类型记录类型 . 这是一些示例代码:

    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);
    

    有's also a LINQ provider that translates to google' s structured query operators

    var q = from r in table.AsQueryable()
            where r.IntProp > -1000 && r.StringProp == "hello"
            orderby r.IntProp
            select r;
    
  • 170

    您可以通过以下几种方式进行操作:

    • 使用谷歌_519042的答案)获取一个ListFeed,它可以返回一个行列表(Google用法中的ListEntry),每个行都有一个名称 - 值对列表 . Google电子表格API(http://code.google.com/apis/spreadsheets/code.html)文档提供了足够的信息,可帮助您入门 .

    • 使用Google可视化API,您可以提交更复杂(几乎类似于SQL)的查询,以仅获取所需的行/列 .

    • 电子表格内容作为Atom订阅源返回,因此您可以使用XPath或SAX解析来提取列表订阅源的内容 . 在http://gqlx.twyst.co.za有一个这样做的例子(在Java和Javascript中虽然我害怕) .

  • 2

    http://code.google.com/apis/gdata/articles/dotnet_client_lib.html

    这应该让你开始 . 我最近没玩过它,但我一段时间后下载了一个非常旧的版本,看起来非常稳固 . 这个更新到Visual Studio 2008也是如此,请查看文档!

相关问题