简体   繁体   中英

How to Access and Insert Data in to Google Spread Sheet using C# and Sheet Api?

Can one please help me in how to access the Google Spread Sheet Data using there Google Sheet API using oAuth 2.0 also when the sheet is accessed how to insert/add data there online

In .NET C#? I found this Link but it is using oauth 1 and only accessing the data Please provide an example or link

I found the following code that access and write values in columns :

string CLIENT_ID = "***";

        string CLIENT_SECRET = "***";

        string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";

        string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";


        OAuth2Parameters parameters = new OAuth2Parameters();

        parameters.ClientId = CLIENT_ID;

        parameters.ClientSecret = CLIENT_SECRET;

        parameters.RedirectUri = REDIRECT_URI;

        parameters.Scope = SCOPE;

        string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
        Console.WriteLine(authorizationUrl);
        Console.WriteLine("Please visit the URL above to authorize your OAuth "
          + "request token.  Once that is complete, type in your access code to "
          + "continue...");
        parameters.AccessCode = Console.ReadLine();

        OAuthUtil.GetAccessToken(parameters);
        string accessToken = parameters.AccessToken;
        Console.WriteLine("OAuth Access Token: " + accessToken);

        GOAuth2RequestFactory requestFactory =
            new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
        service.RequestFactory = requestFactory;

        SpreadsheetQuery query = new SpreadsheetQuery();

        SpreadsheetFeed feed = service.Query(query);

        //foreach (SpreadsheetEntry entry in feed.Entries)
        //{
        //    Console.WriteLine(entry.Title.Text);
        //}

        SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
        Console.WriteLine(spreadsheet.Title.Text);

        // Get the first worksheet of the first spreadsheet.
        // TODO: Choose a worksheet more intelligently based on your
        // app's needs.
        WorksheetFeed wsFeed = spreadsheet.Worksheets;
        WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

        // Define the URL to request the list feed of the worksheet.
        AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

        // Fetch the list feed of the worksheet.
        ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
        ListFeed listFeed = service.Query(listQuery);


        CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
        CellFeed cellFeed = service.Query(cellQuery);

        CellEntry cellEntry = new CellEntry(1, 1, "firstname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 2, "lastname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 3, "age");

        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 4, "height");
        cellFeed.Insert(cellEntry);

        // Create a local representation of the new row.
        ListEntry row = new ListEntry();
        row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe"  });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "lastname", Value = "Smith" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "age", Value = "26" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "height", Value = "176" });

        // Send the new row to the API for insertion.
        service.Insert(listFeed, row);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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