Writing data to a google spreadsheet

Today a client wanted all form data to be saved to a spreadsheet. I knew it’s possible to do it to excel but I came across an easier solution. Google spreadsheets, I realized, is very easy in this regard. So easy I even integrated it along side wordpress. There are a couple of requirements however. First you need to pick up a version of the Zend PHP framework with Gdata in it’s library. You can stop there but I found a helper class that makes it that much easy.

Once you have both of those you have a bit of configuration to do.

set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/wp-content/plugins/zend/library");
include("$_SERVER[DOCUMENT_ROOT]/wp-content/plugins/zend/demos/Zend/Gdata/Google_Spreadsheet.php");

The first line is including the Zend Library and the second is loading the helper class. Once those two lines are added to you script you can begin connection.

$u = "your email";
$p = "your password";
$spreadsheet = new Google_Spreadsheet($u,$p);

Just like that you are connected. So for my client, who wanted to add a new row to the spreadsheet for everytime a registration form was finished, I named the columns in the spreadsheet the exact name as the input tags in the form. Then ran the following code.

$ss_array = array();
foreach($_POST as $key=>$value)
{
	$ss_array[$key] = $value;
}
$spreadsheet->addRow($ss_array);
unset($ss_array);

Pretty cool! If you look into the helper class and even into the more advanced Zend files you will see there is a bunch of ways to manipulate. You can get rows from a spreadsheet even with search criteria. If you mastered the functions you could even use it as a poor man’s, err.. homeless man’s database if you wanted to. Even if you are using a database, this is a nice option to have next to it so a client could more easily see data. More clients will understand spreadsheets than anything sql.

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: