diff --git a/.gitignore b/.gitignore index fc98dac..327a86e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ composer.lock *.swp ~* *~ + +### macOS ### +*.DS_Store +.AppleDouble +.LSOverride \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 18bf015..c8affff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index a426c78..d47767a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Google/Spreadsheet/Client.php b/src/Google/Spreadsheet/Client.php index b5de517..56e9d12 100644 --- a/src/Google/Spreadsheet/Client.php +++ b/src/Google/Spreadsheet/Client.php @@ -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 ? @@ -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; } /** diff --git a/src/Google/Spreadsheet/Sheet.php b/src/Google/Spreadsheet/Sheet.php index 31a5ca4..4e0e1e2 100644 --- a/src/Google/Spreadsheet/Sheet.php +++ b/src/Google/Spreadsheet/Sheet.php @@ -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"): @@ -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 *