Wednesday, June 27, 2012

SpreadSheet API with .NET:
How to read data from Private Google Spreadsheet ?

To expand our learning towards Google SpreadSheet API, in this article we will learn to read data from private Google SpreadSheets.

As you know you can store data into your Spreadsheet and publish it to the world. When you publish the spreadsheet, it became available into public domain. In our last article, we learn to read data from publish Google spreadsheet. The SpreadSheet which we have not share or published that are Private Google Spreadsheet and are available to its owner. We will try to read data from these private spreadsheets in this post.

To start with let us understand what we are going to read. I have two Google SpreadSheet available that I have uploaded to Google Doc. The first spreadsheet is called "ProductDetails" and it has one worksheet with following data.

Product Detail:

The second spreadsheet, I have is called “EmpDetails”. This Spreadsheet has two worksheets with following data into it.

EmpDetails:

Our goal is to use SpreadSheet API and read the data from the above two Spreadsheets. We want to read data without opening the browser or login into Google doc. We have following .NET interface to connect, pull and read the data from Google Spreadsheets and worksheets.

Let us talk about the interface first – We have two ListView Control. The ListView 1 shows all the Google Spreadsheets, we have stored into our Google Doc. The code logic for the same is written on button "Pull SpreadSheet List". When user selects an individual Spreadsheet in Listview 1, the Listview 2 shows all the worksheets available in that Google Spreadsheet.

After selecting an individual worksheet from ListView 2, user can click on button "Read Data from SpreadSheet". The code logic written on this button will connect with the Google Spreadsheet and related worksheet, and read the data from it. The data is then shown in the Data grid control.

To use Google Data API in our .NET project, we first need to add references of Google Data API DLLs in our project. After that, we declare that we are using following Google Data API namespaces in our program/form.

using Google.GData;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

We have declared following two variables at form level. These variables will store the Spreadsheet and worksheet name selected by the user on the form.

public partial class Form2 : Form
{
  string spreadsheetName;
  string wrkSheetName;

The .NET cod logic on button "Pull SpreadSheet List" is following.

private void button1_Click_1(object sender, EventArgs e)
{
   listView1.Columns.Add("SpreadSheet Name");
   listView1.Columns.Add("Summary");
   listView1.Columns.Add("Published Date");
   listView1.FullRowSelect = true;

   SpreadsheetsService GoogleExcelService;
   GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
   GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

   SpreadsheetQuery query = new SpreadsheetQuery();
   SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

   foreach (SpreadsheetEntry mySpread in myFeed.Entries)
   {
    listView1.Items.Add(new ListViewItem(new string[] { mySpread.Title.Text, mySpread.Summary.Text, mySpread.Updated.ToShortDateString() }));
   }
}

The Listview 1 control has following .NET code logic on SelectedIndexChanged event.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
   spreadsheetName = "";
   
   listView2.Clear();
   listView2.Columns.Add("Worksheet Name");
   listView2.Columns.Add("No. of Columns");
   listView2.Columns.Add("No. of Rows");
   listView2.Columns.Add("Summary");
   listView2.FullRowSelect = true;

   ListView.SelectedListViewItemCollection SpreadsheetList = listView1.SelectedItems;

   foreach (ListViewItem item in SpreadsheetList)
   {
    spreadsheetName = item.SubItems[0].Text.ToString();

   }
   if (spreadsheetName.Length > 0)
   {
    SpreadsheetsService GoogleExcelService;
    GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
    GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

    SpreadsheetQuery query = new SpreadsheetQuery();
    SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

    foreach (SpreadsheetEntry mySpread in myFeed.Entries)
    {
     if (mySpread.Title.Text == spreadsheetName)
     {
      WorksheetFeed wfeed = mySpread.Worksheets;
      foreach (WorksheetEntry wsheet in wfeed.Entries)
      {
       listView2.Items.Add(new ListViewItem(new string[] { wsheet.Title.Text, wsheet.Cols.ToString(), wsheet.Rows.ToString(), wsheet.Summary.Text }));

      }
     }
    }
   }
}

The Listview 2 control has following .NET code logic on SelectedIndexChanged event.

private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
 wrkSheetName = "";
 ListView.SelectedListViewItemCollection WorkSheetList = listView2.SelectedItems;

 foreach (ListViewItem item in WorkSheetList)
 {
 wrkSheetName = item.SubItems[0].Text.ToString();
 }
}

The code logic on button "Read Data from Worksheet" is following.

private void button6_Click(object sender, EventArgs e)
 {
  if (spreadsheetName.Length > 0)
  {
  SpreadsheetsService GoogleExcelService;
  GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
  GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");
  
  
  SpreadsheetQuery query = new SpreadsheetQuery();
  SpreadsheetFeed myFeed = GoogleExcelService.Query(query);


  foreach (SpreadsheetEntry mySpread in myFeed.Entries)
  {
   if (mySpread.Title.Text == spreadsheetName)
   {
   WorksheetFeed wfeed = mySpread.Worksheets;
   foreach (WorksheetEntry wsheet in wfeed.Entries)
   {
    if (wsheet.Title.Text == wrkSheetName)
    {
    AtomLink atm = wsheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);
    ListQuery Lquery = new ListQuery(atm.HRef.ToString());
    ListFeed LFeed = GoogleExcelService.Query(Lquery);

    DataTable myTable = new DataTable();
    DataColumn DC;

    foreach (ListEntry LmySpread in LFeed.Entries)
    {
     DataRow myDR = myTable.NewRow();
     foreach (ListEntry.Custom listrow in LmySpread.Elements)
     {
     DC = myTable.Columns[listrow.LocalName] ?? myTable.Columns.Add(listrow.LocalName);
     myDR[DC] = listrow.Value;
     }
     myTable.Rows.Add(myDR);
    }
    dataGridView1.DataSource = myTable;
    dataGridView1.Refresh();
    }
   }
   }
  }
  }
  System.Windows.Forms.MessageBox.Show("Data Reading is Completed");
}

After setting up the code, I connected with the internet, and run the form. I clicked on button Pull SpreadSheet List. It showed me the two Google Spreadsheet that I have in my Google Doc account. The next thing, I did was to select Spreadsheet EmpDetails. The worksheets of spreadsheet EmpDetails were shown in ListView 2. I selected Worksheet EmpDetails and clicked on button Read Data from Spreadsheet. The data of worksheet was read and shown in the data grid.

Reading worksheet Sheet 6 data into data grid

Example of reading data from worksheet sheet 1 under Google Spreadsheet ProductDetails.

That is all for this post. We have learnt to read data from private Google spreadsheet and worksheets.

Thanks for reading till this point.

If you want to explore more with the Source code of this post; please visit Download Zone.

Download Zone @ SinghVikash.blogspot.com

Welcome to the Download Zone, a repository to find the source code of articles, samples published at www.singhvikash.blogspot.com

The source code is arranged in different categories, please check the category and click on the Download link. This will take you to the Skydrive where the sample files are stored in zip format. You can click the zip file and save it your device.

SpreadSheet API with .NET
1. How to add new row into Google SpreadSheet? Download
2. How to delete individual row from Google Spreadsheet? Download
3. How to read data from Private Google SpreadSheet? Download
4. How to read data from Published Google SpreadSheet? Download
5. How to delete an existing worksheet from Google Spreadsheet? Download Link
6. How to modify an existing Google Spreadsheet Worksheet? Download Link
7. How to create new worksheet with Google Spreadsheet? Download Link
8. How to read Google SpreadSheet properties? Download Link
9. How to add blank rows and columns into Google SpreadSheet? Download Link
10. How to pull spreadsheet list from Google Doc? Download Link
Picasa web API with .NET
1. How to get Started with Google Data API aka GData? Download Link
2. How to use Picasa Web Album with Google Data API? Download
3. How to use Picasa Web Album with Google Data API – Part 3? Download Link
4. How to create new Picasa Web Album with Google Data API? Download Link
5. How to pull photo list from Picasa web Album with Google Data API? Download Link
6. How to upload image in Picasa Web Album with Google Data API? Download Link
7. How to delete image from Picasa Web Album with Google Data API? Download Link
8. How to upload multiple images to Picasa Web Album with Google Data API? Download Link
9. How to download images from Picasa web Album with Google Data API? Download
10. How to pull Album ID from Picasa Web Album with Google Data API? Download
11. How to pull thumbnail of all photos from Picasa web Album? Download
SQL Server Integration Services (SSIS)
1. SSIS: How to load data into SQL table from Google Spreadsheets? Download
2. SSIS: How to pull Stock Quotes from Google Finance? Download Link
3. SSIS: How to pull Currency Rates from European Central Bank? Download
4. SSIS: Read public data of Twitter Users Download
5. SSIS: How to read Excel Meta Data? Download
6. SSIS: How to Load images into Picasa web album? Download Link
7. SSIS: How to Download images from Picasa album? Download Link
8. SSIS: How to generate HyperLink into Excel Output? Download

Note:
Please note that by downloading the source code you agree that the execution of source code is your responsibiliy. Any trouble or loss you encounter while executing the code is your responsibility. This site (www.singhvikash.blogspot.com) or the author is not responsible for any damage done while executing the source code.

Sunday, June 24, 2012

SpreadSheet API with .NET:
How to read data from Published Google SpreadSheet?

This post is next in string of our learning Google SpreadSheet API with .NET. We have learn some very fine basic things about SpreadSheet API and how to integrate it with your .NET client applications. We have learn to create new Worksheet, read and modify worksheet properties, pull SpreadSheet list, delete worksheet in this series till this point.

In this post, we will learn to read data from Google published Spreadsheet. A Published SpreadSheet is one that is available to you to share with the whole world. Evey published worksheet has a dedicated link that you can share with anyone. When you published a SpredSheet, Google Doc attach a SpreadSheet Key as an unique identifaction.

Publishing a worksheet is very simple with Google Doc. All you have to do is to login into your Google Doc account and select your SpreadSheet and click on the Share button on top right.

This will open the Sharing Setting page with a google generated link. This link contains the SpreadSheet Key which you need to note down for this post. You can click on Access setting to change the setting so that it can be accessed by the world or you can provide emails who will have the access to this shared Spreadsheet.

With Google SpreadSheet API, we can read the data present in the published SpreadSheet. For this post, I published the Google SpreadSheet EmpDetails that I uploaded to Google Doc and noted down the SpreadSheet Key.

We have following .NET GUI interface. We have two Listview control and a Datagrid control and two buttons on the form. ListView 1 will display all the SpreadSheet List available in our Google Doc account when we click on button Pull SpreadSheet List. ListView 2 will display the Worksheets that are availabe in the SpreadSheet which we select from the list view 1. When we click on button Read Data from Spreadsheet, the data of SpreadSheet will be shown in the DataGrid.

To start within the code, we are using following Google Data API namespace in our program.

using Google.GData;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

We have following form-level variable in our program. This variable will store the Google Spreadsheet name that we select from List View 1.

public partial class Form1 : Form
{
 string spreadsheetName;

The .NET code logic for the button “Pull Spreadsheet List” is following. The code is self explanatory. We are creating a Spreadsheet Service and passing our Gmail credentials. If you are using Client Login authentication methodology, then you have e to pass the credentials. We are creating a Spread Sheet query and passing it to Google Spread sheet feed. The Spreadsheet feed connects with the Google Doc and return feed entries. Each of the feed entry represents a Google Spreadsheet.

private void button1_Click(object sender, EventArgs e)
{
   listView1.Columns.Add("SpreadSheet Name");
   listView1.Columns.Add("Summary");
   listView1.Columns.Add("Published Date");
   listView1.FullRowSelect = true;

   SpreadsheetsService GoogleExcelService;
   GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
   GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

   SpreadsheetQuery query = new SpreadsheetQuery();
   SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

   foreach (SpreadsheetEntry mySpread in myFeed.Entries)
   {
    listView1.Items.Add(new ListViewItem(new string[] { mySpread.Title.Text, mySpread.Summary.Text, mySpread.Updated.ToShortDateString() }));

   }

}

The .NET code logic on ListView 2 SelectedIndexChanged event is following. Here we are showing the storing the selected Google SpreadSheet name and later reading its properties (no. of worksheet, rows and columns)

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
   spreadsheetName = "";
   listView2.Clear();
   listView2.Columns.Add("Worksheet Name");
   listView2.Columns.Add("No. of Columns");
   listView2.Columns.Add("No. of Rows");
   listView2.Columns.Add("Summary");
   ListView.SelectedListViewItemCollection SpreadsheetList = listView1.SelectedItems;

   foreach (ListViewItem item in SpreadsheetList)
   {
    spreadsheetName = item.SubItems[0].Text.ToString();

   }

   if (spreadsheetName.Length > 0)
   {
    SpreadsheetsService GoogleExcelService;
    GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
    GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

    SpreadsheetQuery query = new SpreadsheetQuery();
    SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

    foreach (SpreadsheetEntry mySpread in myFeed.Entries)
    {
     if (mySpread.Title.Text == spreadsheetName)
     {
      WorksheetFeed wfeed = mySpread.Worksheets;
      foreach (WorksheetEntry wsheet in wfeed.Entries)
      {
       listView2.Items.Add(new ListViewItem(new string[] {wsheet.Title.Text, wsheet.Cols.ToString(), wsheet.Rows.ToString(), wsheet.Summary.Text }));

      }
     }
    }
   }   
  }

The .NET code logic on button Read Data from Spreadsheet is following. Here we are using ListQuery. You have to remember that if you want to read individual rows from the Spreadsheet then you have to use ListQuery. Each of the entry inside ListQuery represents individual SpreadSheet row. We are passing the SpreadSheet Key to the ListQuery, which we noted while publishing it. We are creating a DataTable and adding rows and columns into it.

private void button6_Click(object sender, EventArgs e)
{
   if (spreadsheetName.Length > 0)
   {
    SpreadsheetsService GoogleExcelService;
    GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
    GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");
    
    ListQuery query = new ListQuery("0AmYgMIof-5mgdGM2OGxoTmUyc3JRTFlMZ1BTUG5SOVE", "1", "public", "values");
    ListFeed myFeed = GoogleExcelService.Query(query);

    DataTable myTable = new DataTable();
    DataColumn DC;

    foreach (ListEntry mySpread in myFeed.Entries)
    {
     DataRow myDR = myTable.NewRow();
      foreach (ListEntry.Custom listrow in mySpread.Elements)
      {
        DC = myTable.Columns[listrow.LocalName] ?? myTable.Columns.Add(listrow.LocalName);
       myDR[DC] = listrow.Value;
       }
      myTable.Rows.Add(myDR);
     }
    dataGridView1.DataSource = myTable;
    dataGridView1.Refresh();
    }
    System.Windows.Forms.MessageBox.Show("Rows and Columns Added Successfully");
   } 
}

After setting up the code, I connected with the internet and run the form. I clicked on the Pull SpreadSheet List button and selected EmpDetail SpreadSheet. After that, I clicked on button Read Data from SpreadSheet and as expected the code logic runs successfully and read all the data and display it under data grid.

So our objective of reading data from a published Google SpreadSheet is achived. We can enhance the program to return the Google SpreadSheet key and make this interface more dynamic.

Thanks for reading this post.

Saturday, June 23, 2012

SpreadSheet API with .NET:
How to delete an existing worksheet from Google Spreadsheet?

In order to continue our learning towards Google Spreadsheet API, this post will help to understand how we can delete an existing worksheet from Google Spreadsheet. The Google Spreadsheet API provided by Google team work as an interface if we want to communicate with Google Spreadsheets without being on Google Doc.

To start with, we have following spreadsheet available in my Google Doc. We have EmpDetails spreadsheet and it two worksheets EmpDetails and EmpSalaryDetails. We will delete the existing worksheet EmpSalaryDetails from this spreadsheet.

We have following .NET GUI interface to deal with this example.

We have two listview control and two buttons on the form. The concept is when we click on Pull SpreadSheet List button; the .NET code will connect with the Google Doc and pull all the spreadsheet list available and show it in Listview 1 control. After selecting an individual SpreadSheet from ListView1, all the available worksheet in that spreadsheet and their details will be shown in ListView 2. We will select the worksheet that we want to delete from ListView 2 and click on Delete Worksheet; the code logic will connect with the Google Doc and delete the selected worksheet under Spreadsheet.

We have downloaded and installed the Google Data API library, so we are using following namespace in our program.

using Google.GData;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

We are using following two variables in our form spreadsheetName stores the Google Spreadsheet name which users select from the list. Variable wrkSheetName stores the worksheet name which users selects from Listview 2.

public partial class Form1 : Form
{
 string spreadsheetName;
 string wrkSheetName;

The .NET code snippet on button “Pull SpreadSheet List” is following. In this code logic, we are defining an instance of Google Spreadsheets Service and passing the Gmail credentials to connect with Googl e Doc. We are creating the Spreasheet Query and passing it to the Spreadsheet feed. The Spreadsheet feed connects with the Google Doc and returns feed. Each feed represents a Google Spreadsheet. We have a foreach loop where we are navigating through each of the feed and reading its name, summary and updated date.

private void button1_Click(object sender, EventArgs e)
 {
  listView1.Columns.Add("SpreadSheet Name");
  listView1.Columns.Add("Summary");
  listView1.Columns.Add("Published Date");
  listView1.FullRowSelect = true;

  SpreadsheetsService GoogleExcelService;
  GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
  GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

  SpreadsheetQuery query = new SpreadsheetQuery();
  SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

  foreach (SpreadsheetEntry mySpread in myFeed.Entries)
  {
  listView1.Items.Add(new ListViewItem(new string[] { mySpread.Title.Text, mySpread.Summary.Text, mySpread.Updated.ToShortDateString() }));

  }

 }

The .NET code logic on Listview 1 SelectedIndexChanged event is following. We are reading the spreadsheet name selected by the user and reading the Spreadsheet properties (worksheet name, no. Of rows and columns into each worksheet) and adding it to ListView 2.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  {
   spreadsheetName = "";
   listView2.Clear();
   listView2.Columns.Add("Worksheet Name");
   listView2.Columns.Add("No. of Columns");
   listView2.Columns.Add("No. of Rows");
   listView2.Columns.Add("Summary");
   ListView.SelectedListViewItemCollection SpreadsheetList = listView1.SelectedItems;

   foreach (ListViewItem item in SpreadsheetList)
   {
    spreadsheetName = item.SubItems[0].Text.ToString();

   }

   if (spreadsheetName.Length > 0)
   {
    SpreadsheetsService GoogleExcelService;
    GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
    GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

    SpreadsheetQuery query = new SpreadsheetQuery();
    SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

    foreach (SpreadsheetEntry mySpread in myFeed.Entries)
    {
     if (mySpread.Title.Text == spreadsheetName)
     {
      WorksheetFeed wfeed = mySpread.Worksheets;
      foreach (WorksheetEntry wsheet in wfeed.Entries)
      {
       listView2.Items.Add(new ListViewItem(new string[] { wsheet.Title.Text, wsheet.Cols.ToString(), wsheet.Rows.ToString(), wsheet.Summary.Text }));

      }
     }
    }
   }
   }

We have following .NET code interface on Listview2 SelectedIndexChanged event which simply stores the name of the worksheet selected in the list view 2 control.

private void listView2_SelectedIndexChanged(object sender, EventArgs e)
{
  wrkSheetName = "";
  ListView.SelectedListViewItemCollection WorkSheetList = listView2.SelectedItems;

  foreach (ListViewItem item in WorkSheetList)
  {
  wrkSheetName = item.SubItems[0].Text.ToString();
  }
}

The final piece of code we have on button "Delete Worksheet" is following. Here, we are navigating through each spreadsheet and creating a worksheet feed which returns individual worksheet into its each feed. We are comparing each worksheet name and if it matches to the selected worksheet in Lisview 2, we are simply deleting this. In this code block we are deleting worksheet EmpSalaryDetails.

private void button5_Click(object sender, EventArgs e)
 {
  if (wrkSheetName.Length > 0)
  {
  SpreadsheetsService GoogleExcelService;
  GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
  GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

  SpreadsheetQuery query = new SpreadsheetQuery();
  SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

  foreach (SpreadsheetEntry mySpread in myFeed.Entries)
  {
   if (mySpread.Title.Text == spreadsheetName)
   {
   WorksheetFeed wfeed = mySpread.Worksheets;
   foreach (WorksheetEntry wsheet in wfeed.Entries)
   {
    if (wsheet.Title.Text == wrkSheetName)
    {
    wsheet.Delete();
    }
   }
   }

  }
  System.Windows.Forms.MessageBox.Show("Worksheet" + wrkSheetName + "deleted Successfully");

  }
 }

After setting up the code logic, I connected to the internet and clicked on button “Pull SpreadSheet Lists”, which returns the entire spreadsheet lists available into my Google Doc account and added it to ListView 1. Then, I selected Spreadsheet EmpDetails and the code logic of ListView1 SelectedIndexChanged runs and return all the worksheet available into SpreadSheet EmpDetails. I selected the worksheet EmpSalaryDetails and clicked on button Delete Worksheet . The code runs and deleted the worksheet EmpSalaryDetals.

I login into my Google Doc account and open the spreadsheet EmpDetails to verify the deletion. As expected, the worksheet EmpSalaryDetails was successfully deleted.

So, our objective to learn deletion of worksheet has been achived.

Thanks for reading this post.

If you want to explore with the Source code, you can find the source code of this .NET program into Download Zone

SpreadSheet API with .NET:
How to modify an existing Google Spreadsheet Worksheet?

This post is next in string of our learning to Google Spreadsheet API. We have learnt quite a few things about Google Spreadsheet API. We learn how to communicate with Google Spreadsheets located into Google Doc. We learn to create new worksheet, pull spreadsheet list, adding rows and columns into Google worksheet etc.

In this post we will learn how we can modify an existing Google Spreadsheet worksheet. We will learn how we can rename the worksheet; add rows and column into it. To proceed with here is the screen shot of the EmpDetails spreadsheet we uploaded in our first post. The default name is Sheet1 and it has 4 columns and five rows into it.

We have following .NET interface which we are using in this series of learning Google Spreadsheet API. We have two buttons and one list view control on form. The "Pull Spreadsheet List" button has .NET code snippet which interacts with Google Doc by using Spreadsheet API and pull Spreadsheet list. The Spreadsheet list is shown in the list view control. We have second button called "Update Worksheet" on the .NET GUI. This button contains the .NET code logic which will apply the modification on Google Spreadsheet we have selected from the list.

To use Google Spreadsheet API, we have to add reference of Google Data API in our project and have to use following namespaces in the form.

using Google.GData;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

We are using following form level variable in the code. This variable will store the Google Spreadsheet name.

public partial class Form1 : Form
    {
        string spreadsheetName;

The .NET code snippet on button "Pull SpreadSheet List" is following:

private void button1_Click(object sender, EventArgs e)
  {
   listView1.Columns.Add("SpreadSheet Name");
   listView1.Columns.Add("Summary");
   listView1.Columns.Add("Published Date");
   listView1.FullRowSelect = true;

   SpreadsheetsService GoogleExcelService;
   GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
   GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

   SpreadsheetQuery query = new SpreadsheetQuery();
   SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

   foreach (SpreadsheetEntry mySpread in myFeed.Entries)
   {
    listView1.Items.Add(new ListViewItem(new string[] { mySpread.Title.Text, mySpread.Summary.Text, mySpread.Updated.ToShortDateString() }));

   }

  }

The list view control displays the spreadsheet list, after selecting an individual spreadsheet from the list user can perform the modifications. We have following code snippet which stores the name of the spreadsheet we select from the list.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
 spreadsheetName = "";
 ListView.SelectedListViewItemCollection SpreadsheetList = listView1.SelectedItems;

 foreach (ListViewItem item in SpreadsheetList)
 {
 spreadsheetName = item.SubItems[0].Text.ToString();

 }
}

The code logic on button "Update Worksheet" is following. We are renaming the worksheet from Sheet1 to EmpDetails. We have modified the number of rows to 15 and columns to 8.

private void button3_Click(object sender, EventArgs e)
{
  if (spreadsheetName.Length > 0)
  {
  SpreadsheetsService GoogleExcelService;
  GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
  GoogleExcelService.setUserCredentials("gmailuserid", "Gmailpassword");

  SpreadsheetQuery query = new SpreadsheetQuery();
  SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

  foreach (SpreadsheetEntry mySpread in myFeed.Entries)
  {
   if (mySpread.Title.Text == spreadsheetName)
   {
   WorksheetFeed wfeed = mySpread.Worksheets;
   foreach (WorksheetEntry wsheet in wfeed.Entries)
   {
    //Add new worksheet and some blank rows and columns
    wsheet.Title.Text = "EmpDetails";
    wsheet.Rows = 15;
    wsheet.Cols = 7;
    wsheet.Update();
   }
   }

  }
  System.Windows.Forms.MessageBox.Show("Worksheet updated Successfully");
  }
}

After setting up the code, the very first thing we need to do is to connect with the internet and run the form/program. I clicked on button "Pull SpreadSheet List" and the code logic runs successfully and pulled the entire Spreadsheets name into the list.

I selected the EmpDetails spreadsheet and clicked on button "Update Worksheet";. The code runs successfully and throws the message that Worksheet has been updated.

I log into my Google Doc account and open the spreadsheet. The modifications have been made to the Spreadsheet. The worksheet name has been changed. Rows have been added till 15 and columns till 8

So we have learn how we can modify an existing worksheet using Spreadsheet API. Thansk for reading till this point.

Popular Posts

Real Time Web Analytics