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