6

I'm trying to add a row to google spreadsheet. They give a source https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row bu this source is not working for me can anyone tell me please whats is wrong with lines witch include name "row". "Error 11 The name 'row' does not exist in the current context"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace Google_test3
{
class Program
{
    static void Main(string[] args)
    {
        string USERNAME = "test";
        string PASSWORD = "test";
        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");

        service.setUserCredentials(USERNAME, PASSWORD);



        // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
        SpreadsheetQuery query = new SpreadsheetQuery();

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.Query(query);

        if (feed.Entries.Count == 0)
        {
            Console.WriteLine("None");
        }

        // TODO: Choose a spreadsheet more intelligently based on your
        // app's needs.
        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);
        // Create a local representation of the new row.
        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);
    }
}
}
wp78de
  • 18,207
  • 7
  • 43
  • 71
zee
  • 415
  • 1
  • 7
  • 13
  • hello, in my case i get an error because the line AtomLink listFeedLink = newWsEntry.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null); the listFeedLink is null – Edd Jun 07 '12 at 07:27
  • This API version has been discontinued and superseded by Google.Apis.Sheets.v4. – wp78de Dec 28 '17 at 00:37
  • Google.Apis.Sheets.v4 is confusing. I have been looking for over an hour on how to insert a row without learning some obscure scripting language. Just give me some function to call in c# !!!!! – PastExpiry.com Oct 16 '21 at 06:19

2 Answers2

3

There's a line missing in the example in the documentation:

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

If you switch to the Java view, you can see that the Java version includes this line.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    Thanks for that, but know again probably I missing something else because is halted on last row // Send the new row to the API for insertion. service.Insert(listFeed, row); Execution of request failed: https://spreadsheets.google.com/feeds/list/t-KSoisgoJpEyyOznXnjwJw/od6/private/full – zee May 09 '12 at 15:10
  • @zee have you found any solution for the issue you mentioned in your comment. I am facing the same issue. Please help. – techV Dec 11 '15 at 10:59
1

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

Tapan kumar
  • 6,719
  • 1
  • 24
  • 25