Wallogit.com
2017 © Pedro Peláez
PHP Library for accessing google spreadsheet content via SheetDB. https://sheetdb.io
The SheetDB PHP Library is available through Composer, (*1)
composer require sheetdb/sheetdb-php
At any time you can play with our test API here: https://sheetdb.io/api/v1/58f61be4dda40, (*2)
You can also go to Google Sheets and play with it: https://docs.google.com/spreadsheets/u/2/d/1mrsgBk4IAdSs8Ask5H1z3bWYDlPTKplDIU_FzyktrGk/edit The spreadsheet resets every hour., (*3)
In order to instantiate SheetDB connection you need to give an api id. You can find it in SheetDB Dashboard., (*4)
require('vendor/autoload.php');
use SheetDB\SheetDB;
$sheetdb = new SheetDB('58f61be4dda40');
$response = $sheetdb->get(); // returns all spreadsheets data
$response = $sheetdb->keys(); // returns all spreadsheets key names
$response = $sheetdb->name(); // returns name of a spreadsheet document
You can use search method to find only specific rows. You have 2 options. Search rows that meets all of the conditions or search rows that meets at least one of the conditions., (*5)
You can use second parameter if you want the search to be case sensitive (it is boolean). By default it is not case sensitive., (*6)
$response = $sheetdb->search(['name'=>'Steve','age'=>'22']); // returns when name="Steve" AND age=22 $response = $sheetdb->searchOr(['name'=>'Steve','age'=>'19']); // returns when name="Steve" OR age=19 $response = $sheetdb->search(['name'=>'Steve'], true); // returns when name="Steve", this query is case sensitive
If you want to create a new row you'll need to pass an array with key_name => value. If you want to create multiple rows that should be an array of arrays., (*7)
$sheetdb->create(['name'=>'Mark','age'=>'35']);
$sheetdb->create([
['name'=>'Chris','age'=>'34'],
['name'=>'Amanda','age'=>'29'],
]);
First two parameters are key and value that need to match, third argument is array similar to the create one., (*8)
Fourth parameter is optional. If it's true not specified values will be emptied., (*9)
$sheetdb->update('name','Chris',['age'=>'33']); // it will change only age
$sheetdb->update('name','Chris',['age'=>'33'],true); // it will update age to 33 but all other values will be emptied, in this case a name
Update for various queries. First argument is data itself. Each object in it should have a query key with the actual query (for example, "id=5"), and the remaining keys will be updated, as in a regular PATCH / PUT request., (*10)
Second parameter is optional. If it's true not specified values will be emptied., (*11)
$sheetdb->batchUpdate([
[
'query' => 'id=1',
'age' => '33',
],
[
'query' => 'id=2',
'age' => '52',
]
]);
Just like in update first two parameters are key and value. Every row that will match query will be deleted., (*12)
$sheetdb->delete('name','Chris'); // will delete all rows with name = "Chris"