开发者

How do I insert a row in to a Google spreadsheet using c#

I've seen; Accessing Google Spreadsheets with C# using Google Data API

and

http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_dotnet.html#CreatingRows

However i'm still having trouble inserting a new row in to an existing google spread sheet. Does anyone have a canned example which inserts a List<string> for example in to new r开发者_如何学Goow in a spreadsheet workbook.

Many thanks,


Use GDataDB http://github.com/mausch/GDataDB

GDataDB provides a simple way to insert .net POCO entities in to a google spread sheet.

    public void AddToGoogle()
    {
        var client = new DatabaseClient(Settings.Default.GmailAccount, Settings.Default.GmailPassword);
        string dbName = Settings.Default.WorkBook;

        var db = client.GetDatabase(dbName) ?? client.CreateDatabase(dbName);
        string tableName = Settings.Default.WorkSheet;

        var t = db.GetTable<ActivityLog>(tableName) ?? db.CreateTable<ActivityLog>(tableName);
        var all = t.FindAll();

        t.Add(this);
    }


This Services from Google are discontinued and now they came up with another one named Google.Apis.Sheets.v4 services.

so the above code will not work now a days, I have already tried.

And find something that worked out for me.

I have written a blog and shared the whole source code there. Check it out.

private static SheetsService AuthorizeGoogleApp()
 {
     UserCredential credential;

     using (var stream =
         new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
     {
         string credPath = System.Environment.GetFolderPath(
             System.Environment.SpecialFolder.Personal);
         credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

         credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
             GoogleClientSecrets.Load(stream).Secrets,
             Scopes,
             "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,
     });

     return service;
 }

For the entire source code check it out. Insert new row to Google Sheet using Google.Apis.Sheets.V4 Services

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜