Advertisement
Guest User

Untitled

a guest
Nov 7th, 2009
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. class SomeController extends AppController {
  2. var $uses = null; // Don't use any model
  3. function index() {
  4.     App::import('Vendor', 'zend_include_path');
  5.     App::import('Vendor', 'Zend_Gdata', true, false, 'Zend/Gdata.php');
  6.    
  7.     Zend_Loader::loadClass('Zend_Http_Client');
  8.     Zend_Loader::loadClass('Zend_Gdata');
  9.     Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  10.     Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
  11.    
  12.     $authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  13.     $user = 'username';
  14.     $pass = 'password';
  15.     $this->currKey = 'thesheetkey';
  16.    
  17.     try {
  18.         $httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $authService);
  19.         $this->gdClient = new Zend_Gdata_Spreadsheets($httpClient);
  20.        
  21.         $this->promptForWorksheet(0); // Put the 0th worksheet of our sheet to $this->currWkshtId
  22.         $this->listGetAction(); // Will list all the rows inside the worksheet
  23.        
  24.         $row = array('column1'=>'value','column2'=>'value','columnN'=>'value');
  25.         $this->listInsertAction($row);
  26.         } catch ( Exception $e )  {
  27.         echo $e->getMessage();
  28.     }
  29. }
  30.  
  31. public function promptForWorksheet($wordSheetI=0)
  32. {
  33.     $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  34.     $query->setSpreadsheetKey($this->currKey);
  35.     $feed = $this->gdClient->getWorksheetFeed($query);
  36.     print "== Available Worksheets ==\n";
  37.     $this->printFeed($feed);
  38.     $input = $wordSheetI;
  39.     $currWkshtId = split('/', $feed->entries[$input]->id->text);
  40.     $this->currWkshtId = $currWkshtId[8];
  41. }
  42. public function listGetAction()
  43. {
  44.     $query = new Zend_Gdata_Spreadsheets_ListQuery();
  45.     $query->setSpreadsheetKey($this->currKey);
  46.     $query->setWorksheetId($this->currWkshtId);
  47.     $this->listFeed = $this->gdClient->getListFeed($query);
  48.     print "entry id | row-content in column A | column-header: cell-content\n".
  49.     "Please note: The 'dump' command on the list feed only dumps data until the first blank row is encountered.\n\n";
  50.    
  51.     $this->printFeed($this->listFeed);
  52.     print "\n";
  53. }
  54. public function printFeed($feed)
  55. {
  56.     $i = 0;
  57.     foreach($feed->entries as $entry) {
  58.         if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
  59.             print $entry->title->text .' '. $entry->content->text . "\n";
  60.             } else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
  61.             print $i .' '. $entry->title->text .' | '. $entry->content->text . "\n";
  62.             } else {
  63.             print $i .' '. $entry->title->text . "\n";
  64.         }
  65.         $i++;
  66.     }
  67. }
  68. public function listInsertAction($rowArray)
  69. {
  70.     //$rowArray = $this->stringToArray($rowData);
  71.     $entry = $this->gdClient->insertRow($rowArray, $this->currKey,
  72.     $this->currWkshtId);
  73.    
  74.     if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
  75.         foreach ($rowArray as $column_header => $value) {
  76.             echo "Success! Inserted '$value' in column '$column_header' at row ".
  77.             substr($entry->getTitle()->getText(), 5) ."\n";
  78.         }
  79.     }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement