Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ composer.lock
*.swp
~*
*~

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Change Log

## 0.1.7

- csv: adding the possibility to get the CSV version of a Sheet

## 0.1.5

- improve: let Sheet::update() to receive condition as 1st argument
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ $client->config(array(
));
```

### Get CSV

```php
$client = Google_Spreadsheet::getClient("the/path/to/credential.json");
// Get the file by file ID
$file = $client->file("XXXxxxXXXXxxxXXXX");
// Get the sheet by title
$sheet = $file->sheet("Sheet1");
// Dump CSV
var_dump($sheet->csv());
```


## Requirement

Expand Down
5 changes: 3 additions & 2 deletions src/Google/Spreadsheet/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function config(/* $key [,$value] */){
*/
public function getAccessToken(){
$session_key = $this->config("session_key");
$token = array_key_exists($this->options["session_key"], $_SESSION) ?
$token = array_key_exists($this->options["session_key"], $_SESSION) ?
$_SESSION[$session_key] : null;
if($token){
// expired ?
Expand Down Expand Up @@ -139,7 +139,8 @@ public function request($url, $method = "GET", $headers = array(), $postBody = n
$this->cache($url, $feed);
}
}
return json_decode($feed, true);
$result = json_decode($feed, true);
return (json_last_error() === JSON_ERROR_NONE) ? $result : $feed;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Google/Spreadsheet/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __construct($meta, $client){

foreach($this->meta["link"] as $link){
switch(true){
case strstr($link["rel"], "#exportcsv"):
$this->link["exportcsv"] = $link["href"]; break;
case strstr($link["rel"], "#cellsfeed"):
$this->link["cellsfeed"] = $link["href"] . "?alt=json"; break;
case strstr($link["rel"], "#listfeed"):
Expand All @@ -50,6 +52,16 @@ public function fetch($force = false){
return $this;
}

/**
* Get a csv version of the sheet
*
* @param {Boolean} $force ... Ignore cache data or not
* @return {String} ... csv
*/
public function csv($force = false){
return $this->client->request($this->link["exportcsv"], "GET", array(), null, $force);
}

/**
* Select rows by condition
*
Expand Down