Zend_Gdata_Spreadsheets.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.gdata.spreadsheets">
  4. <title>Using Google Spreadsheets</title>
  5. <para>
  6. The Google Spreadsheets data <acronym>API</acronym> allows client applications to view
  7. and update Spreadsheets content in the form of Google data <acronym>API</acronym> feeds.
  8. Your client application can request a list of a user's spreadsheets,
  9. edit or delete content in an existing Spreadsheets worksheet, and
  10. query the content in an existing Spreadsheets worksheet.
  11. </para>
  12. <para>
  13. See
  14. <ulink url="http://code.google.com/apis/spreadsheets/overview.html">http://code.google.com/apis/spreadsheets/overview.html</ulink>
  15. for more information about the Google Spreadsheets <acronym>API</acronym>.
  16. </para>
  17. <sect2 id="zend.gdata.spreadsheets.creating">
  18. <title>Create a Spreadsheet</title>
  19. <para>
  20. The Spreadsheets data <acronym>API</acronym> does not currently provide a way to
  21. programmatically create or delete a spreadsheet.
  22. </para>
  23. </sect2>
  24. <sect2 id="zend.gdata.spreadsheets.listspreadsheets">
  25. <title>Get a List of Spreadsheets</title>
  26. <para>
  27. You can get a list of spreadsheets for a particular user by using
  28. the <code>getSpreadsheetFeed</code> method of the Spreadsheets
  29. service. The service will return a
  30. <classname>Zend_Gdata_Spreadsheets_SpreadsheetFeed</classname> object
  31. containing a list of spreadsheets associated with the authenticated
  32. user.
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  36. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  37. $spreadsheetService = new Zend_Gdata_Spreadsheets($client);
  38. $feed = $spreadsheetService->getSpreadsheetFeed();
  39. ]]></programlisting>
  40. </sect2>
  41. <sect2 id="zend.gdata.spreadsheets.listworksheets">
  42. <title>Get a List of Worksheets</title>
  43. <para>
  44. A given spreadsheet may contain multiple worksheets. For each
  45. spreadsheet, there's a worksheets metafeed listing all the
  46. worksheets in that spreadsheet.
  47. </para>
  48. <para>
  49. Given the spreadsheet key from the &lt;id&gt; of a
  50. <classname>Zend_Gdata_Spreadsheets_SpreadsheetEntry</classname>
  51. object you've already retrieved, you can fetch a feed
  52. containing a list of worksheets associated with that spreadsheet.
  53. </para>
  54. <programlisting language="php"><![CDATA[
  55. $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
  56. $query->setSpreadsheetKey($spreadsheetKey);
  57. $feed = $spreadsheetService->getWorksheetFeed($query);
  58. ]]></programlisting>
  59. <para>
  60. The resulting <classname>Zend_Gdata_Spreadsheets_WorksheetFeed</classname>
  61. object feed represents the response from the server. Among other
  62. things, this feed contains a list of
  63. <classname>Zend_Gdata_Spreadsheets_WorksheetEntry </classname>
  64. objects (<code>$feed->entries</code>), each of which represents a
  65. single worksheet.
  66. </para>
  67. </sect2>
  68. <sect2 id="zend.gdata.spreadsheets.listfeeds">
  69. <title>Interacting With List-based Feeds</title>
  70. <para>
  71. A given worksheet generally contains multiple rows, each
  72. containing multiple cells. You can request data from the
  73. worksheet either as a list-based feed, in which each entry
  74. represents a row, or as a cell-based feed, in which each
  75. entry represents a single cell. For information on cell-based
  76. feeds, see <link linkend="zend.gdata.spreadsheets.cellfeeds">Interacting with cell-based feeds</link>.
  77. </para>
  78. <para>
  79. The following sections describe how to get a list-based feed,
  80. add a row to a worksheet, and send queries with various query
  81. parameters.
  82. </para>
  83. <para>
  84. The list feed makes some assumptions about how the data is laid
  85. out in the spreadsheet.
  86. </para>
  87. <para>
  88. In particular, the list feed treats the first row of the
  89. worksheet as a header row; Spreadsheets dynamically creates
  90. <acronym>XML</acronym> elements named after the contents of header-row cells.
  91. Users who want to provide Gdata feeds should not put any data
  92. other than column headers in the first row of a worksheet.
  93. </para>
  94. <para>
  95. The list feed contains all rows after the first row up to the
  96. first blank row. The first blank row terminates the data set.
  97. If expected data isn't appearing in a feed, check the worksheet
  98. manually to see whether there's an unexpected blank row in the
  99. middle of the data. In particular, if the second row of the
  100. spreadsheet is blank, then the list feed will contain no data.
  101. </para>
  102. <para>
  103. A row in a list feed is as many columns wide as the worksheet itself.
  104. </para>
  105. <sect3 id="zend.gdata.spreadsheets.listfeeds.get">
  106. <title>Get a List-based Feed</title>
  107. <para>
  108. To retrieve a worksheet's list feed, use the
  109. <code>getListFeed</code> method of the Spreadsheets service.
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  113. $query->setSpreadsheetKey($spreadsheetKey);
  114. $query->setWorksheetId($worksheetId);
  115. $listFeed = $spreadsheetService->getListFeed($query);
  116. ]]></programlisting>
  117. <para>
  118. The resulting <classname>Zend_Gdata_Spreadsheets_ListFeed</classname>
  119. object <varname>$listfeed</varname> represents a response from the
  120. server. Among other things, this feed contains an array of
  121. <classname>Zend_Gdata_Spreadsheets_ListEntry</classname> objects
  122. (<code>$listFeed->entries</code>), each of which represents
  123. a single row in a worksheet.
  124. </para>
  125. <para>
  126. Each <classname>Zend_Gdata_Spreadsheets_ListEntry</classname> contains an
  127. array, <code>custom</code>, which contains the data for that
  128. row. You can extract and display this array:
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. $rowData = $listFeed->entries[1]->getCustom();
  132. foreach($rowData as $customEntry) {
  133. echo $customEntry->getColumnName() . " = " . $customEntry->getText();
  134. }
  135. ]]></programlisting>
  136. <para>
  137. An alternate version of this array, <code>customByName</code>,
  138. allows direct access to an entry's cells by name. This is
  139. convenient when trying to access a specific header:
  140. </para>
  141. <programlisting language="php"><![CDATA[
  142. $customEntry = $listFeed->entries[1]->getCustomByName('my_heading');
  143. echo $customEntry->getColumnName() . " = " . $customEntry->getText();
  144. ]]></programlisting>
  145. </sect3>
  146. <sect3 id="zend.gdata.spreadsheets.listfeeds.reverse">
  147. <title>Reverse-sort Rows</title>
  148. <para>
  149. By default, rows in the feed appear in the same order as the
  150. corresponding rows in the GUI; that is, they're in order by
  151. row number. To get rows in reverse order, set the reverse
  152. properties of the <classname>Zend_Gdata_Spreadsheets_ListQuery</classname>
  153. object to <constant>TRUE</constant>:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  157. $query->setSpreadsheetKey($spreadsheetKey);
  158. $query->setWorksheetId($worksheetId);
  159. $query->setReverse('true');
  160. $listFeed = $spreadsheetService->getListFeed($query);
  161. ]]></programlisting>
  162. <para>
  163. Note that if you want to order (or reverse sort) by a
  164. particular column, rather than by position in the worksheet,
  165. you can set the <code>orderby</code> value of the
  166. <classname>Zend_Gdata_Spreadsheets_ListQuery</classname> object to
  167. <code>column:&lt;the header of that column&gt;</code>.
  168. </para>
  169. </sect3>
  170. <sect3 id="zend.gdata.spreadsheets.listfeeds.sq">
  171. <title>Send a Structured Query</title>
  172. <para>
  173. You can set a <classname>Zend_Gdata_Spreadsheets_ListQuery</classname>'s
  174. <code>sq</code> value to produce a feed with entries that meet
  175. the specified criteria. For example, suppose you have a worksheet
  176. containing personnel data, in which each row represents
  177. information about a single person. You wish to retrieve all rows
  178. in which the person's name is "John" and the person's age is over
  179. 25. To do so, you would set <code>sq</code> as follows:
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  183. $query->setSpreadsheetKey($spreadsheetKey);
  184. $query->setWorksheetId($worksheetId);
  185. $query->setSpreadsheetQuery('name=John and age>25');
  186. $listFeed = $spreadsheetService->getListFeed($query);
  187. ]]></programlisting>
  188. </sect3>
  189. <sect3 id="zend.gdata.spreadsheets.listfeeds.addrow">
  190. <title>Add a Row</title>
  191. <para>
  192. Rows can be added to a spreadsheet by using the
  193. <code>insertRow</code> method of the Spreadsheet service.
  194. </para>
  195. <programlisting language="php"><![CDATA[
  196. $insertedListEntry = $spreadsheetService->insertRow($rowData,
  197. $spreadsheetKey,
  198. $worksheetId);
  199. ]]></programlisting>
  200. <para>
  201. The <varname>$rowData</varname> parameter contains an array of column
  202. keys to data values. The method returns a
  203. <classname>Zend_Gdata_Spreadsheets_SpreadsheetsEntry</classname> object
  204. which represents the inserted row.
  205. </para>
  206. <para>
  207. Spreadsheets inserts the new row immediately after the last row
  208. that appears in the list-based feed, which is to say
  209. immediately before the first entirely blank row.
  210. </para>
  211. </sect3>
  212. <sect3 id="zend.gdata.spreadsheets.listfeeds.editrow">
  213. <title>Edit a Row</title>
  214. <para>
  215. Once a <classname>Zend_Gdata_Spreadsheets_ListEntry</classname> object
  216. is fetched, its rows can be updated by using the
  217. <code>updateRow</code> method of the Spreadsheet service.
  218. </para>
  219. <programlisting language="php"><![CDATA[
  220. $updatedListEntry = $spreadsheetService->updateRow($oldListEntry,
  221. $newRowData);
  222. ]]></programlisting>
  223. <para>
  224. The <varname>$oldListEntry</varname> parameter contains the list entry
  225. to be updated. <varname>$newRowData</varname> contains an array of
  226. column keys to data values, to be used as the new row data.
  227. The method returns a
  228. <classname>Zend_Gdata_Spreadsheets_SpreadsheetsEntry</classname> object
  229. which represents the updated row.
  230. </para>
  231. </sect3>
  232. <sect3 id="zend.gdata.spreadsheets.listfeeds.deleterow">
  233. <title>Delete a Row</title>
  234. <para>
  235. To delete a row, simply invoke <code>deleteRow</code> on the
  236. <classname>Zend_Gdata_Spreadsheets</classname> object with the existing
  237. entry to be deleted:
  238. </para>
  239. <programlisting language="php"><![CDATA[
  240. $spreadsheetService->deleteRow($listEntry);
  241. ]]></programlisting>
  242. <para>
  243. Alternatively, you can call the <code>delete</code> method of
  244. the entry itself:
  245. </para>
  246. <programlisting language="php"><![CDATA[
  247. $listEntry->delete();
  248. ]]></programlisting>
  249. </sect3>
  250. </sect2>
  251. <sect2 id="zend.gdata.spreadsheets.cellfeeds">
  252. <title>Interacting With Cell-based Feeds</title>
  253. <para>
  254. In a cell-based feed, each entry represents a single cell.
  255. </para>
  256. <para>
  257. Note that we don't recommend interacting with both a cell-based
  258. feed and a list-based feed for the same worksheet at the same time.
  259. </para>
  260. <sect3 id="zend.gdata.spreadsheets.cellfeeds.get">
  261. <title>Get a Cell-based Feed</title>
  262. <para>
  263. To retrieve a worksheet's cell feed, use the
  264. <code>getCellFeed</code> method of the Spreadsheets service.
  265. </para>
  266. <programlisting language="php"><![CDATA[
  267. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  268. $query->setSpreadsheetKey($spreadsheetKey);
  269. $query->setWorksheetId($worksheetId);
  270. $cellFeed = $spreadsheetService->getCellFeed($query);
  271. ]]></programlisting>
  272. <para>
  273. The resulting <classname>Zend_Gdata_Spreadsheets_CellFeed</classname>
  274. object <varname>$cellFeed</varname> represents a response from the
  275. server. Among other things, this feed contains an array of
  276. <classname>Zend_Gdata_Spreadsheets_CellEntry</classname> objects
  277. (<code>$cellFeed>entries</code>), each of which represents
  278. a single cell in a worksheet. You can display this information:
  279. </para>
  280. <programlisting language="php"><![CDATA[
  281. foreach($cellFeed as $cellEntry) {
  282. $row = $cellEntry->cell->getRow();
  283. $col = $cellEntry->cell->getColumn();
  284. $val = $cellEntry->cell->getText();
  285. echo "$row, $col = $val\n";
  286. }
  287. ]]></programlisting>
  288. </sect3>
  289. <sect3 id="zend.gdata.spreadsheets.cellfeeds.cellrangequery">
  290. <title>Send a Cell Range Query</title>
  291. <para>
  292. Suppose you wanted to retrieve the cells in the first column
  293. of a worksheet. You can request a cell feed containing only
  294. this column as follows:
  295. </para>
  296. <programlisting language="php"><![CDATA[
  297. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  298. $query->setMinCol(1);
  299. $query->setMaxCol(1);
  300. $query->setMinRow(2);
  301. $feed = $spreadsheetService->getCellsFeed($query);
  302. ]]></programlisting>
  303. <para>
  304. This requests all the data in column 1, starting with row 2.
  305. </para>
  306. </sect3>
  307. <sect3 id="zend.gdata.spreadsheets.cellfeeds.updatecell">
  308. <title>Change Contents of a Cell</title>
  309. <para>
  310. To modify the contents of a cell, call
  311. <code>updateCell</code> with the row, column,
  312. and new value of the cell.
  313. </para>
  314. <programlisting language="php"><![CDATA[
  315. $updatedCell = $spreadsheetService->updateCell($row,
  316. $col,
  317. $inputValue,
  318. $spreadsheetKey,
  319. $worksheetId);
  320. ]]></programlisting>
  321. <para>
  322. The new data is placed in the specified cell in the worksheet.
  323. If the specified cell contains data already, it will be
  324. overwritten. Note: Use <code>updateCell</code> to change
  325. the data in a cell, even if the cell is empty.
  326. </para>
  327. </sect3>
  328. </sect2>
  329. </sect1>