Skip to content

Commit 7c87fb7

Browse files
committed
Added sorting/filtering support and also modified ListFeed to provide data exactly as supplied by google without any processing
1 parent a5ef0a9 commit 7c87fb7

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This library provides a simple interface to the Google Spreadsheet API.
44

55
There are a couple of important things to note.
66

7-
* This library requires a valid OAuth access token to work but does not provide any means of generating one. The [Google APIs Client Library for PHP](https://github.com/google/google-api-php-client) has all the functionality required for for generating and refreshing tokens so it would have been a waste of time duplicating the official google library.
7+
* This library requires a valid OAuth access token to work but does not provide any means of generating one. The [Google APIs Client Library for PHP](https://github.com/google/google-api-php-client) has all the functionality required for for generating and refreshing tokens so it would have been a waste of time duplicating the official google library. I have created a demo application which shows how to generate an OAuth token [here](https://github.com/asimlqt/php-google-oauth).
88
* You can not create spreadsheets using this (PHP Google Spreadsheet Client) library, as creating spreadsheets is not part of the Spreadsheet API and the functionality already exists in the official Google Client Library.
99

1010
I strongly recommend you read through the [official Google Spreadsheet API documentation](https://developers.google.com/google-apps/spreadsheets) to get a grasp of the concepts.
@@ -20,7 +20,7 @@ Using [composer](https://getcomposer.org/) is the recommended way to install it.
2020
```json
2121
{
2222
"require": {
23-
"asimlqt/php-google-spreadsheet-client": "2.2.*"
23+
"asimlqt/php-google-spreadsheet-client": "2.3.*"
2424
}
2525
}
2626
```
@@ -131,6 +131,17 @@ foreach ($listFeed->getEntries() as $entry) {
131131

132132
The getValues() method returns an associative array where the keys are the column names and the values are the cell content.
133133

134+
> Note: The Google api converts the column headers to lower case so the column headings might not appear to be the same as what you see in Google Drive using your browser.
135+
136+
> Note: If there is data for a particular row which does not have a column header then Google randomly generates a header and as far as i know it always begins with an underscore. Bear in mind that this is not generated by this library.
137+
138+
You can also sort and filter the data so you only retrieve what is required, this is expecially useful for large worksheets.
139+
140+
```php
141+
$listFeed = $worksheet->getListFeed(array("sq" => "age > 45", "reverse" => "true"));
142+
```
143+
To find out all the available options visit [https://developers.google.com/google-apps/spreadsheets/#sorting_rows](https://developers.google.com/google-apps/spreadsheets/#sorting_rows).
144+
134145
### Adding a list row
135146

136147
```php
@@ -145,6 +156,8 @@ $row = array('name'=>'John', 'age'=>25);
145156
$listFeed->insert($row);
146157
```
147158

159+
> When adding or updating a row the column headers need to match exactly what was returned by the Google API, not what you see in Google Drive.
160+
148161
### Updating a list row
149162

150163
```php

src/Google/Spreadsheet/ListFeed.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ListFeed
3737
/**
3838
* Constructor
3939
*
40-
* @param string $xmlStr
40+
* @param string $xmlString
4141
*/
4242
public function __construct($xmlString)
4343
{
@@ -89,44 +89,18 @@ public function getEntries()
8989
$rows = array();
9090

9191
if(count($this->xml->entry) > 0) {
92-
$colNames = $this->getColumnNames($this->xml);
93-
$colNamesCount = count($colNames);
9492

9593
foreach ($this->xml->entry as $entry) {
96-
$cols = $entry->xpath('gsx:*');
97-
$vals = array();
98-
99-
foreach($cols as $col) {
100-
$vals[] = $col->__toString();
101-
}
102-
103-
if(count($vals) < $colNamesCount) {
104-
$vals = array_pad($vals, $colNamesCount, null);
94+
$data = array();
95+
foreach($entry->xpath('gsx:*') as $col) {
96+
$data[$col->getName()] = $col->__toString();
10597
}
10698

107-
$rows[] = new ListEntry($entry, array_combine($colNames, $vals));
99+
$rows[] = new ListEntry($entry, $data);
108100
}
109101
}
102+
110103
return $rows;
111104
}
112105

113-
/**
114-
* Get the column names
115-
*
116-
* @param \SimpleXMLElement $xml
117-
*
118-
* @return array
119-
*/
120-
public function getColumnNames(SimpleXMLElement $xml = null)
121-
{
122-
if($xml === null) {
123-
$xml = $this->xml;
124-
}
125-
126-
$ret = array();
127-
foreach($xml->entry->xpath('gsx:*') as $col) {
128-
$ret[] = $col->getName();
129-
}
130-
return $ret;
131-
}
132-
}
106+
}

src/Google/Spreadsheet/Worksheet.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,18 @@ public function getColCount()
105105
/**
106106
* Get the list feed of this worksheet
107107
*
108+
* @param array $query add additional query params to the url to sort/filter the results
109+
*
108110
* @return \Google\Spreadsheet\List\Feed
109111
*/
110-
public function getListFeed()
112+
public function getListFeed(array $query = array())
111113
{
112-
$res = ServiceRequestFactory::getInstance()->get($this->getListFeedUrl());
114+
$feedUrl = $this->getListFeedUrl();
115+
if(count($query) > 0) {
116+
$feedUrl .= "?" . http_build_query($query);
117+
}
118+
119+
$res = ServiceRequestFactory::getInstance()->get($feedUrl);
113120
return new ListFeed($res);
114121
}
115122

tests/Google/Spreadsheet/WorksheetTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,46 @@ public function testGetTitle()
3131
$this->assertEquals('Test', $worksheet->getTitle());
3232
}
3333

34-
}
34+
public function testGetListFeedDefault()
35+
{
36+
$feedUrl = "https://spreadsheets.google.com/feeds/list/tA3TdJ0RIVEem3xQZhG2Ceg/od8/private/full";
37+
38+
$mockServiceRequest = $this->getMockBuilder('Google\Spreadsheet\DefaultServiceRequest')
39+
->setMethods(array("get"))
40+
->disableOriginalConstructor()
41+
->getMock();
42+
43+
$mockServiceRequest
44+
->expects($this->once())
45+
->method('get')
46+
->with($this->equalTo($feedUrl))
47+
->willReturn(file_get_contents(__DIR__.'/xml/list-feed.xml'));
48+
49+
ServiceRequestFactory::setInstance($mockServiceRequest);
50+
51+
$worksheet = new Worksheet(new SimpleXMLElement(file_get_contents(__DIR__.'/xml/worksheet.xml')));
52+
$worksheet->getListFeed();
53+
}
54+
55+
public function testGetListFeedWithQuery()
56+
{
57+
$feedUrl = "https://spreadsheets.google.com/feeds/list/tA3TdJ0RIVEem3xQZhG2Ceg/od8/private/full?reverse=true&sq=age+%3E+45";
58+
59+
$mockServiceRequest = $this->getMockBuilder('Google\Spreadsheet\DefaultServiceRequest')
60+
->setMethods(array("get"))
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$mockServiceRequest
65+
->expects($this->once())
66+
->method('get')
67+
->with($this->equalTo($feedUrl))
68+
->willReturn(file_get_contents(__DIR__.'/xml/list-feed.xml'));
69+
70+
ServiceRequestFactory::setInstance($mockServiceRequest);
71+
72+
$worksheet = new Worksheet(new SimpleXMLElement(file_get_contents(__DIR__.'/xml/worksheet.xml')));
73+
$worksheet->getListFeed(array("reverse" => "true", "sq" => "age > 45"));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)