Ghostsheet

Simple Google Spreadsheet Loader for PHP and Ajax

Download .zip Download .tar.gz View on GitHub

Ghostsheet

Simple Google Spreadsheet Loader for PHP and Ajax

Feature

  • Load Google Spreadsheet and parse it to easy-to-read object data
  • Save it as cache file
  • Having an interface for Ajax or JSONP

Basic Usage

Create Spreadsheet

Example :

name age:integer email 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)

Get Spreadsheet ID

Get your spreadsheet id from URL

  1. Open "File" > "Publish to the web" dialog
  2. In "Get a link to the published data" section, select "RSS"
  3. You can get url like below
https://spreadsheets.google.com/feeds/cells/XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/yyY/public/basic?alt=rss

Ths string "XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/yyY" is used as your sheet's ID

Load it

require "the/path/to/Ghostsheet.php";
$gs = new Ghostsheet(array(
    "cacheDir" => "./gscache/"
));
$data = $gs->load("XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/yyY");

Now, $data has 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),
        ...
    )
);

Ajax

Ghostsheet has an interface for AJAX request.

# ajax.php
$gs = new Ghostsheet();
$gs->ajax($_GET);

This will respond with JSON for the passed parameter. If no arguments, this uses $_GET as default.

Example for jQuery :

$.getJSON("ajax.php", {id : "XXxxxxXXXxxxXxXxxxxXxxxxxXXXxXXXxXxxXXXXXXXx/yyY", cache : false})
.then(function(data){
    var items = data.items;
});

Configure

Configure options with config() or set().

$gs->set("cacheDir", "./mycache/");
$gs->config(array("timeout", 60));

API Doc

» Learn More About Ghostsheet

Change Log

» Change Log


Author

mach3