2 Replies - 2142 Views - Last Post: 24 November 2012 - 01:12 AM Rate Topic: -----

#1 DevanF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-August 12

Add Row To Google Spreadsheet

Posted 16 November 2012 - 07:32 PM

So I have been trying to add a row to a google spreadsheet so that I don't have to open and close my browser every time I wanna add something to it.

Here is the website I am using for the API: Google Spreadsheets API version 3.0

Here is my current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace ELOSpreadsheet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            SpreadsheetsService service = new SpreadsheetsService("exampleCo-exampleApp-1");

      // TODO: Authorize the service object for a specific user (see other sections)

      service.setUserCredentials("username@gmail.com", "password");
      SpreadsheetQuery query = new SpreadsheetQuery();

      query.Title = "Gam3rGod's ELO Spreadsheet";
      SpreadsheetFeed feed = service.Query(query);

      if (feed.Entries.Count == 0)
      {
        // TODO: There were no spreadsheets, act accordingly.
      }

      // TODO: Choose a spreadsheet more intelligently based on your
      // app's needs.
      SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
      //Response.Write(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.
      ListEntry row = new ListEntry();
      row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });

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



Every time i try to run this code I get the error:
Execution of request failed: https://spreadsheets...d6/private/full

Is This A Good Question/Topic? 0
  • +

Replies To: Add Row To Google Spreadsheet

#2 bgammill  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 11-June 12

Re: Add Row To Google Spreadsheet

Posted 16 November 2012 - 10:25 PM

Looks like you're not setting the authentication token. Try this code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace ELOSpreadsheet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            SpreadsheetsService service = new SpreadsheetsService("exampleCo-exampleApp-1");

      // TODO: Authorize the service object for a specific user (see other sections)

      service.setUserCredentials("username@gmail.com", "password");
      string token = service.QueryClientLoginToken();
      service.SetAuthenticationToken(token);

      SpreadsheetQuery query = new SpreadsheetQuery();

      query.Title = "Gam3rGod's ELO Spreadsheet";
      SpreadsheetFeed feed = service.Query(query);

      if (feed.Entries.Count == 0)
      {
        // TODO: There were no spreadsheets, act accordingly.
      }

      // TODO: Choose a spreadsheet more intelligently based on your
      // app's needs.
      SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
      //Response.Write(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.
      ListEntry row = new ListEntry();
      row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });

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


Was This Post Helpful? 0
  • +
  • -

#3 DevanF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-August 12

Re: Add Row To Google Spreadsheet

Posted 24 November 2012 - 01:12 AM

View Postbgammill, on 16 November 2012 - 10:25 PM, said:

Looks like you're not setting the authentication token. Try this code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace ELOSpreadsheet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            SpreadsheetsService service = new SpreadsheetsService("exampleCo-exampleApp-1");

      // TODO: Authorize the service object for a specific user (see other sections)

      service.setUserCredentials("username@gmail.com", "password");
      string token = service.QueryClientLoginToken();
      service.SetAuthenticationToken(token);

      SpreadsheetQuery query = new SpreadsheetQuery();

      query.Title = "Gam3rGod's ELO Spreadsheet";
      SpreadsheetFeed feed = service.Query(query);

      if (feed.Entries.Count == 0)
      {
        // TODO: There were no spreadsheets, act accordingly.
      }

      // TODO: Choose a spreadsheet more intelligently based on your
      // app's needs.
      SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
      //Response.Write(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.
      ListEntry row = new ListEntry();
      row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });

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



Still gives the same error...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1