Sunday, July 1, 2012

SpreadSheet API with .NET:
How to delete individual row from Google Spreadsheet?

The journey towards learning Google Spreadsheet API continues. In this post, we will learn how we can delete rows from Google Spreadsheets, which are now stored into Google Drive using Spreadsheet API. You might know this, Google has migrated the Google Docs into Google Drive. So, all our files which we used to store into Google Doc is now available into Google Drive. The first 5 GB space is free for us. Thanks! Google.

So coming back to our learning, we have following two spreadsheets available into Google Drive. We will try to delete data from EmpDetails spreadsheet.

We have following data available into EmpDetails spreadsheet. We will delete the very first row using Spreadsheet API.

We have following .NET GUI interface. We have two text boxes where we will enter our Gmail credentials; this is required as we have t identify ourself to Google Spreadsheet. We have a button called "Delete First Row". This button contains all the code logic to connect to Google Spredsheet, read the particual worksheet and delete the row data.

To use Spreadsheet API, the very first thing we have to do is to add Google API refreces into our program. We are using following namespaces into our .NET program.

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

The .NET code logic on button “Delete First Row” is following. The code is self explanatory with the comments inside it.

private void button2_Click(object sender, EventArgs e)
{
  SpreadsheetsService GoogleExcelService;
  GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
  //Pass Google credentials 
  GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);


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


  foreach (SpreadsheetEntry mySpread in myFeed.Entries)
  {
  //Process the code logic for EmpDetails Spreadsheet only
  if (mySpread.Title.Text == "EmpDetails")
  {
   WorksheetFeed wfeed = mySpread.Worksheets;
   foreach (WorksheetEntry wsheet in wfeed.Entries)
   {
   //Process the code logic for EmpDetails Worksheet only
   if (wsheet.Title.Text == "EmpDetails")
   {
    AtomLink atm = wsheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, AtomLink.ATOM_TYPE);

    ListQuery Lquery = new ListQuery(atm.HRef.ToString());
    ListFeed LFeed = GoogleExcelService.Query(Lquery);
    
    //Process the code logic for first row only
    ListEntry excelRow = (ListEntry)LFeed.Entries[0];
    excelRow.Delete(); 
    
    System.Windows.Forms.MessageBox.Show("Record Deleted Successfully");

    }
   }
  }
  }
 }

After setting up the code, I connected with the internet and run the program. I supplied my gmail credentials and click on button "Delete First Row". The code logic runs successfully and deleted the first row.

I went to my Google drive to check and open the EmpDetails spreadsheet. As per my expectations, the Google Spreadsheet API had deleted the first row.

So our learning to delete rows into Google Spreadsheet using Spreadsheet API is complete. Thanks for reading till this point.

If you want to explore with this source code, please visit Download Zone.

Popular Posts

Real Time Web Analytics