Showing posts with label PHP Google Spreadsheets. Show all posts
Showing posts with label PHP Google Spreadsheets. Show all posts

Wednesday, November 23, 2011

PHP Google Spreadsheets

Saving Form Data to Google Spreadsheets Using PHP and the Google Docs API
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:
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:
  1. name
  2. email
  3. 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. // 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";   
?>

Download