File indexing completed on 2025-03-09 05:25:57
0001 <?php 0002 0003 /** 0004 * Zend Framework 0005 * 0006 * LICENSE 0007 * 0008 * This source file is subject to the new BSD license that is bundled 0009 * with this package in the file LICENSE.txt. 0010 * It is also available through the world-wide-web at this URL: 0011 * http://framework.zend.com/license/new-bsd 0012 * If you did not receive a copy of the license and are unable to 0013 * obtain it through the world-wide-web, please send an email 0014 * to license@zend.com so we can send you a copy immediately. 0015 * 0016 * @category Zend 0017 * @package Zend_Gdata 0018 * @subpackage Spreadsheets 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 * @version $Id$ 0022 */ 0023 0024 /** 0025 * Zend_Gdata 0026 */ 0027 // require_once('Zend/Gdata.php'); 0028 0029 /** 0030 * Zend_Gdata_Spreadsheets_SpreadsheetFeed 0031 */ 0032 // require_once('Zend/Gdata/Spreadsheets/SpreadsheetFeed.php'); 0033 0034 /** 0035 * Zend_Gdata_Spreadsheets_WorksheetFeed 0036 */ 0037 // require_once('Zend/Gdata/Spreadsheets/WorksheetFeed.php'); 0038 0039 /** 0040 * Zend_Gdata_Spreadsheets_CellFeed 0041 */ 0042 // require_once('Zend/Gdata/Spreadsheets/CellFeed.php'); 0043 0044 /** 0045 * Zend_Gdata_Spreadsheets_ListFeed 0046 */ 0047 // require_once('Zend/Gdata/Spreadsheets/ListFeed.php'); 0048 0049 /** 0050 * Zend_Gdata_Spreadsheets_SpreadsheetEntry 0051 */ 0052 // require_once('Zend/Gdata/Spreadsheets/SpreadsheetEntry.php'); 0053 0054 /** 0055 * Zend_Gdata_Spreadsheets_WorksheetEntry 0056 */ 0057 // require_once('Zend/Gdata/Spreadsheets/WorksheetEntry.php'); 0058 0059 /** 0060 * Zend_Gdata_Spreadsheets_CellEntry 0061 */ 0062 // require_once('Zend/Gdata/Spreadsheets/CellEntry.php'); 0063 0064 /** 0065 * Zend_Gdata_Spreadsheets_ListEntry 0066 */ 0067 // require_once('Zend/Gdata/Spreadsheets/ListEntry.php'); 0068 0069 /** 0070 * Zend_Gdata_Spreadsheets_DocumentQuery 0071 */ 0072 // require_once('Zend/Gdata/Spreadsheets/DocumentQuery.php'); 0073 0074 /** 0075 * Zend_Gdata_Spreadsheets_ListQuery 0076 */ 0077 // require_once('Zend/Gdata/Spreadsheets/ListQuery.php'); 0078 0079 /** 0080 * Zend_Gdata_Spreadsheets_CellQuery 0081 */ 0082 // require_once('Zend/Gdata/Spreadsheets/CellQuery.php'); 0083 0084 /** 0085 * Gdata Spreadsheets 0086 * 0087 * @link http://code.google.com/apis/gdata/spreadsheets.html 0088 * 0089 * @category Zend 0090 * @package Zend_Gdata 0091 * @subpackage Spreadsheets 0092 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0093 * @license http://framework.zend.com/license/new-bsd New BSD License 0094 */ 0095 class Zend_Gdata_Spreadsheets extends Zend_Gdata 0096 { 0097 const SPREADSHEETS_FEED_URI = 'https://spreadsheets.google.com/feeds/spreadsheets'; 0098 const SPREADSHEETS_POST_URI = 'https://spreadsheets.google.com/feeds/spreadsheets/private/full'; 0099 const WORKSHEETS_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#worksheetsfeed'; 0100 const LIST_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#listfeed'; 0101 const CELL_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#cellsfeed'; 0102 const AUTH_SERVICE_NAME = 'wise'; 0103 0104 /** 0105 * Namespaces used for Zend_Gdata_Photos 0106 * 0107 * @var array 0108 */ 0109 public static $namespaces = array( 0110 array('gs', 'http://schemas.google.com/spreadsheets/2006', 1, 0), 0111 array( 0112 'gsx', 'http://schemas.google.com/spreadsheets/2006/extended', 1, 0) 0113 ); 0114 0115 /** 0116 * Create Gdata_Spreadsheets object 0117 * 0118 * @param Zend_Http_Client $client (optional) The HTTP client to use when 0119 * when communicating with the Google servers. 0120 * @param string $applicationId The identity of the app in the form of Company-AppName-Version 0121 */ 0122 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0') 0123 { 0124 $this->registerPackage('Zend_Gdata_Spreadsheets'); 0125 $this->registerPackage('Zend_Gdata_Spreadsheets_Extension'); 0126 parent::__construct($client, $applicationId); 0127 $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME); 0128 $this->_server = 'spreadsheets.google.com'; 0129 } 0130 0131 /** 0132 * Gets a spreadsheet feed. 0133 * 0134 * @param mixed $location A DocumentQuery or a string URI specifying the feed location. 0135 * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed 0136 */ 0137 public function getSpreadsheetFeed($location = null) 0138 { 0139 if ($location == null) { 0140 $uri = self::SPREADSHEETS_FEED_URI; 0141 } else if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) { 0142 if ($location->getDocumentType() == null) { 0143 $location->setDocumentType('spreadsheets'); 0144 } 0145 $uri = $location->getQueryUrl(); 0146 } else { 0147 $uri = $location; 0148 } 0149 0150 return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetFeed'); 0151 } 0152 0153 /** 0154 * Gets a spreadsheet entry. 0155 * 0156 * @param string $location A DocumentQuery or a URI specifying the entry location. 0157 * @return SpreadsheetEntry 0158 */ 0159 public function getSpreadsheetEntry($location) 0160 { 0161 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) { 0162 if ($location->getDocumentType() == null) { 0163 $location->setDocumentType('spreadsheets'); 0164 } 0165 $uri = $location->getQueryUrl(); 0166 } else { 0167 $uri = $location; 0168 } 0169 0170 return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetEntry'); 0171 } 0172 0173 /** 0174 * Gets a worksheet feed. 0175 * 0176 * @param mixed $location A DocumentQuery, SpreadsheetEntry, or a string URI 0177 * @return Zend_Gdata_Spreadsheets_WorksheetFeed The feed of worksheets 0178 */ 0179 public function getWorksheetFeed($location) 0180 { 0181 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) { 0182 if ($location->getDocumentType() == null) { 0183 $location->setDocumentType('worksheets'); 0184 } 0185 $uri = $location->getQueryUrl(); 0186 } else if ($location instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry) { 0187 $uri = $location->getLink(self::WORKSHEETS_FEED_LINK_URI)->href; 0188 } else { 0189 $uri = $location; 0190 } 0191 0192 return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_WorksheetFeed'); 0193 } 0194 0195 /** 0196 * Gets a worksheet entry. 0197 * 0198 * @param string $location A DocumentQuery or a URI specifying the entry location. 0199 * @return WorksheetEntry 0200 */ 0201 public function GetWorksheetEntry($location) 0202 { 0203 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) { 0204 if ($location->getDocumentType() == null) { 0205 $location->setDocumentType('worksheets'); 0206 } 0207 $uri = $location->getQueryUrl(); 0208 } else { 0209 $uri = $location; 0210 } 0211 0212 return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_WorksheetEntry'); 0213 } 0214 0215 /** 0216 * Gets a cell feed. 0217 * 0218 * @param string $location A CellQuery, WorksheetEntry or a URI specifying the feed location. 0219 * @return CellFeed 0220 */ 0221 public function getCellFeed($location) 0222 { 0223 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) { 0224 $uri = $location->getQueryUrl(); 0225 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) { 0226 $uri = $location->getLink(self::CELL_FEED_LINK_URI)->href; 0227 } else { 0228 $uri = $location; 0229 } 0230 return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_CellFeed'); 0231 } 0232 0233 /** 0234 * Gets a cell entry. 0235 * 0236 * @param string $location A CellQuery or a URI specifying the entry location. 0237 * @return CellEntry 0238 */ 0239 public function getCellEntry($location) 0240 { 0241 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) { 0242 $uri = $location->getQueryUrl(); 0243 } else { 0244 $uri = $location; 0245 } 0246 0247 return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_CellEntry'); 0248 } 0249 0250 /** 0251 * Gets a list feed. 0252 * 0253 * @param mixed $location A ListQuery, WorksheetEntry or string URI specifying the feed location. 0254 * @return ListFeed 0255 */ 0256 public function getListFeed($location) 0257 { 0258 if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) { 0259 $uri = $location->getQueryUrl(); 0260 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) { 0261 $uri = $location->getLink(self::LIST_FEED_LINK_URI)->href; 0262 } else { 0263 $uri = $location; 0264 } 0265 0266 return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_ListFeed'); 0267 } 0268 0269 /** 0270 * Gets a list entry. 0271 * 0272 * @param string $location A ListQuery or a URI specifying the entry location. 0273 * @return ListEntry 0274 */ 0275 public function getListEntry($location) 0276 { 0277 if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) { 0278 $uri = $location->getQueryUrl(); 0279 } else { 0280 $uri = $location; 0281 } 0282 0283 return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_ListEntry'); 0284 } 0285 0286 /** 0287 * Updates an existing cell. 0288 * 0289 * @param int $row The row containing the cell to update 0290 * @param int $col The column containing the cell to update 0291 * @param int $inputValue The new value for the cell 0292 * @param string $key The key for the spreadsheet to be updated 0293 * @param string $wkshtId (optional) The worksheet to be updated 0294 * @return CellEntry The updated cell entry. 0295 */ 0296 public function updateCell($row, $col, $inputValue, $key, $wkshtId = 'default') 0297 { 0298 $cell = 'R'.$row.'C'.$col; 0299 0300 $query = new Zend_Gdata_Spreadsheets_CellQuery(); 0301 $query->setSpreadsheetKey($key); 0302 $query->setWorksheetId($wkshtId); 0303 $query->setCellId($cell); 0304 0305 $entry = $this->getCellEntry($query); 0306 $entry->setCell(new Zend_Gdata_Spreadsheets_Extension_Cell(null, $row, $col, $inputValue)); 0307 $response = $entry->save(); 0308 return $response; 0309 } 0310 0311 /** 0312 * Inserts a new row with provided data. 0313 * 0314 * @param array $rowData An array of column header to row data 0315 * @param string $key The key of the spreadsheet to modify 0316 * @param string $wkshtId (optional) The worksheet to modify 0317 * @return ListEntry The inserted row 0318 */ 0319 public function insertRow($rowData, $key, $wkshtId = 'default') 0320 { 0321 $newEntry = new Zend_Gdata_Spreadsheets_ListEntry(); 0322 $newCustomArr = array(); 0323 foreach ($rowData as $k => $v) { 0324 $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom(); 0325 $newCustom->setText($v)->setColumnName($k); 0326 $newEntry->addCustom($newCustom); 0327 } 0328 0329 $query = new Zend_Gdata_Spreadsheets_ListQuery(); 0330 $query->setSpreadsheetKey($key); 0331 $query->setWorksheetId($wkshtId); 0332 0333 $feed = $this->getListFeed($query); 0334 $editLink = $feed->getLink('http://schemas.google.com/g/2005#post'); 0335 0336 return $this->insertEntry($newEntry->saveXML(), $editLink->href, 'Zend_Gdata_Spreadsheets_ListEntry'); 0337 } 0338 0339 /** 0340 * Updates an existing row with provided data. 0341 * 0342 * @param ListEntry $entry The row entry to update 0343 * @param array $newRowData An array of column header to row data 0344 */ 0345 public function updateRow($entry, $newRowData) 0346 { 0347 $newCustomArr = array(); 0348 foreach ($newRowData as $k => $v) { 0349 $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom(); 0350 $newCustom->setText($v)->setColumnName($k); 0351 $newCustomArr[] = $newCustom; 0352 } 0353 $entry->setCustom($newCustomArr); 0354 0355 return $entry->save(); 0356 } 0357 0358 /** 0359 * Deletes an existing row . 0360 * 0361 * @param ListEntry $entry The row to delete 0362 */ 0363 public function deleteRow($entry) 0364 { 0365 $entry->delete(); 0366 } 0367 0368 /** 0369 * Returns the content of all rows as an associative array 0370 * 0371 * @param mixed $location A ListQuery or string URI specifying the feed location. 0372 * @return array An array of rows. Each element of the array is an associative array of data 0373 */ 0374 public function getSpreadsheetListFeedContents($location) 0375 { 0376 $listFeed = $this->getListFeed($location); 0377 $listFeed = $this->retrieveAllEntriesForFeed($listFeed); 0378 $spreadsheetContents = array(); 0379 foreach ($listFeed as $listEntry) { 0380 $rowContents = array(); 0381 $customArray = $listEntry->getCustom(); 0382 foreach ($customArray as $custom) { 0383 $rowContents[$custom->getColumnName()] = $custom->getText(); 0384 } 0385 $spreadsheetContents[] = $rowContents; 0386 } 0387 return $spreadsheetContents; 0388 } 0389 0390 /** 0391 * Returns the content of all cells as an associative array, indexed 0392 * off the cell location (ie 'A1', 'D4', etc). Each element of 0393 * the array is an associative array with a 'value' and a 'function'. 0394 * Only non-empty cells are returned by default. 'range' is the 0395 * value of the 'range' query parameter specified at: 0396 * http://code.google.com/apis/spreadsheets/reference.html#cells_Parameters 0397 * 0398 * @param mixed $location A CellQuery, WorksheetEntry or a URL (w/o query string) specifying the feed location. 0399 * @param string $range The range of cells to retrieve 0400 * @param boolean $empty Whether to retrieve empty cells 0401 * @return array An associative array of cells 0402 */ 0403 public function getSpreadsheetCellFeedContents($location, $range = null, $empty = false) 0404 { 0405 $cellQuery = null; 0406 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) { 0407 $cellQuery = $location; 0408 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) { 0409 $url = $location->getLink(self::CELL_FEED_LINK_URI)->href; 0410 $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url); 0411 } else { 0412 $url = $location; 0413 $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url); 0414 } 0415 0416 if ($range != null) { 0417 $cellQuery->setRange($range); 0418 } 0419 $cellQuery->setReturnEmpty($empty); 0420 0421 $cellFeed = $this->getCellFeed($cellQuery); 0422 $cellFeed = $this->retrieveAllEntriesForFeed($cellFeed); 0423 $spreadsheetContents = array(); 0424 foreach ($cellFeed as $cellEntry) { 0425 $cellContents = array(); 0426 $cell = $cellEntry->getCell(); 0427 $cellContents['formula'] = $cell->getInputValue(); 0428 $cellContents['value'] = $cell->getText(); 0429 $spreadsheetContents[$cellEntry->getTitle()->getText()] = $cellContents; 0430 } 0431 return $spreadsheetContents; 0432 } 0433 0434 /** 0435 * Alias for getSpreadsheetFeed 0436 * 0437 * @param mixed $location A DocumentQuery or a string URI specifying the feed location. 0438 * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed 0439 */ 0440 public function getSpreadsheets($location = null) 0441 { 0442 return $this->getSpreadsheetFeed($location = null); 0443 } 0444 0445 }