
The general idea is to read a Google Spreadsheet through PHP and save user submitted form data via the Google Documents List Data API. By doing this, you can quickly view all the submissions at a glance and you are also able to export CSV files of the data. Using Google Docs gives you and your clients a quick and easy interface to interact with form data.
I’ve written a small PHP helper class to assist with the whole process (PHP5). You are going to need the following:
- A working copy of the Zend GData Client Library (1.0.3+)
- A copy of the PHP Google Spreadsheet Helper Class
- A Google account, in order to be able to create a spreadsheet document using Google Docs.
First you should login to Google Docs using your existing Google account. Once logged in you will want to create a new spreadsheet document, you will be immediately taken to a spreadsheet interface. Lets start off by creating some column field names on row #1:
- name
- comments
Make sure to save your spreadsheet and give it a name, you will need to use the spreadsheet name in the code below.
The following is a basic example of using the Google_Spreadsheet PHP helper class.
<?php
// Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
$u = "username@gmail.com";
$p = "password";
$ss = new Google_Spreadsheet($u,$p);
$ss->useSpreadsheet("My Spreadsheet");
// if not setting worksheet, "Sheet1" is assumed
// $ss->useWorksheet("worksheetName");
$row = array
(
"name" => "John Doe"
, "email" => "john@example.com"
, "comments" => "Hello world"
);
if ($ss->addRow($row)) echo "Form data successfully stored using Google Spreadsheet";
else echo "Error, unable to store spreadsheet data";
?>
In this next example, I’ve added an additional column to the spreadsheet (id). The id column is used as a unique identifier in order to be able to update the row at a later point; you can use any column as the identifier, such as the email column.
<?php
// Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
$u = "username@gmail.com";
$p = "password";
$ss = new Google_Spreadsheet($u,$p);
$ss->useSpreadsheet("My Spreadsheet");
$ss->useWorksheet("wks2");
// important:
// adding a leading alpha char prevents errors, there are issues
// when trying to lookup an identifier in a column where the
// value starts with both alpha and numeric characters, using a
// leading alpha character causes the column and its values to be
// seen as a strictly a strings/text
$id = "z" . md5(microtime(true));
$row = array
(
"id" => $id // used for later lookups
, "name" => "John Doe"
, "email" => "john@example.com"
, "comments" => "Hello world"
);
if ($ss->addRow($row)) echo "Form data successfully stored";
else echo "Error, unable to store data";
$row = array
(
"name" => "John Q Doe"
);
if ($ss->updateRow($row,"id=".$id)) echo "Form data successfully updated";
else echo "Error, unable to update spreadsheet data";
?>
If you need to get row data …
<?php
// Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
$u = "username@gmail.com";
$p = "password";
$ss = new Google_Spreadsheet($u,$p);
$ss->useSpreadsheet("My Spreadsheet");
$ss->useWorksheet("wks2");
$rows = $ss->getRows("id=zd92cd4a8f7a001c343a5144ad3570668");
if ($rows) print_r($rows);
else echo "Error, unable to get spreadsheet data";
// double quotes must be used for values with spaces
$rows = $ss->getRows('name="John Doe"');
if ($rows) print_r($rows);
else echo "Error, unable to get spreadsheet data";
?>
If you need to delete row data (courtesy of dmon) …
<?php
// Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
$u = "username@gmail.com";
$p = "password";
$ss = new Google_Spreadsheet($u,$p);
$ss->useSpreadsheet("My Spreadsheet");
// if not setting worksheet, "Sheet1" is assumed
// $ss->useWorksheet("worksheetName");
try
{
if ($ss->deleteRow('name="John Q Doe"')) echo "Form data successfully deleted";
else echo "Error, unable to delete data";
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>
Download
Google Spreadsheet PHP Helper Class, this project is on github.

Hi, first off, great tutorial. It was really helpful and very well written. However, I would also like to know how to addColumn rather than a row. Cheers
How to add auto increment ID (with numbers only) and timestamp to spreadsheet ? Is it possible? THanks in advance!
Hi!
I need to make this thing work..
But I get this error:
Warning: require_once(Zend/Loader.php) [function.require-once]: failed to open stream: No such file or directory in /home/mydomain/public_html/mydomain.in/register/Google_Spreadsheet.php on line 216
Fatal error: require_once() [function.require]: Failed opening required ‘Zend/Loader.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php:zend/library’) in /home/mydomain/public_html/mydomain.in/register/Google_Spreadsheet.php on line 216
I guess I haven’t installed ZEND properly, can you suggest how to install it?
Thanks for such a great tutorial and helper class !
I have used this and it works fine for me. I am using PHP: 5.3.5. One problem is that it is giving one warning like
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\usolver\Google_Spreadsheet.php on line 258
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\usolver\Google_Spreadsheet.php on line 289
I am unable to hide this warning or solve this or solve this warning. Please help me for this warning.
Can you provide an example of this code with oauth 2.0? I don’t want to use google user name password
Hey,
I use this in some use cases. But went to a problem, when the Spreadsheet is bigger then some size, the memory on server for PHP is out. Is there any option to work with Google Spreadsheet in more memory efficient way? Any ideas? I think that Zend GData works with Spreadsheet in memory…and the spreadsheet is (for sure) constantly increasing…
Very interesting and useful article, thanks a lot for this helper!
I’m using it on a French site and unfortunately, the cleanKey() function removes the accentued characters (é à è ï…), any idea how to avoid that?
I do have some columns with accent in their headers, so I can’t get to write in them properly :/
Anyway, thanks again!
Actually, please ignore my comment, I found a solution to allow accents
function cleanKey($k)
{
return strtolower(preg_replace(‘/[^A-Za-z0-9àáâãäåçèéêëìíîïðòóôõöùúûüýÿ\-\.]+/’,”,$k));
}