Insert row into Google Spreadsheet using Zend Framework

I got a little stumped when attempting to insert a new row in a Google Docs Spreadsheet using the Zend Framework, so I figured I’d share this in case someone else runs in the same problems. The documentation provided is really great, but I was getting caught in how the row data was supposed to be getting passed. It just needs a basic array with key/value pairs.

First you need to have the Zend Framework installed on your server. Google Code has a great article on getting started. Then you need to have a spreadsheet in Google Docs.

Now we’re going to write a simple PHP script that will input a new row into a basic spreadsheet. First you need to call the following Zend Framework include files for the script to work:

require_once 'Zend/Loader.php';

Next we’re going to set two variables with your Google Docs login info to authenticate our row creation. Enter you Google Account login:

$email = '<your_google_account_username>';
$pass = '<your_google_account_password>';

Now we authenticate ourselves with Google Docs and create a Zend_Gdata_Spreadsheets object.

$authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, $authService);
$spreadsheetService = new Zend_Gdata_Spreadsheets($httpClient);

Next we have to let Google Docs know which of our spreadsheets we want to write to. To do this we need the spreadsheet key. Obtaining the key is fairly simple, just grab it from the spreadsheet’s URL.

For instance:

the key is:


Not only do we need the spreadsheet key, but you also have to know what the worksheet id is too. Obtaining the worksheet id can be a little tricky, but if you only have one worksheet then it usually always is od6.

$spreadsheetKey = 'pqtY4KMBjwqlinsu9-f1PEg';
$worksheetId = 'od6';

Now we have to create the row data that we want to add to the above mentioned spreadsheet. You have to pass it an array with key/value pairs. The key is the column label in all lower case.

So to add a new row containing the string smurf to a column named stuff you build the following array:

$rowData = array('stuff' => 'smurf');

That’s it. If you were adding to more columns you would just pass more key/value pairs to the variable.

Now you just call the insertRow method and you’re all set.

$insertedListEntry = $spreadsheetService->insertRow($rowData, $spreadsheetKey, $worksheetId);

Pretty easy and very cool stuff.

14 replies on “Insert row into Google Spreadsheet using Zend Framework”

Oh man, I spent hours and hours trying to figure this out. You have no idea how much this has helped me. I’ll have a drink in your name as soon as I get this project finished!

Ditto, very helpful info. A couple other things worth noting:

1) Row one is ALWAYS the your column header/label row

2) Column header/labels are both lower case AND spaces, underscores are trimmed. So for example a column header with the cell value of “Member Activity Last 24 hours” would need to be referenced with “memberactivitylast24hours”.

3) Getting your worksheet id is a bit tricky. Here is how I did it. Use the code above to get your $spreadsheetService defined. Then use this code to list the ID for each worksheet:

$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$feed = $spreadsheetService->getWorksheetFeed($query);
foreach ($feed as $key => $entry) {
echo $entry->title;
echo $entry->id;

The result will be something similar to :


Its the last part of the URI that you want, so in this example ‘od4’ is the id of the given worksheet. Use that where ever you need to provide the worksheet id.

thnak you,
But I have a problem. Its getting “Authentication with Google failed. Reason: BadAuthentication”

hmm, first make sure that you’re entering your authentication correctly. Google recently changed how their Google Apps are accessed, so that could also be a possible reason. You might have to merge account(s). Login to your Google Docs and it will most likely alert you if this is the case.

I have some data stored on phpmyadmin database. How can i send this data using zend’s google spreadsheet api.

Can anyone help with this.


Any idea how I do an insert into the middle of the spreadsheet, rather than appending to the end? The only way I can think of is to update every row. i.e. the references on every row need checking and may need an update if I manually insert by moving rows down.
But for some reason the delete method manages to look after all the references, suggesting an easy insert is possible.

Leave a Reply

Your email address will not be published. Required fields are marked *