Saturday, July 21, 2012

SpreadSheet API with .NET:
How to add new row into Google SpreadSheet?

Recently I concluded Google SpreadSheet API series with .NET. The series had good learning on how to use Google Spreadsheet API with .NET. The response to the series was great. I received a lot of emails on this topic. One of the things that were constantly asked was how to add new data into Google Spreadsheet with .NET using Listfeed. This article is in line to provide an example for the same.

In this post we will learn how we can add new row into Google Spreadsheet from our .NET interface. For demonstration purpose, I have uploaded following Google Spreadsheet file into Google Drive. We have a spreadsheet with the name "ProductDetails".

This Spreadsheet has following data into it. We will be adding new row in this sheet from .NET interface.

To start with .NET interface, I have following .NET form. The form contains two text boxes where we will supply Google credentials. To interact with Google Spreadsheet we have to provide our Gmail credentials to identify ourself to Google, if we are using Client Login authentication mechanism.

We have four buttons and three Listview controls on the form. The idea is to when user enter their Google credentials and click on button1 i.e. “Pull Spreadsheet List”, the .NET code logic will interact with Google Drive and pull all the available Google Spreadsheet details into Listview1.

From ListView1 user will select the Spreadsheet where they want to add new row and click on button3 i.e. "Pull Worksheet List". The .NET code logic written on button3 will pull all the worksheets details under the selected Google Spreadsheet from ListView1. The worksheet details will be shown into ListView2.

From ListView2 user will select the worksheet where they want to add new row and click on button 4 i.e. "Display Worsheet Data". The .NET code logic written on button4 will pull all the data available on selected Google worksheets and show it under Listview3.

The "Add new Row" button contains the code logic to add new row into Google Spreadheet. In our example the code logic written on "Add new Row" will add new data into ProductDetails spreadsheet.

We are using following namespace and in this program.

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;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

The .NET code logic written on this form is following.

namespace SpreadSheet_API_New_Row
{
 public partial class Form3 : Form
 {
  SpreadsheetsService GoogleExcelService;
  public Form3()
  {
  InitializeComponent();
   
  GoogleExcelService = new SpreadsheetsService("Vikash-GData-Sample-App");
   
  }

//Code logic written on button “Add New Row”
  private void button2_Click(object sender, EventArgs e)
  {
  //Pass Google credentials 
  GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);
  
  ListEntry entry = new ListEntry();
//Add new row data with productname bottles and quantity 100.
  entry.Elements.Add(new ListEntry.Custom() { LocalName = "productname", Value = "Bottles" });
  entry.Elements.Add(new ListEntry.Custom() { LocalName = "quantity", Value = "100" });   

  AtomEntry retEntry = GoogleExcelService.Insert(new Uri(this.listView2.SelectedItems[0].SubItems[2].Text), entry);
  System.Windows.Forms.MessageBox.Show("Record added Successfully");

   
  }

 
//Code logic written on button “Pull Spreadsheet List”

  
  private void button1_Click_1(object sender, EventArgs e)
  {
  //Pass Google credentials 
  GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);  

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

  this.listView1.Items.Clear();
  listView1.Columns.Add("SpreadSheetName");
  listView1.Columns.Add("SpreadSheetURL");
  listView1.FullRowSelect = true;
  AtomEntryCollection entries = myFeed.Entries;
  for (int i = 0; i < entries.Count; i++)
  {
   // Get the worksheets feed URI
   AtomLink worksheetsLink = entries[i].Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel,
             AtomLink.ATOM_TYPE);

   ListViewItem item = new ListViewItem(new string[2] { entries[i].Title.Text, worksheetsLink.HRef.Content });

   this.listView1.Items.Add(item);
  }
  }

  
//Code logic written on button “Pull Worksheet List”

  private void button3_Click_1(object sender, EventArgs e)
  {
  //Pass Google credentials 
  GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);
  WorksheetQuery query = new WorksheetQuery(listView1.SelectedItems[0].SubItems[1].Text);
  WorksheetFeed wsFeed = GoogleExcelService.Query(query);
  AtomEntryCollection entries = wsFeed.Entries;

  this.listView2.Items.Clear();
  listView2.Columns.Add("Worksheet1");
  listView2.Columns.Add("CellFeedURL");
  listView2.Columns.Add("ListFeedURL");
  listView2.FullRowSelect = true;

  for (int i = 0; i < entries.Count; i++)
  {
   // cells feed URI
   AtomLink cellsLink = entries[i].Links.FindService(GDataSpreadsheetsNameTable.CellRel, AtomLink.ATOM_TYPE);

   // list feed URI
   AtomLink listLink = entries[i].Links.FindService(GDataSpreadsheetsNameTable.ListRel, AtomLink.ATOM_TYPE);

   ListViewItem item = new ListViewItem(new string[3] { entries[i].Title.Text, cellsLink.HRef.Content, listLink.HRef.Content });

   this.listView2.Items.Add(item);
  }
  }


//Code logic written on button “Display Worksheet Data”

  private void button4_Click(object sender, EventArgs e)
  {
  //Pass Google credentials 
  GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);
  ListQuery query = new ListQuery(listView2.SelectedItems[0].SubItems[2].Text);
  ListFeed LFeed = GoogleExcelService.Query(query);
  this.listView3.Clear();
  listView3.Columns.Add("SR#");
  listView3.Columns.Add("ProductName");
  listView3.Columns.Add("Quantity");
  AtomEntryCollection entries = LFeed.Entries;

  for (int i = 0; i < entries.Count; i++)
  {
   ListEntry entry = entries[i] as ListEntry;

   ListViewItem item = new ListViewItem();
   item.Text = (i + 1).ToString();

   if (entry != null)
   {
    ListEntry.CustomElementCollection elements = entry.Elements;
    for (int j = 0; j < elements.Count; j++)
    {
    item.SubItems.Add(elements[j].Value);
    }
    listView3.Items.Add(item);
   }
  }
  }
 }
}

After setting up the code logic, I connected with the internet and run the program. The results were in line with the expectation. A new row with the productname “bottle” and quantity 100 was added into productDetails spreadsheet.

I open the ProductDetails spreadsheet by login into google drive and new row data was into it.

So our objective to add new row into Google Spreadsheet has been achived.

The one important thing I want to mention here which I learn during this program is following:

Google expects that the column headings in Google Spreadsheets are all in lower case and without spaces into it. If you have any issue with the column headings like this, you might face errors while inserting your new data through .NET inferface.

Thanks for reading this post.

If you are want to explore with this Source code, you can download this sample program from Downlaod Zone

Friday, July 20, 2012

Ms-Excel: The ROW() and INDEX() function story

This post is inspired by a question that one of the blog reader asked me recently. The question was related to Ms-Excel. Here is the question

I have 3 spreadsheets, Sheet1 and Sheet2. I need to copy the info from A18 in Sheet1 to A1 in Sheet2 (=Sheet1!A18) I know that is the correct formula. But I now need to copy the subsequent 7 blocks…A18, A25, A32, A39… ect for over 4000 values (meaning I can’t do it by hand). I m sure the formula looks something like this =Sheet1!A18+7… but that does not work. Any help would be greatly appreciated

I have used Ms-Excel and have done some VBA coding as well. So the very first thing that strike to me is that we can write few line of code in VBA and we can accomplish it. On a second thought, I tried to search is there a way in excel that we can achieve this.

The finding was quite interesting and it educated me with two important functions of Ms-Excel.

1. ROW()
2. INDEX()

Let us first see what the problem was and how we resolved it. For the demonstration purpose I have created an excel file with following data into it.

The user has two sheets into Ms-Excel. Sheet1 contains data that user want to copy to Sheet 2. The trick here is that user wants to start copying data from A18 cell in Sheet1. Then he/she wants to copy next 7 subsequent blocks of data i.e. A25, A32, A39, A46, A52, A59, and A66 and so on. The sheet1 has 4000 records into it.

Do you think Ms-Excel has any function or formula to copy data in this fashion? If your answer is No – (just like me before writing this post) – please read on.

Ms-Excel has two functions that are kind of magical to solve this issue.

ROW([reference]): The ROW() function returns the row number. So if you write formula "= ROW()" into A5 cell of Sheet1, this will return 5 i.e. 5th Row. The reference parameter is optional. If you pass a reference, it will always take the starting row number in the reference. For example, if we write =ROW(E10:L20), this will return 10 i.e. 10th row. Take some more example, the function =ROW(K3:S27) will return row 3 i.e. 3rd row.

INDEX(array, row_num, col_num): This INDEX() function returns specific value or the reference to a value from the range. For example, if you want to return specific value 36 from following data, you will pass parameters to INDEX function like =INDEX(A2:A6,3,1), where A2:A6 is array or range, 3 is row number and 1 is column number.

So to solve the user issue the ROW() and INDEX() together founded a solution. The solution is user can apply function =INDEX(Sheet1!$A$2:$A$50,(10 + ROW()*7),1) into Sheet2.

What we are doing with this function is we are taking data range A2 to A50 and putting

10+ ROW()*7 = 10+1*7=17th row data in first cell of Sheet2
10+ ROW()*7 = 10+2*7=24th row data in second cell of Sheet2
10+ ROW()*7 = 10+3*7=31st row data in third cell of Sheet2.

The same function can be written as =INDEX (Sheet1!$A$18:$A$50,ROW()*7-6,1)

Is not this better then writing VBA code?

PS: The solution for this question was given by Microsoft Excel Users group users actively working to solve issues like these. A big thanks goes to them. You can read the entire conversations here

Tuesday, July 17, 2012

How to take back-up and safeguard your blog?

Recently I received a comment on one of my post "How to pull photo list from Picasa web Album with Google Data API?". The comment was from a blog reader, who has lost several weeks of hard work due to unauthorized access to his blog.

Well, I do not know about wordpress because I have not used it. This site www.SighVikash.in is hosted on Google blogger. Google Blogger provides tools which you can use to safeguard your Blog posts, comments and articles. You can take full backup of your blog posts and save it your hard disk. In future, if you need to restore your blog posts, you can do it with Google Blogger. Here is how you can take backup of your Blog:

Step 1: You need to login to your Blogger accounts.

Step 2: You need to go to Blogger dashboard and click on Settings -> Other. Once you click on Other, you will find settings related to Other sections in the right frame of your web page. Under that, there are Blog tools.

There are three tools under Blog Tools

  • Import Blog
  • Export Blog
  • Delete Blog

Export blog is to take backup of your blog. Google blogger provides backup of your Blog in XML file. When you click on Export blog link, following winodw will appear and you can choose Download Blog button.

Once you click on Download Blog button, Google blogger download your blog backup in an XML file. You can save this XML file into your hard disk. In future if you face any issue like the loss of post, you can restore this xml file. Remember you need to take backups periodically so that you have updated backup file available that you can use it in future.

Whenever you want to restore your Blog, all you have to do is the select Import blog from the Blog Tools settings. You have to provide the XML File that you backed up and Google will restore the blog of yours.

I hope it helps to people to understand the importance of taking Blog backup regularly. Thanks for reading this post.

Monday, July 16, 2012

Project Quality Management
What is Quality?

“Quality” is one thing that everyone desire to have in their personal and professional life. It is one single thing that you constantly look for whether you are shopping, dating, working or just relaxing. A popular phrase is “What is common between a Lion and a Chef?” – The answer is Quality. Lion or chef will never compromise with their food quality. It does not matter how hungry the Lion is, he will never eat grass or vegetarian food. The same apply to the Chef.

So the point is Quality is looked upon in every activity that we do. But do we know, what is the Quality that we often hear in the meetings? This article is for the beginners who want to understand what is quality, where it is coming from, how to measure this etc.

It does not matter whether you are working on a product based or project based module, you always hear about project leads and project managers talking about quality?

What is Quality?

Quality is the degree or parameter to which you fulfill the customer requirement. If customer wants to paint the building in deep aqua color and you are delivering it, you are fulfilling the customer need and giving him quality. It may look odd to the rest of the world, but be assured you are producing quality for your customer.

In the world, Quality is always and always looked from Customer prospective. If customer is happy with the project outcome, you have given him quality work.

One basic thing that you should always remember is:

Quality is always and always measurable. As a project team member, the quality you are delivering can be measured and compared every time or any time.

What is the source of Quality?

Whenever a product or project is initiated the customers always express their requirement in a very simple and generic term. Take some example of requirement;

  • A car manufacturer wants to develop a car and wants its cabin to be cool in all temperatures.
  • A customer wants his website to load fast on all devices.
  • The system should be able to handle all transactions generated.

These project or product requirements examples are simple or say subjective in nature. Quality requirements are not given by customer directly; it is project manager or project team that has to pick the quality requirement from the given subjective customer requirement. The above examples are example of subjective requirement. So as a project manager or project team these are your Quality requirement for the project or product you are working upon.

Do you think, for the requirements like above, you will be able to provide quality to the customer? — My answer is NO. Why?

The reason is if you cannot measure quality, you can claim that you have produce quality. For example, the website load time will be vary with the different internet connection speed. So you have to think following scenario in this situation.

  • What should be the website load time on slow internet connection (100KBps speed)?
  • What should be the website load time on internet connection (500KBps speed)?
  • What should be the website load time on internet connection (2MBps speed)

A Project manager or project team has to discuss the above scenario with client. The website home page will take different time to load with different internet connection speed. You have to provide different time range for each of the internet connection speed. For example, you can set up 20 to 50 seconds load time for internet connection speed of 100KBps. Similarly, you can set up 10 to 40 seconds load time for internet connection speed of 1MBps. When you are doing this with your project and customer, what you are doing is – you are setting up Quality Goal for the Quality requirement.

What we do during the Quality goal setup is we are converting the Subjective Quality requirement into Objective Quality requirement. For each of the quality requirement you set up quality goals. A single Quality goal may be applicable to multiple Quality requirements. Each of the quality goals has a name, description and a client acceptable range.

OK, so you have picked up the quality requirements out of project requirement and setup quality goals for each of the quality requirement. What is next?

The next thing is how you are going to achieve the Quality Goals that you have setup for the project or product. The answer is Quality Standards. Quality standards are set of rules and guidelines that help the project team and project manager to achieve quality goals. Do you peer review the design or code in your project, if yes that is an example of Quality standards that you are applying in your project.

As a project team or project manager you have to specify what quality standards you will be using in your project. The example of Quality standards includes coding standards, checklists, templates, procedures, guidelines etc.

So the story for Project Quality Management starts with collecting Quality requirement, setting up Quality Goals and Quality standards for the project. What is next? I will cover this in next topic. Till then thanks for reading this post and please let me know your valuable suggestions, feedback on this.

Saturday, July 14, 2012

EPF SMS: How to read and interpret it correctly?

Employee Provident Fund (EPF) is an integral part of earning for all working professional in India. All government and private employees can save a small fraction of their salary through EPF, which is automatically debited from their salary and credited to their EPF accounts by their employer. The Employee Provident fund offices located across India, provide online facility to check your Provident fund balance, your PF withdraw or query status on PF claim etc.

This post is in line to one of my previous Post Know your EPF balance via SMS. In my last post, I explained how you can check your EPF balance online. When you check your EPF balance online, you have to provide your mobile number.EPF system sent an SMS to this mobile number. This SMS contains message in following format:

EPF Balance in A/C No. [State Code OR Area Code]>\[City Code]\[Company Code OR Establishment Code]\[Company/Establishment Extension Number]\[Your PF A/CNo.] is EE Amt: Rs. [0000], ER Amt:Rs [0000] as on [DATE] (Accounts updated upto [DATE])-EPFO.

Example:

EPF Balance in A/C No. GN\GGN\0012345\000\0009999 is EE Amt: Rs. 67009, ER Amt:Rs 47000 as on 26-06-12 (Accounts updated upto 31-03-2011)-EPFO.

This SMS message has a lot of information, that one individual need to understand.

A/C No: This is your EPF account number. You EPF account number have your area code, company or establishment code and your account number.

EE Amt: Employee Contribution i.e. your total contribution in the EPF account. The sum total of PF amount deducted monthly from your salary.

ER Amt: Employer Contribution i.e your company contribution. The sum total of PF amount monthly contributed into your EPF account by your employer.

As on [Date]: This date tell you that your monthly contribution till this date has been updated in your EPF account. From the example above, it tells that your monthly contribution till 26th July 2012 has been added into your EPF account

Accounts updated upto [Date]: This date tell you that the interest till this date has been added into your EPF account. From the example above, it tells that interest till 31st March 2011 has been added into your EPF account.

The aim for this article is provide your information so that you can read and interpret it correctly. This is related to your finance, so it is important that we understand it rightly. Thanks for reading this post.

Related Articles:

How to download your EPF e-Passbook?
Know Your EPF Balance via SMS

Sunday, July 8, 2012

What is Big Data?

"Big Data" is the new buzzword doing the round in the industry. As the name suggests, it is about data and, it is about large and hefty data. But how much volume of data is considered as “Big Data”? Does 10 GB, 50 GB, 5 TB or 50 TB sizes of data is enough to qualify as Big Data? The answer is No. Surprised?

I too was surprised when I came to know about this fact. To qualify for Big Data, as a business you must be dealing with 100 TB or more data.

There are two types of data in this world.

  • Structured Data
  • Unstructured Data

Structured data are those which are organized in a controlled way. This data is identifiable, easily accessible. The data is organized in columns and rows. The Databases, ERP system that we deal with in daily life are example of repositories which stores the structured data. In a nutshell, the data which is there in the databases are structured data.

Unstructured Data are those which are not easily identifiable. The unstructured data do not have a data model to apply. This data can be in textual or non-textual format. This data cannot be stored in the relational databases because of it nature – i.e this data has ambiguities, complexity and irregularities.

Why I am telling you this? The point I want to make here is that data is growing at a much higher rate than we imagine. Do you know the entire world is creating 8 TB data every day? The 80 percent of this data is unstructured. This unstructured data is coming from a lot of sources – social media (facebook, twitter, google plus, youtube etc.), videos, audios, web logs, machine generated logs, sensors and many more. The unstructured data is coming in the form of messages, emails, presentations, PDF, links, videos, audio, visuals etc. Today, we have more devices (Smartphones, PDAs, iPAD, iPhone, Tablets etc) which are generating data every day. Most of this data is unstructured but contains a lot of information.

Today more than 2 billion people across the world is using internet and contributing to the growth of unstructured data. As per internet world statistics at the end of year 2011, total 2,267,233,742 people are using internet. The point I want to make here is that data is growing and it is growing every second. A big proportion of this data is unused. This data contains useful information. This data has many business insights into it.

Today, the available tools of Data management are not good fit to take this wave of ever growing data heads on and gives business the ability of to search, share and analyze this much data for their business needs.

Big Data is not a single tool or platform. Big Data is a strategy to store all these data (structured + unstructured) and ability to generate useful information, meaningful pattern out of it by analyzing this data. This information comes handy to corporate in making good, smart and profit driven business decisions.

Who has the "Big Data"?

Looking at the size of Big Data qualification, we can easily understand that not every company, business or organization has big data. The SME (Small Medium Enterprise) do not have that much data. So who has this much data available to care for Big Data.

Mainly Telecom, Banks, Insurance, Mobile and some FMCG companies like Wal-Mart has data which can be classified as Big Data. For example, Wal-Mart has 2.5 petabytes of data and this is growing every day. Some of the big guns in these industries are using big data from years.

What are the benefits of Big Data?

Big Data trend is evolving. Big Data will provide smart storage of large and hefty data; its quick and predictive analysis will provide insight to corporate what is going to happen in future. This will improve the company efficiency to come out with smart and great business ideas in future.

The solution to apply Big Data strategy is not cheap – it involves cost. In a very simple term, with Big Data you are dealing with huge wave of data which is generating new wave of data every minute and second. Today, the availability of Cloud and data centers have made the storage of large and hefty data affordable but the real challenge lies in analyzing this data and come up with useful patterns which can add value to the business.

Big Data tools and technologies are still in evaluating stage. Although IT big guys like IBM and Oracle are ready to offer Big Data suite of products from their armors. To apply Big Data as a business you needs a very strong understanding of your business, the outcome you want to achieve with Big Data and you should be ready for cost.

The Big Data wave is so complex and hefty that you can easy lost in this wave. So before you raise your hand to Big Data, think smartly, thoughtfully why do you need this?

We will analyze the Big Data tools and technologies in future post. Thanks for reading till this point

Please visit the Big Data page to read all articles on Big Data

Thursday, July 5, 2012

SSIS: How to load data into SQL table from Google Spreadsheets?

SSIS (SQL Server Integration Services) is well known for extracting data from a variety of sources. It can extract data from online sources as well. Google Spreadsheet is one of the online source which stores data online. In this post, we will learn to extract data from Google Spreadsheet using our favourite tool – SSIS.

Pre-requiste:

Before we talk about SSIS package, there are few concepts that we need understand.

#01. To use Google Spreadsheet data you must have a Google account. With a Google account, you can access Google Drive – the place where Gooogle stores the Google Spreadsheets for you.

#02. To connet and interact with Google Spreadsheet data you need, Google Data API SDK. The Google Data API are available in Java, .NET, Python and PHP. The Google Data API are interface between Google online services (ex. Google Spreadsheet, Picasa, Googel Analytics services etc.) and their calling program (client program written in Java, php, python and .NET ). SSIS being a Microsot products supoorts .NET, so you need to download .NET version of Google Data API. This is available at http://code.google.com/p/google-gdata/downloads/list

#03. Once you have downloaded and installed Google Data API SDK, you need to add Google API DLLs into Global Assembly Cache (GAC). To do this, you need to run Visual Studio Command Prompt as adminstrator. You can use gacutil, a commnd line utility to add the DLLS into GAC. Examples are following in the screen shot.

Now after completing the three pre-requisite, you are all set to start with Google Spreadsheet and SSIS. For the demonstration purpose, I have following Google Spreadsheet available into my Google Drive

I have createad following table in my local SQL Database.?

CREATE TABLE [dbo].[tblProducts](
    [ProductName] [varchar](50) NULL,
    [Quantity] [numeric](18, 0) NULL
) ON [PRIMARY]

This table will store the product details that we extract from Google Spreadsheet “productDetails”.

The next thing is to launch the SSIS design Tool — Microsoft Business Intelligence or SQL Server Data Tool (SSDT), whatever you are using at present.In my case I used MSBI. I have created a SSIS package with an OLEDB Connection Manager. The OLEDB Connection Manager is pointing to my local SQL database, which is hosting table "tblProducts". On the Control Flow Tab, I have a Data Flow task. On Data Flow tab, I have one script component and one OLE DB Destination control.

I have defined following two data flow level variables – GmailUserID and GmailPassword. To connect to Google Spreadsheets which are located in Google Drive, we need to identify our self with our Google account credentials.

The script component we are using in data flow tab is a Source Type. We are using two read only variables in Script component.

We have added two string type Output columns "strProduct" and "intQuantity" into Script component.

Under Script component the very first thing, we need to do is add references of Google Data API DLLs. These DLLs are extracted and stored in our system when we install the Google Data API SDK. On my Windows 7 Home edition system, these DLLs were stored under C:\Program Files (x86)\Google\Google Data API SDK\Redist. I added following DLLs into our script component.

Google.GData.Client
Google.GData.Extensions
Google.GData.Spreadsheets

The Script component contains following code logic to connect and read data from Google Spreadsheet.

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Google.GData;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

  public override void PreExecute()
  {
    base.PreExecute();
  }

  public override void PostExecute()
  {
    base.PostExecute();
   
  }

  public override void CreateNewOutputRows()
  {
//Create new Spreadsheet service

    SpreadsheetsService GoogleExcelService;
    GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
//Connect with Google Spreadsheet services using Google account credentials
    GoogleExcelService.setUserCredentials(Variables.GmailUserID, Variables.GmailPassword);

//Create a new Spreadsheet Query
    SpreadsheetQuery query = new SpreadsheetQuery();
//Pass the SpreadsheetQuery to Spreadsheet Feed
    SpreadsheetFeed myFeed = GoogleExcelService.Query(query);

//The Spreadsheet Feed connects with Googel Spreadsheet services and returns feed. Each Feed represents one Google Spreadsheet.

    foreach (SpreadsheetEntry mySpread in myFeed.Entries)
    {
//Run the code logic if the Spreadsheet name is ProductDetails
      if (mySpread.Title.Text == "ProductDetails")
      {
        
        WorksheetFeed wfeed = mySpread.Worksheets;
        foreach (WorksheetEntry wsheet in wfeed.Entries)
        {
//Run the code logic if worksheet name is Sheet1
          if (wsheet.Title.Text == "Sheet1")
          {
            
            AtomLink atm = wsheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

            ListQuery Lquery = new ListQuery(atm.HRef.ToString());

            ListFeed LFeed = GoogleExcelService.Query(Lquery);

//Each ListFeed represents a row so naviage through each listfeed and add them into Output Buffer.
            foreach (ListEntry LmySpread in LFeed.Entries)
            {
              Output0Buffer.AddRow();
              Output0Buffer.strProduct = LmySpread.Elements[0].Value;
              Output0Buffer.intQuantity = LmySpread.Elements[1].Value;              

              
            }
            
          }
        }
      }
    }
   
  }

}

After setting up the SSIS design components, I connected with the internet and then run the SSIS package. Since we are pulling data from an online source, we need to be connected with internet. The results were expected. The SSIS package executed successfully and pulls all the data from Google Spreadsheet “productDetails” and added them to SQL table "tblProducts".

I quickly checked the SQL table and records were in the table.

So this is how we can extract data from an online source like Google Spreadsheet. Thansk for reading this post.

If you are interested to have a hands-on on this package, you can download this SSIS pacakge from Download Zone.

Related Article:

Tuesday, July 3, 2012

Google Spreadsheet API Series in .NET

This is the last post in the series of our learning Google SpreadSheet API. In this series, we learn to use Spreadsheet API with our .NET program. Google Spreadsheet is great tool to use. There are lot of debate in the online world what is best – Google Spreadsheet or Microsoft Excel Spreadsheet. Well to me, honestly I have used Microsoft Excel Spreadsheet more than Google Spreadsheet. In my opinion both are good.

If you have a limited data and want availability of your data anytime and anywhere, Google Spreadsheet is your choice. In this series of our learning, we learn to interact with our Spreadsheet files stored into Google Drive using Spreadsheet API without login into our Google Drive. We learn to quite a few things about Google Spreadsheet API.

I will leave you with the recap of posts; we created in this small journey of learning Google Spreadsheet API.

How to get started with Google Spreadsheet API?
How to read conditional data from Google Spreadsheet? How to delete individual row from Google Spreadsheet?
How to read data from Private Google SpreadSheet?
How to read data from Published Google SpreadSheet?
How to delete an existing worksheet from Google Spreadsheet?
How to modify an existing Google Spreadsheet Worksheet?
How to create new worksheet with Google Spreadsheet?
How to read Google SpreadSheet properties?
How to add blank rows and columns into Google SpreadSheet?
How to pull spreadsheet list from Google Doc?

Thanks for reading all these posts.

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

Welcome to our ongoing series of learning Google Spreadsheet API. Till this point, we have learnt to interact with Google spreadsheets using Spreadsheet API with our .NET client program. In this article we will learn to read conditional data from Google Spreadsheet which are located into Google Drive. We will read data which fulfill our criteria.

We do not want to read the entire data. We want to return only those data which fulfill the criteria we send to Google Spreadsheet. For example, if we have following Google Spreadsheet "ProductDetails", we want to read only those products where quantity is greater than 50.

We have following .NET GUI interface. We hav e two text boxes where we will supply our Gmail credentials. To connect to Google Spreadsheets, we need to indentify ourself. In this form we have a data grid which will show the data retrun from Spreadsheet API. We have a button “Read Data” which contains the .NET code logic to connect to Google Spredshee and pull conditional data from respective Spreadsheet.

To use Spreadsheet API, the very first thing a .NET developer has to do is to add Google Spreadsheet API refrences into his program. We are using following namspace 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 “Read Data” is following. We are defining a new Spreadsheet service and supplying our gmail credentials to connect with the Google Spreadsheet. We are creating a Spreadsheet query and passing it to Spreadsheet Feed. The Google Spreadsheet Feed returns feeds. Each feed represents a Google Spreadsheet. Later we are creating worksheets feed. Each worksheet feed represents a worksheet. We are using ListFeed to return all rows inside the worksheet. To apply conditional logic we need to set ListFeed.Spreadsheet property. We have to define what are our conditions to read data from LisFeed. In this example we want to read data where quantity is greater than 50. So we have specified the criteria into the code logic.

private void button2_Click(object sender, EventArgs e)
{
 
 SpreadsheetsService GoogleExcelService;
 GoogleExcelService = new SpreadsheetsService("Spreadsheet-Vikash-Test-App");
//Connect with Google Spreadsheet services using gmail credentials
 GoogleExcelService.setUserCredentials(textBox1.Text, textBox2.Text);
 
 
 SpreadsheetQuery query = new SpreadsheetQuery();
 SpreadsheetFeed myFeed = GoogleExcelService.Query(query);


 foreach (SpreadsheetEntry mySpread in myFeed.Entries)
 {
//Run the code logic if the Spreadsheet name is ProductDetails
 if (mySpread.Title.Text == "ProductDetails")
 {
 System.Windows.Forms.MessageBox.Show("inside");
 WorksheetFeed wfeed = mySpread.Worksheets;
 foreach (WorksheetEntry wsheet in wfeed.Entries)
 {
//Run the code logic if worksheet name is Sheet1
 if (wsheet.Title.Text == "Sheet1")
 {
 System.Windows.Forms.MessageBox.Show("Inside");
 AtomLink atm = wsheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

 ListQuery Lquery = new ListQuery(atm.HRef.ToString());
//Apply conditional logic to read data where quantity> 50
 Lquery.SpreadsheetQuery="quantity > 50";
 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 part, it is time to execute it. I connected with the internet and run the program. I supplied my Gmail credentials into the first two text boxes and click on the button “Read Data”. The code logic runs successfully and fetched the data into Grid view. Now if you compare the data of spreadsheet and data grid, you will find it has returns only those products where the quantity is greater than 50.

After setting up the code part, it is time to execute it. I connected with the internet and run the program. I supplied my Gmail credentials into the first two text boxes and click on the button “Read Data”. The code logic runs successfully and fetched the data into Grid view. Now if you compare the data of spreadsheet and data grid, you will find it has returns only those products where the quantity is greater than 50.

So our objective for this post is achived. Thanks for reading till this point.

If you wan to explore yourself with this source code, please visit the Download Zone.

Popular Posts

Real Time Web Analytics