Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxnowteam/7da3ff29835a21496f6583f7d8a83b85 to your computer and use it in GitHub Desktop.
Save fxnowteam/7da3ff29835a21496f6583f7d8a83b85 to your computer and use it in GitHub Desktop.
Google_Spreadsheets
<?php
/*
Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class Google_Spreadsheet
{
private $client;
private $spreadsheetRequest;
private $spreadsheetService;
private $spreadsheet;
private $spreadsheet_id;
private $worksheet = "Sheet1";
private $worksheet_id;
//OAuth Info
private $accessToken;
private $clientEmail;
function __construct($cID,$cEMail,$keyPath,$keyPW,$ss=FALSE,$ws=FALSE)
{
$this->login($cID,$cEMail,$keyPath,$keyPW);
$this->clientEmail = $cEMail;
if ($ss) $this->useSpreadsheet($ss);
if ($ws) $this->useWorksheet($ws);
}
function useSpreadsheet($ss,$ws=FALSE)
{
$this->spreadsheet = $ss;
$this->spreadsheet_id = NULL;
if ($ws) $this->useWorksheet($ws);
}
function useWorksheet($ws)
{
$this->worksheet = $ws;
$this->worksheet_id = NULL;
}
function getListFeed(){
$spreadsheetFeed = $this->spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle($this->spreadsheet);
if(is_null($spreadsheet)) throw new Exception('Unable to find spreadsheet by name: "' . $this->spreadsheet . '", confirm the name of the spreadsheet and confirm it is shared with "' . $this->clientEmail . '".');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle($this->worksheet);
if(is_null($spreadsheet)) throw new Exception('Unable to find worksheet by name: "' . $this->worksheet . '", confirm the name of the worksheet');
$listFeed = $worksheet->getListFeed();
if(is_null($listFeed)) throw new Exception('Unable to find get list feed');
return $listFeed;
}
function addRow($row)
{
$listFeed = $this->getListFeed();
$listFeed->insert($row);
}
/*
// http://code.google.com/apis/spreadsheets/docs/2.0/reference.html#ListParameters
function updateRow($row,$search)
{
if ($this->client instanceof Zend_Gdata_Spreadsheets AND $search)
{
$feed = $this->findRows($search);
if ($feed->entries)
{
foreach($feed->entries as $entry)
{
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
{
$update_row = array();
$customRow = $entry->getCustom();
foreach ($customRow as $customCol)
{
$update_row[$customCol->getColumnName()] = $customCol->getText();
}
// overwrite with new values
foreach ($row as $k => $v)
{
$update_row[$this->cleanKey($k)] = $v;
}
// update row data, then save
$entry = $this->client->updateRow($entry,$update_row);
if ( ! ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)) return FALSE;
}
}
return TRUE;
}
}
return FALSE;
}
*/
// http://code.google.com/apis/spreadsheets/docs/2.0/reference.html#ListParameters
function getRows($search=FALSE)
{
$rows = array();
$listFeed = $this->getListFeed();
foreach ($listFeed->getEntries() as $entry) {
$row = array();
$rows[] = $entry->getValues();
}
return $rows;
}
/*
// user contribution by dmon (6/10/2009)
function deleteRow($search)
{
if ($this->client instanceof Zend_Gdata_Spreadsheets AND $search)
{
$feed = $this->findRows($search);
if ($feed->entries)
{
foreach($feed->entries as $entry)
{
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
{
$this->client->deleteRow($entry);
if ( ! ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)) return FALSE;
}
}
return TRUE;
}
}
return FALSE;
}
function getColumnNames()
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->getSpreadsheetId());
$query->setWorksheetId($this->getWorksheetId());
$query->setMaxResults(1);
$query->setStartIndex(1);
$feed = $this->client->getListFeed($query);
$data = array();
if ($feed->entries)
{
foreach($feed->entries as $entry)
{
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry)
{
$customRow = $entry->getCustom();
foreach ($customRow as $customCol)
{
array_push($data,$customCol->getColumnName());
}
}
}
}
return $data;
}
*/
private function login($cID,$cEMail,$keyPath,$keyPW)
{
//login using oauth2
//needs https://github.com/google/google-api-php-client via composer
$obj_client_auth = new Google_Client ();
$obj_client_auth -> setApplicationName ('test_or_whatever_you_like');
$obj_client_auth -> setClientId ($cID);
$obj_client_auth -> setAssertionCredentials (new Google_Auth_AssertionCredentials (
$cEMail,
array('https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'),
file_get_contents ($keyPath),
$keyPW
));
$obj_client_auth -> getAuth () -> refreshTokenWithAssertion ();
$obj_token = json_decode ($obj_client_auth -> getAccessToken ());
$this->accessToken = $obj_token->access_token;
//check that we are logged in
if (!$this->accessToken) {
echo "Login Failed.\n";
return FALSE;
}
//set spreadsheet objects
//needs https://github.com/asimlqt/php-google-spreadsheet-client via composer
$this->serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($this->accessToken);
Google\Spreadsheet\ServiceRequestFactory::setInstance($this->serviceRequest);
$this->spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
return TRUE;
}
/*
private function findRows($search=FALSE)
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->getSpreadsheetId());
$query->setWorksheetId($this->getWorksheetId());
if ($search) $query->setSpreadsheetQuery($search);
$feed = $this->client->getListFeed($query);
return $feed;
}
private function getSpreadsheetId($ss=FALSE)
{
if ($this->spreadsheet_id) return $this->spreadsheet_id;
$ss = $ss?$ss:$this->spreadsheet;
$ss_id = FALSE;
$feed = $this->client->getSpreadsheetFeed();
foreach($feed->entries as $entry)
{
if ($entry->title->text == $ss)
{
$ss_id = explode("/",$entry->id->text);
$ss_id = array_pop($ss_id);
$this->spreadsheet_id = $ss_id;
break;
}
}
return $ss_id;
}
private function getWorksheetId($ss_id=FALSE,$ws=FALSE)
{
if ($this->worksheet_id) return $this->worksheet_id;
$ss_id = $ss_id?$ss_id:$this->spreadsheet_id;
$ws = $ws?$ws:$this->worksheet;
$wk_id = FALSE;
if ($ss_id AND $ws)
{
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($ss_id);
$feed = $this->client->getWorksheetFeed($query);
foreach($feed->entries as $entry)
{
if ($entry->title->text == $ws)
{
$wk_id = explode("/",$entry->id->text);
$wk_id = array_pop($wk_id);
$this->worksheet_id = $wk_id;
break;
}
}
}
return $wk_id;
}
function cleanKey($k)
{
return strtolower(preg_replace('/[^A-Za-z0-9\-\.]+/','',$k));
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment