- Follow the steps 1, 2, 3, 4 and 6 of this article: http://www.nczonline.net/blog/2014/03/04/accessing-google-spreadsheets-from-node-js/
- Download jgoogleapi and copy it into the extension folder of your app: http://www.yiiframework.com/extension/jgoogleapi/
- Download php-google-spreadsheet-client and copy it into the vendor folder of your app: https://github.com/asimlqt/php-google-spreadsheet-client/
Created
March 19, 2014 22:44
-
-
Save leandono/9653008 to your computer and use it in GitHub Desktop.
[Yii] Saving data into Google Spreadsheet using JGoogleAPI & php-google-spreadsheet-client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Configuration of JGoogleAPI | |
// application components | |
'components' => array( | |
'JGoogleAPI' => array( | |
'class' => 'ext.JGoogleAPI.JGoogleAPI', | |
'defaultAuthenticationType'=>'serviceAPI', | |
'serviceAPI' => array( | |
'clientId' => 'YOUR_SERVICE_ACCOUNT_CLIENT_ID', | |
'clientEmail' => 'YOUR_SERVICE_ACCOUNT_CLIENT_EMAIL', | |
'keyFilePath' => 'THE_PATH_TO_YOUR_KEY_FILE', //.p12 file | |
), | |
'scopes' => array( | |
'serviceAPI' => array( | |
'drive' => array( | |
'https://spreadsheets.google.com/feeds', | |
), | |
), | |
), | |
'useObjects'=>true, | |
), | |
... | |
), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Yii::import('application.vendors.*'); | |
require_once 'Google/Spreadsheet/Autoloader.php'; | |
class SiteController extends CController | |
{ | |
/** | |
* Index | |
*/ | |
public function actionIndex() | |
{ | |
$spreadsheetTitle = '<name of the spreadsheet>'; | |
$worksheetTitle = '<name of the worksheet>'; | |
$data = array(); | |
//Data to save into the column 'email'. Be sure that exist the column! | |
$data['Email'] = 'test@example.com'; | |
$token = $this->_getToken(); | |
if($this->_saveToSpreadsheet($spreadsheetTitle, $worksheetTitle, $data, $token)){ | |
echo 'saved!'; | |
} else { | |
echo 'something goes wrong'; | |
} | |
} | |
/** | |
* Get a valid token to use with the Google Services | |
*/ | |
private function _getToken() | |
{ | |
$jgoogleapi = Yii::app()->JGoogleAPI; | |
$client = $jgoogleapi->getClient(); | |
$client->setAccessType('offline'); | |
$client->setApprovalPrompt('force'); | |
//Save the token in a json file | |
$tokenFile = Yii::app()->file->set(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'token.json'); | |
$token = $tokenFile->getContents(); | |
if ($token) { | |
$client->setAccessToken($token); | |
} | |
if($client->getAuth()->isAccessTokenExpired()) { | |
$client->getAuth()->refreshTokenWithAssertion(); | |
$tokenFile->create(); | |
$tokenFile->setContents($client->getAccessToken(), true); | |
} | |
$tokenData = json_decode($client->getAccessToken()); | |
return $tokenData->access_token; | |
} | |
/** | |
* Save information in to a Google Spreadsheet | |
* @param {string} $spreadsheetTitle Title of the Spreadsheet to use | |
* @param {string} $worksheetTitle Title of the Worksheet to use | |
* @param {array} $data Information to grab in the Spreadsheet | |
* @param {string} $token Token for authentication | |
*/ | |
private function _saveToSpreadsheet($spreadsheetTitle, $worksheetTitle, $data, $token) | |
{ | |
$request = new Google\Spreadsheet\Request($token); | |
$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($request); | |
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest); | |
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); | |
$spreadsheetFeed = $spreadsheetService->getSpreadsheets(); | |
$spreadsheet = $spreadsheetFeed->getByTitle($spreadsheetTitle); | |
$worksheetFeed = $spreadsheet->getWorksheets(); | |
$worksheet = $worksheetFeed->getByTitle($worksheetTitle); | |
$listFeed = $worksheet->getListFeed(); | |
return $listFeed->insert($data) ? true : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment