Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SomeController extends AppController {
- var $uses = null; // Don't use any model
- function index() {
- App::import('Vendor', 'zend_include_path');
- App::import('Vendor', 'Zend_Gdata', true, false, 'Zend/Gdata.php');
- Zend_Loader::loadClass('Zend_Http_Client');
- Zend_Loader::loadClass('Zend_Gdata');
- Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
- Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
- $authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
- $user = 'username';
- $pass = 'password';
- $this->currKey = 'thesheetkey';
- try {
- $httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $authService);
- $this->gdClient = new Zend_Gdata_Spreadsheets($httpClient);
- $this->promptForWorksheet(0); // Put the 0th worksheet of our sheet to $this->currWkshtId
- $this->listGetAction(); // Will list all the rows inside the worksheet
- $row = array('column1'=>'value','column2'=>'value','columnN'=>'value');
- $this->listInsertAction($row);
- } catch ( Exception $e ) {
- echo $e->getMessage();
- }
- }
- public function promptForWorksheet($wordSheetI=0)
- {
- $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
- $query->setSpreadsheetKey($this->currKey);
- $feed = $this->gdClient->getWorksheetFeed($query);
- print "== Available Worksheets ==\n";
- $this->printFeed($feed);
- $input = $wordSheetI;
- $currWkshtId = split('/', $feed->entries[$input]->id->text);
- $this->currWkshtId = $currWkshtId[8];
- }
- public function listGetAction()
- {
- $query = new Zend_Gdata_Spreadsheets_ListQuery();
- $query->setSpreadsheetKey($this->currKey);
- $query->setWorksheetId($this->currWkshtId);
- $this->listFeed = $this->gdClient->getListFeed($query);
- print "entry id | row-content in column A | column-header: cell-content\n".
- "Please note: The 'dump' command on the list feed only dumps data until the first blank row is encountered.\n\n";
- $this->printFeed($this->listFeed);
- print "\n";
- }
- public function printFeed($feed)
- {
- $i = 0;
- foreach($feed->entries as $entry) {
- if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
- print $entry->title->text .' '. $entry->content->text . "\n";
- } else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
- print $i .' '. $entry->title->text .' | '. $entry->content->text . "\n";
- } else {
- print $i .' '. $entry->title->text . "\n";
- }
- $i++;
- }
- }
- public function listInsertAction($rowArray)
- {
- //$rowArray = $this->stringToArray($rowData);
- $entry = $this->gdClient->insertRow($rowArray, $this->currKey,
- $this->currWkshtId);
- if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
- foreach ($rowArray as $column_header => $value) {
- echo "Success! Inserted '$value' in column '$column_header' at row ".
- substr($entry->getTitle()->getText(), 5) ."\n";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement