Spreadsheets.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Spreadsheets
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Gdata
  23. */
  24. require_once('Zend/Gdata.php');
  25. /**
  26. * Zend_Gdata_Spreadsheets_SpreadsheetFeed
  27. */
  28. require_once('Zend/Gdata/Spreadsheets/SpreadsheetFeed.php');
  29. /**
  30. * Zend_Gdata_Spreadsheets_WorksheetFeed
  31. */
  32. require_once('Zend/Gdata/Spreadsheets/WorksheetFeed.php');
  33. /**
  34. * Zend_Gdata_Spreadsheets_CellFeed
  35. */
  36. require_once('Zend/Gdata/Spreadsheets/CellFeed.php');
  37. /**
  38. * Zend_Gdata_Spreadsheets_ListFeed
  39. */
  40. require_once('Zend/Gdata/Spreadsheets/ListFeed.php');
  41. /**
  42. * Zend_Gdata_Spreadsheets_SpreadsheetEntry
  43. */
  44. require_once('Zend/Gdata/Spreadsheets/SpreadsheetEntry.php');
  45. /**
  46. * Zend_Gdata_Spreadsheets_WorksheetEntry
  47. */
  48. require_once('Zend/Gdata/Spreadsheets/WorksheetEntry.php');
  49. /**
  50. * Zend_Gdata_Spreadsheets_CellEntry
  51. */
  52. require_once('Zend/Gdata/Spreadsheets/CellEntry.php');
  53. /**
  54. * Zend_Gdata_Spreadsheets_ListEntry
  55. */
  56. require_once('Zend/Gdata/Spreadsheets/ListEntry.php');
  57. /**
  58. * Zend_Gdata_Spreadsheets_DocumentQuery
  59. */
  60. require_once('Zend/Gdata/Spreadsheets/DocumentQuery.php');
  61. /**
  62. * Zend_Gdata_Spreadsheets_ListQuery
  63. */
  64. require_once('Zend/Gdata/Spreadsheets/ListQuery.php');
  65. /**
  66. * Zend_Gdata_Spreadsheets_CellQuery
  67. */
  68. require_once('Zend/Gdata/Spreadsheets/CellQuery.php');
  69. /**
  70. * Gdata Spreadsheets
  71. *
  72. * @link http://code.google.com/apis/gdata/spreadsheets.html
  73. *
  74. * @category Zend
  75. * @package Zend_Gdata
  76. * @subpackage Spreadsheets
  77. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  78. * @license http://framework.zend.com/license/new-bsd New BSD License
  79. */
  80. class Zend_Gdata_Spreadsheets extends Zend_Gdata
  81. {
  82. const SPREADSHEETS_FEED_URI = 'http://spreadsheets.google.com/feeds/spreadsheets';
  83. const SPREADSHEETS_POST_URI = 'http://spreadsheets.google.com/feeds/spreadsheets/private/full';
  84. const WORKSHEETS_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#worksheetsfeed';
  85. const LIST_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#listfeed';
  86. const CELL_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#cellsfeed';
  87. const AUTH_SERVICE_NAME = 'wise';
  88. /**
  89. * Namespaces used for Zend_Gdata_Photos
  90. *
  91. * @var array
  92. */
  93. public static $namespaces = array(
  94. array('gs', 'http://schemas.google.com/spreadsheets/2006', 1, 0),
  95. array(
  96. 'gsx', 'http://schemas.google.com/spreadsheets/2006/extended', 1, 0)
  97. );
  98. /**
  99. * Create Gdata_Spreadsheets object
  100. *
  101. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  102. * when communicating with the Google servers.
  103. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  104. */
  105. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  106. {
  107. $this->registerPackage('Zend_Gdata_Spreadsheets');
  108. $this->registerPackage('Zend_Gdata_Spreadsheets_Extension');
  109. parent::__construct($client, $applicationId);
  110. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  111. $this->_server = 'spreadsheets.google.com';
  112. }
  113. /**
  114. * Gets a spreadsheet feed.
  115. *
  116. * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
  117. * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
  118. */
  119. public function getSpreadsheetFeed($location = null)
  120. {
  121. if ($location == null) {
  122. $uri = self::SPREADSHEETS_FEED_URI;
  123. } else if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  124. if ($location->getDocumentType() == null) {
  125. $location->setDocumentType('spreadsheets');
  126. }
  127. $uri = $location->getQueryUrl();
  128. } else {
  129. $uri = $location;
  130. }
  131. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetFeed');
  132. }
  133. /**
  134. * Gets a spreadsheet entry.
  135. *
  136. * @param string $location A DocumentQuery or a URI specifying the entry location.
  137. * @return SpreadsheetEntry
  138. */
  139. public function getSpreadsheetEntry($location)
  140. {
  141. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  142. if ($location->getDocumentType() == null) {
  143. $location->setDocumentType('spreadsheets');
  144. }
  145. $uri = $location->getQueryUrl();
  146. } else {
  147. $uri = $location;
  148. }
  149. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetEntry');
  150. }
  151. /**
  152. * Gets a worksheet feed.
  153. *
  154. * @param mixed $location A DocumentQuery, SpreadsheetEntry, or a string URI
  155. * @return Zend_Gdata_Spreadsheets_WorksheetFeed The feed of worksheets
  156. */
  157. public function getWorksheetFeed($location)
  158. {
  159. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  160. if ($location->getDocumentType() == null) {
  161. $location->setDocumentType('worksheets');
  162. }
  163. $uri = $location->getQueryUrl();
  164. } else if ($location instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry) {
  165. $uri = $location->getLink(self::WORKSHEETS_FEED_LINK_URI)->href;
  166. } else {
  167. $uri = $location;
  168. }
  169. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_WorksheetFeed');
  170. }
  171. /**
  172. * Gets a worksheet entry.
  173. *
  174. * @param string $location A DocumentQuery or a URI specifying the entry location.
  175. * @return WorksheetEntry
  176. */
  177. public function GetWorksheetEntry($location)
  178. {
  179. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  180. if ($location->getDocumentType() == null) {
  181. $location->setDocumentType('worksheets');
  182. }
  183. $uri = $location->getQueryUrl();
  184. } else {
  185. $uri = $location;
  186. }
  187. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_WorksheetEntry');
  188. }
  189. /**
  190. * Gets a cell feed.
  191. *
  192. * @param string $location A CellQuery, WorksheetEntry or a URI specifying the feed location.
  193. * @return CellFeed
  194. */
  195. public function getCellFeed($location)
  196. {
  197. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  198. $uri = $location->getQueryUrl();
  199. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  200. $uri = $location->getLink(self::CELL_FEED_LINK_URI)->href;
  201. } else {
  202. $uri = $location;
  203. }
  204. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_CellFeed');
  205. }
  206. /**
  207. * Gets a cell entry.
  208. *
  209. * @param string $location A CellQuery or a URI specifying the entry location.
  210. * @return CellEntry
  211. */
  212. public function getCellEntry($location)
  213. {
  214. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  215. $uri = $location->getQueryUrl();
  216. } else {
  217. $uri = $location;
  218. }
  219. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_CellEntry');
  220. }
  221. /**
  222. * Gets a list feed.
  223. *
  224. * @param mixed $location A ListQuery, WorksheetEntry or string URI specifying the feed location.
  225. * @return ListFeed
  226. */
  227. public function getListFeed($location)
  228. {
  229. if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) {
  230. $uri = $location->getQueryUrl();
  231. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  232. $uri = $location->getLink(self::LIST_FEED_LINK_URI)->href;
  233. } else {
  234. $uri = $location;
  235. }
  236. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_ListFeed');
  237. }
  238. /**
  239. * Gets a list entry.
  240. *
  241. * @param string $location A ListQuery or a URI specifying the entry location.
  242. * @return ListEntry
  243. */
  244. public function getListEntry($location)
  245. {
  246. if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) {
  247. $uri = $location->getQueryUrl();
  248. } else {
  249. $uri = $location;
  250. }
  251. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_ListEntry');
  252. }
  253. /**
  254. * Updates an existing cell.
  255. *
  256. * @param int $row The row containing the cell to update
  257. * @param int $col The column containing the cell to update
  258. * @param int $inputValue The new value for the cell
  259. * @param string $key The key for the spreadsheet to be updated
  260. * @param string $wkshtId (optional) The worksheet to be updated
  261. * @return CellEntry The updated cell entry.
  262. */
  263. public function updateCell($row, $col, $inputValue, $key, $wkshtId = 'default')
  264. {
  265. $cell = 'R'.$row.'C'.$col;
  266. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  267. $query->setSpreadsheetKey($key);
  268. $query->setWorksheetId($wkshtId);
  269. $query->setCellId($cell);
  270. $entry = $this->getCellEntry($query);
  271. $entry->setCell(new Zend_Gdata_Spreadsheets_Extension_Cell(null, $row, $col, $inputValue));
  272. $response = $entry->save();
  273. return $response;
  274. }
  275. /**
  276. * Inserts a new row with provided data.
  277. *
  278. * @param array $rowData An array of column header to row data
  279. * @param string $key The key of the spreadsheet to modify
  280. * @param string $wkshtId (optional) The worksheet to modify
  281. * @return ListEntry The inserted row
  282. */
  283. public function insertRow($rowData, $key, $wkshtId = 'default')
  284. {
  285. $newEntry = new Zend_Gdata_Spreadsheets_ListEntry();
  286. $newCustomArr = array();
  287. foreach ($rowData as $k => $v) {
  288. $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
  289. $newCustom->setText($v)->setColumnName($k);
  290. $newEntry->addCustom($newCustom);
  291. }
  292. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  293. $query->setSpreadsheetKey($key);
  294. $query->setWorksheetId($wkshtId);
  295. $feed = $this->getListFeed($query);
  296. $editLink = $feed->getLink('http://schemas.google.com/g/2005#post');
  297. return $this->insertEntry($newEntry->saveXML(), $editLink->href, 'Zend_Gdata_Spreadsheets_ListEntry');
  298. }
  299. /**
  300. * Updates an existing row with provided data.
  301. *
  302. * @param ListEntry $entry The row entry to update
  303. * @param array $newRowData An array of column header to row data
  304. */
  305. public function updateRow($entry, $newRowData)
  306. {
  307. $newCustomArr = array();
  308. foreach ($newRowData as $k => $v) {
  309. $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
  310. $newCustom->setText($v)->setColumnName($k);
  311. $newCustomArr[] = $newCustom;
  312. }
  313. $entry->setCustom($newCustomArr);
  314. return $entry->save();
  315. }
  316. /**
  317. * Deletes an existing row .
  318. *
  319. * @param ListEntry $entry The row to delete
  320. */
  321. public function deleteRow($entry)
  322. {
  323. $entry->delete();
  324. }
  325. /**
  326. * Returns the content of all rows as an associative array
  327. *
  328. * @param mixed $location A ListQuery or string URI specifying the feed location.
  329. * @return array An array of rows. Each element of the array is an associative array of data
  330. */
  331. public function getSpreadsheetListFeedContents($location)
  332. {
  333. $listFeed = $this->getListFeed($location);
  334. $listFeed = $this->retrieveAllEntriesForFeed($listFeed);
  335. $spreadsheetContents = array();
  336. foreach ($listFeed as $listEntry) {
  337. $rowContents = array();
  338. $customArray = $listEntry->getCustom();
  339. foreach ($customArray as $custom) {
  340. $rowContents[$custom->getColumnName()] = $custom->getText();
  341. }
  342. $spreadsheetContents[] = $rowContents;
  343. }
  344. return $spreadsheetContents;
  345. }
  346. /**
  347. * Returns the content of all cells as an associative array, indexed
  348. * off the cell location (ie 'A1', 'D4', etc). Each element of
  349. * the array is an associative array with a 'value' and a 'function'.
  350. * Only non-empty cells are returned by default. 'range' is the
  351. * value of the 'range' query parameter specified at:
  352. * http://code.google.com/apis/spreadsheets/reference.html#cells_Parameters
  353. *
  354. * @param mixed $location A CellQuery, WorksheetEntry or a URL (w/o query string) specifying the feed location.
  355. * @param string $range The range of cells to retrieve
  356. * @param boolean $empty Whether to retrieve empty cells
  357. * @return array An associative array of cells
  358. */
  359. public function getSpreadsheetCellFeedContents($location, $range = null, $empty = false)
  360. {
  361. $cellQuery = null;
  362. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  363. $cellQuery = $location;
  364. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  365. $url = $location->getLink(self::CELL_FEED_LINK_URI)->href;
  366. $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
  367. } else {
  368. $url = $location;
  369. $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
  370. }
  371. if ($range != null) {
  372. $cellQuery->setRange($range);
  373. }
  374. $cellQuery->setReturnEmpty($empty);
  375. $cellFeed = $this->getCellFeed($cellQuery);
  376. $cellFeed = $this->retrieveAllEntriesForFeed($cellFeed);
  377. $spreadsheetContents = array();
  378. foreach ($cellFeed as $cellEntry) {
  379. $cellContents = array();
  380. $cell = $cellEntry->getCell();
  381. $cellContents['formula'] = $cell->getInputValue();
  382. $cellContents['value'] = $cell->getText();
  383. $spreadsheetContents[$cellEntry->getTitle()->getText()] = $cellContents;
  384. }
  385. return $spreadsheetContents;
  386. }
  387. /**
  388. * Alias for getSpreadsheetFeed
  389. *
  390. * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
  391. * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
  392. */
  393. public function getSpreadsheets($location = null)
  394. {
  395. return $this->getSpreadsheetFeed($location = null);
  396. }
  397. }