Simple Google Spreadsheet Loader for PHP and Ajax
- Load Google Spreadsheet and parse it to easy-to-read object data
- Save it as cache file
- Having an interface for Ajax or JSONP
Example :
name | age:integer | active:bool | |
---|---|---|---|
john | 18 | john@example.com | true |
tom | 21 | tom@example.com | false |
- First row must be header consists of field name
- If field name has data type (as
:string
), PHP try to juggle value to the type
(If not set, value will be output as string)
Ghostsheet is able to fetch by index or name of sheet. Pass worksheet's key and index or name of the sheet you want to fetch data of.
$gs = new Ghostsheet();
$data = $gs->get("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx", 0); // Get first sheet
$data = $gs->get("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx", "product"); // Get sheet named "product"
Full ID is an identifier string formatted as "[spreadsheet-key]/[sheet-id]".
This skips a process to fetch the sheet list by API, it's much faster than fetching by index or name
$gs = new Ghostsheet();
$data = $gs->get("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/od6");
Four modes are available by specifying in third argument.
// Get "product" sheet with "load" mode
$gs->get("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx", "product", "load");
// Or Full ID
$data = $gs->get("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/od6", null, "load");
- "load" (default) Check local cache, if it's expired fetch remote data, save it as cache.
- "update" Get remote data and save it as cache, in spite of cache's lifetime.
- "cache" Get local cache data in spite of its lifetime. If cache does not exist, return null.
- "fetch" Get remote data, doesn't save it as cache.
Now, $data
has an array consists of spreadsheet contents.
array(
"id" => "https://spreadsheets.google.com/feeds/cells/XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/yyY/public/basic",
"title" => "mysheet", // your sheet's name
"updated" => "2013-05-28T10:37:51.771Z",
"items" => array(
array("name" => "John", "age" => 18, "email" => "john@example.com", "active" => true),
...
)
);
Ghostsheet has an interface for AJAX request.
# ajax.php
$gs = new Ghostsheet();
$gs->ajax($_GET);
This will respond with JSON for the passed parameters. If no arguments, this uses $_GET as default.
Example for jQuery :
$.getJSON("ajax.php", {
key: "XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx", // Spreadsheet Key or ID
name: "product", // Sheet Index or Name
mode: "load", // Load mode
})
.then(function(data){
var items = data.items;
});
Configure options with config()
$gs->config(array("cache_dir", "./mycache"));
$gs->config("cache_dir", "./mycache/");
$gs->config("cache_dir"); // Returns "./mycache/"
$gs->config(); // Returns all options
mach3