简体   繁体   English

如何使用C#在Google电子表格中插入一行

[英]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 使用Google Data API通过C#访问Google Spreadsheets

and

http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_dotnet.html#CreatingRows 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. 但是我仍然无法在现有的Google电子表格中插入新行。 Does anyone have a canned example which inserts a List<string> for example in to new row in a spreadsheet workbook. 是否有人有一个罐头示例,例如将List<string>插入到电子表格工作簿的新行中。

Many thanks, 非常感谢,

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

GDataDB provides a simple way to insert .net POCO entities in to a google spread sheet. GDataDB提供了一种简单的方法来将.net POCO实体插入到Google电子表格中。

    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. Google停止提供此服务,现在他们又提出了另一个名为Google.Apis.Sheets.v4的服务。

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 使用Google.Apis.Sheets.V4服务将新行插入Google表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM