8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
18 * @subpackage Spreadsheets
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
21 * @version $Id: Spreadsheets.php 16971 2009-07-22 18:05:45Z mikaelkael $
27 require_once('Zend/Gdata.php');
30 * Zend_Gdata_Spreadsheets_SpreadsheetFeed
32 require_once('Zend/Gdata/Spreadsheets/SpreadsheetFeed.php');
35 * Zend_Gdata_Spreadsheets_WorksheetFeed
37 require_once('Zend/Gdata/Spreadsheets/WorksheetFeed.php');
40 * Zend_Gdata_Spreadsheets_CellFeed
42 require_once('Zend/Gdata/Spreadsheets/CellFeed.php');
45 * Zend_Gdata_Spreadsheets_ListFeed
47 require_once('Zend/Gdata/Spreadsheets/ListFeed.php');
50 * Zend_Gdata_Spreadsheets_SpreadsheetEntry
52 require_once('Zend/Gdata/Spreadsheets/SpreadsheetEntry.php');
55 * Zend_Gdata_Spreadsheets_WorksheetEntry
57 require_once('Zend/Gdata/Spreadsheets/WorksheetEntry.php');
60 * Zend_Gdata_Spreadsheets_CellEntry
62 require_once('Zend/Gdata/Spreadsheets/CellEntry.php');
65 * Zend_Gdata_Spreadsheets_ListEntry
67 require_once('Zend/Gdata/Spreadsheets/ListEntry.php');
70 * Zend_Gdata_Spreadsheets_DocumentQuery
72 require_once('Zend/Gdata/Spreadsheets/DocumentQuery.php');
75 * Zend_Gdata_Spreadsheets_ListQuery
77 require_once('Zend/Gdata/Spreadsheets/ListQuery.php');
80 * Zend_Gdata_Spreadsheets_CellQuery
82 require_once('Zend/Gdata/Spreadsheets/CellQuery.php');
87 * @link http://code.google.com/apis/gdata/spreadsheets.html
91 * @subpackage Spreadsheets
92 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
93 * @license http://framework.zend.com/license/new-bsd New BSD License
95 class Zend_Gdata_Spreadsheets
extends Zend_Gdata
97 const SPREADSHEETS_FEED_URI
= 'http://spreadsheets.google.com/feeds/spreadsheets';
98 const SPREADSHEETS_POST_URI
= 'http://spreadsheets.google.com/feeds/spreadsheets/private/full';
99 const WORKSHEETS_FEED_LINK_URI
= 'http://schemas.google.com/spreadsheets/2006#worksheetsfeed';
100 const LIST_FEED_LINK_URI
= 'http://schemas.google.com/spreadsheets/2006#listfeed';
101 const CELL_FEED_LINK_URI
= 'http://schemas.google.com/spreadsheets/2006#cellsfeed';
102 const AUTH_SERVICE_NAME
= 'wise';
105 * Namespaces used for Zend_Gdata_Photos
109 public static $namespaces = array(
110 array('gs', 'http://schemas.google.com/spreadsheets/2006', 1, 0),
112 'gsx', 'http://schemas.google.com/spreadsheets/2006/extended', 1, 0)
116 * Create Gdata_Spreadsheets object
118 * @param Zend_Http_Client $client (optional) The HTTP client to use when
119 * when communicating with the Google servers.
120 * @param string $applicationId The identity of the app in the form of Company-AppName-Version
122 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
124 $this->registerPackage('Zend_Gdata_Spreadsheets');
125 $this->registerPackage('Zend_Gdata_Spreadsheets_Extension');
126 parent
::__construct($client, $applicationId);
127 $this->_httpClient
->setParameterPost('service', self
::AUTH_SERVICE_NAME
);
128 $this->_server
= 'spreadsheets.google.com';
132 * Gets a spreadsheet feed.
134 * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
135 * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
137 public function getSpreadsheetFeed($location = null)
139 if ($location == null) {
140 $uri = self
::SPREADSHEETS_FEED_URI
;
141 } else if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery
) {
142 if ($location->getDocumentType() == null) {
143 $location->setDocumentType('spreadsheets');
145 $uri = $location->getQueryUrl();
150 return parent
::getFeed($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetFeed');
154 * Gets a spreadsheet entry.
156 * @param string $location A DocumentQuery or a URI specifying the entry location.
157 * @return SpreadsheetEntry
159 public function getSpreadsheetEntry($location)
161 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery
) {
162 if ($location->getDocumentType() == null) {
163 $location->setDocumentType('spreadsheets');
165 $uri = $location->getQueryUrl();
170 return parent
::getEntry($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetEntry');
174 * Gets a worksheet feed.
176 * @param mixed $location A DocumentQuery, SpreadsheetEntry, or a string URI
177 * @return Zend_Gdata_Spreadsheets_WorksheetFeed The feed of worksheets
179 public function getWorksheetFeed($location)
181 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery
) {
182 if ($location->getDocumentType() == null) {
183 $location->setDocumentType('worksheets');
185 $uri = $location->getQueryUrl();
186 } else if ($location instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry
) {
187 $uri = $location->getLink(self
::WORKSHEETS_FEED_LINK_URI
)->href
;
192 return parent
::getFeed($uri, 'Zend_Gdata_Spreadsheets_WorksheetFeed');
196 * Gets a worksheet entry.
198 * @param string $location A DocumentQuery or a URI specifying the entry location.
199 * @return WorksheetEntry
201 public function GetWorksheetEntry($location)
203 if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery
) {
204 if ($location->getDocumentType() == null) {
205 $location->setDocumentType('worksheets');
207 $uri = $location->getQueryUrl();
212 return parent
::getEntry($uri, 'Zend_Gdata_Spreadsheets_WorksheetEntry');
218 * @param string $location A CellQuery, WorksheetEntry or a URI specifying the feed location.
221 public function getCellFeed($location)
223 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery
) {
224 $uri = $location->getQueryUrl();
225 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry
) {
226 $uri = $location->getLink(self
::CELL_FEED_LINK_URI
)->href
;
230 return parent
::getFeed($uri, 'Zend_Gdata_Spreadsheets_CellFeed');
236 * @param string $location A CellQuery or a URI specifying the entry location.
239 public function getCellEntry($location)
241 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery
) {
242 $uri = $location->getQueryUrl();
247 return parent
::getEntry($uri, 'Zend_Gdata_Spreadsheets_CellEntry');
253 * @param mixed $location A ListQuery, WorksheetEntry or string URI specifying the feed location.
256 public function getListFeed($location)
258 if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery
) {
259 $uri = $location->getQueryUrl();
260 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry
) {
261 $uri = $location->getLink(self
::LIST_FEED_LINK_URI
)->href
;
266 return parent
::getFeed($uri, 'Zend_Gdata_Spreadsheets_ListFeed');
272 * @param string $location A ListQuery or a URI specifying the entry location.
275 public function getListEntry($location)
277 if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery
) {
278 $uri = $location->getQueryUrl();
283 return parent
::getEntry($uri, 'Zend_Gdata_Spreadsheets_ListEntry');
287 * Updates an existing cell.
289 * @param int $row The row containing the cell to update
290 * @param int $col The column containing the cell to update
291 * @param int $inputValue The new value for the cell
292 * @param string $key The key for the spreadsheet to be updated
293 * @param string $wkshtId (optional) The worksheet to be updated
294 * @return CellEntry The updated cell entry.
296 public function updateCell($row, $col, $inputValue, $key, $wkshtId = 'default')
298 $cell = 'R'.$row.'C'.$col;
300 $query = new Zend_Gdata_Spreadsheets_CellQuery();
301 $query->setSpreadsheetKey($key);
302 $query->setWorksheetId($wkshtId);
303 $query->setCellId($cell);
305 $entry = $this->getCellEntry($query);
306 $entry->setCell(new Zend_Gdata_Spreadsheets_Extension_Cell(null, $row, $col, $inputValue));
307 $response = $entry->save();
312 * Inserts a new row with provided data.
314 * @param array $rowData An array of column header to row data
315 * @param string $key The key of the spreadsheet to modify
316 * @param string $wkshtId (optional) The worksheet to modify
317 * @return ListEntry The inserted row
319 public function insertRow($rowData, $key, $wkshtId = 'default')
321 $newEntry = new Zend_Gdata_Spreadsheets_ListEntry();
322 $newCustomArr = array();
323 foreach ($rowData as $k => $v) {
324 $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
325 $newCustom->setText($v)->setColumnName($k);
326 $newEntry->addCustom($newCustom);
329 $query = new Zend_Gdata_Spreadsheets_ListQuery();
330 $query->setSpreadsheetKey($key);
331 $query->setWorksheetId($wkshtId);
333 $feed = $this->getListFeed($query);
334 $editLink = $feed->getLink('http://schemas.google.com/g/2005#post');
336 return $this->insertEntry($newEntry->saveXML(), $editLink->href
, 'Zend_Gdata_Spreadsheets_ListEntry');
340 * Updates an existing row with provided data.
342 * @param ListEntry $entry The row entry to update
343 * @param array $newRowData An array of column header to row data
345 public function updateRow($entry, $newRowData)
347 $newCustomArr = array();
348 foreach ($newRowData as $k => $v) {
349 $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
350 $newCustom->setText($v)->setColumnName($k);
351 $newCustomArr[] = $newCustom;
353 $entry->setCustom($newCustomArr);
355 return $entry->save();
359 * Deletes an existing row .
361 * @param ListEntry $entry The row to delete
363 public function deleteRow($entry)
369 * Returns the content of all rows as an associative array
371 * @param mixed $location A ListQuery or string URI specifying the feed location.
372 * @return array An array of rows. Each element of the array is an associative array of data
374 public function getSpreadsheetListFeedContents($location)
376 $listFeed = $this->getListFeed($location);
377 $listFeed = $this->retrieveAllEntriesForFeed($listFeed);
378 $spreadsheetContents = array();
379 foreach ($listFeed as $listEntry) {
380 $rowContents = array();
381 $customArray = $listEntry->getCustom();
382 foreach ($customArray as $custom) {
383 $rowContents[$custom->getColumnName()] = $custom->getText();
385 $spreadsheetContents[] = $rowContents;
387 return $spreadsheetContents;
391 * Returns the content of all cells as an associative array, indexed
392 * off the cell location (ie 'A1', 'D4', etc). Each element of
393 * the array is an associative array with a 'value' and a 'function'.
394 * Only non-empty cells are returned by default. 'range' is the
395 * value of the 'range' query parameter specified at:
396 * http://code.google.com/apis/spreadsheets/reference.html#cells_Parameters
398 * @param mixed $location A CellQuery, WorksheetEntry or a URL (w/o query string) specifying the feed location.
399 * @param string $range The range of cells to retrieve
400 * @param boolean $empty Whether to retrieve empty cells
401 * @return array An associative array of cells
403 public function getSpreadsheetCellFeedContents($location, $range = null, $empty = false)
406 if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery
) {
407 $cellQuery = $location;
408 } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry
) {
409 $url = $location->getLink(self
::CELL_FEED_LINK_URI
)->href
;
410 $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
413 $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
416 if ($range != null) {
417 $cellQuery->setRange($range);
419 $cellQuery->setReturnEmpty($empty);
421 $cellFeed = $this->getCellFeed($cellQuery);
422 $cellFeed = $this->retrieveAllEntriesForFeed($cellFeed);
423 $spreadsheetContents = array();
424 foreach ($cellFeed as $cellEntry) {
425 $cellContents = array();
426 $cell = $cellEntry->getCell();
427 $cellContents['formula'] = $cell->getInputValue();
428 $cellContents['value'] = $cell->getText();
429 $spreadsheetContents[$cellEntry->getTitle()->getText()] = $cellContents;
431 return $spreadsheetContents;
435 * Alias for getSpreadsheetFeed
437 * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
438 * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
440 public function getSpreadsheets($location = null)
442 return $this->getSpreadsheetFeed($location = null);