Zend_Gdata_Spreadsheets.xml 16 KB

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