Reading Google spreadsheets in PHP

Here are some coding lines that using Zend GData interface to work with Google spreadsheets and worksheets inside them.

To get the info about all spreadsheets in your Google account

require_once 'Zend/Loader.php';
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient("email", "pass", $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$feed = $spreadsheetService->getSpreadsheetFeed();

// you can print spreadsheet names like this
for($i = 0; $i<count($feed->entries); $i++){
$ss = $feed->entries[$i]; // $ss is a spread sheet
print_r($ss->title->text); // spread name

// you can get worksheets in any spreadsheet like this...
$ws = $feed->entries[$i]->worksheets;

Here is the info about number of worksheets in the spreadsheet:

$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$feed = $spreadsheetService->getWorksheetFeed($query);
echo count($feed->entries); // number of worksheets

Prints all cells from the worksheet #1

$query = new Zend_Gdata_Spreadsheets_CellQuery();
$feed = $spreadsheetService->getWorksheetFeed($query);
$cellFeed = $spreadsheetService->getCellFeed($query);
foreach($cellFeed as $cellEntry) {
	$row = $cellEntry->cell->getRow();
	$col = $cellEntry->cell->getColumn();
	$val = $cellEntry->cell->getText();
	echo "$row, $col = $val\n";

tags: & category: -