program tip

Google 데이터 API를 사용하여 C #으로 Google 스프레드 시트에 액세스

radiobox 2020. 8. 16. 20:04
반응형

Google 데이터 API를 사용하여 C #으로 Google 스프레드 시트에 액세스


Google 스프레드 시트에 단일 시트로 일부 정보가 있습니다. Google 자격 증명과 스프레드 시트 주소를 제공하여 .NET에서이 정보를 읽을 수있는 방법이 있습니까? Google 데이터 API를 사용하는 것이 가능합니까? 궁극적으로 DataTable의 Google 스프레드 시트에서 정보를 가져와야합니다. 내가 어떻게 해? 누군가 시도했다면 pls는 정보를 공유합니다.


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

나는 간단한 래퍼 썼다 주위 구글의 닷넷 클라이언트 라이브러리를 , 그것은 노출 간단한 데이터베이스와 같은 강력한 형식의 기록 유형과 인터페이스. 다음은 몇 가지 샘플 코드입니다.

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

Google의 구조화 된 쿼리 연산자로 변환하는 LINQ 공급자도 있습니다 .

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

(2016 년 6 월 -11 월) 질문과 답변은 다음과 같이 구식입니다. 1) GData API 는 이전 세대의 Google API입니다. 모든 GData API가 더 이상 사용되지는 않지만 모든 최신 Google APIGoogle 데이터 프로토콜을 사용 하지 않습니다 . 2) 새로운 Google Sheets API v4 (GData도 아님)가 있습니다.

Moving forward from here, you need to get the Google APIs Client Library for .NET and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's a C# code sample to help get you started. Also check the .NET reference docs for the Sheets API and the .NET Google APIs Client Library developers guide.

If you're not allergic to Python (if you are, just pretend it's pseudocode ;) ), I made several videos with slightly longer, more "real-world" examples of using the API you can learn from and migrate to C# if desired:


You can do what you're asking several ways:

  1. Using Google's spreadsheet C# library (as in Tacoman667's answer) to fetch a ListFeed which can return a list of rows (ListEntry in Google parlance) each of which has a list of name-value pairs. The Google spreadsheet API (http://code.google.com/apis/spreadsheets/code.html) documentation has more than enough information to get you started.

  2. Using the Google visualization API which lets you submit more sophisticated (almost like SQL) queries to fetch only the rows/columns you require.

  3. The spreadsheet contents are returned as Atom feeds so you can use XPath or SAX parsing to extract the contents of a list feed. There is an example of doing it this way (in Java and Javascript only though I'm afraid) at http://gqlx.twyst.co.za.


I'm pretty sure there'll be some C# SDKs / toolkits on Google Code for this. I found this one, but there may be others so it's worth having a browse around.


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

This should get you started. I haven't played with it lately but I downloaded a very old version a while back and it seemed pretty solid. This one is updated to Visual Studio 2008 as well so check out the docs!


This Twilio blog page made on March 24, 2017 by Marcos Placona may be helpful.

Google Spreadsheets and .NET Core

It references Google.Api.Sheets.v4 and OAuth2.

참고URL : https://stackoverflow.com/questions/725627/accessing-google-spreadsheets-with-c-sharp-using-google-data-api

반응형