"Google Spreadsheet to DB" pulls Google Spreadsheet data via Google’s API and saves it in your wordpress database.
- PHP 5.5+
- Composer
$ cd /path-to-your/wp-content/plugins/
$ git clone git@github.com:sectsect/google-spreadsheet-to-db.git
$ cd google-spreadsheet-to-db/functions/composer/
$ composer install
That's it:ok_hand:
By default, a new spreadsheet cannot be accessed via Google’s API. We’ll need to go to your Google APIs console and create a new project and set it up to expose your Spreadsheets’ data.
- Go to the Google APIs Console.
- Create a new project.
- Click Enable API. Search for and enable the Google Drive API.
- Create credentials for a Web Server to access Application Data.
- Name the service account and grant it a Project Role of Editor.
- Download the JSON file.
- Copy the JSON file to your app directory and rename it to
client_secret.json
⚠️ Setclient_secret.json
in a location to deny web access on your server.
We now have a big chunk of authentication information, including what Google calls a client_email
, which uniquely represents this OAuth service account.
Grab the value of client_email
from your client_secret.json
, and head back to your spreadsheet. Click the Share button in the top right, and paste the client_email
value into the field to give it edit rights.
Hit send. That’s it! 👌
- Go to
Settings
->Google Spreadsheet to DB
on your wordpress admin panel. - Set the following values and save it once.
- The absolute path to
client_secret.json
- Spreadsheet name
- Single Sheet name
- Data format to be stored in database
- json_encode
- json_encode (JSON_UNESCAPED_UNICODE)
- Click the
Data import from Google Spreadsheet
button. 🎉
You can edit the array got from Google API with add_filter( 'google_ss2db_before_save', $function_to_add )
in your functions.php before saving to database.
add_filter( 'google_ss2db_before_save', function ( $array ) {
// Do something...
return $return;
} );
new Google_Spreadsheet_To_DB_Query();
Parameter | Type | Notes | Default Value | |
---|---|---|---|---|
where | array | id or date |
array() |
|
key | string | id or date |
false |
|
value | int | e.g. 3 / 2017-07-24 18:46:23 |
false |
|
orderby | string | date or id |
date |
|
order | string | DESC or ASC |
DESC |
|
limit | int | number of row to get | All Data 📝 You can also use -1 to get all data. |
|
offset | int | number of row to displace or pass over | 0 |
$sheets = new Google_Spreadsheet_To_DB_Query();
$rows = $sheets->getrow();
foreach ( $rows as $row ) {
$id = $row->id;
$date = $row->date;
$val = json_decode( $row->value );
}
$args = array(
'orderby' => 'id',
'order' => 'ASC',
'limit' => 3,
'offset' => 3,
);
$sheets = new Google_Spreadsheet_To_DB_Query( $args );
$rows = $sheets->getrow();
foreach ( $rows as $row ) {
$id = $row->id;
$date = $row->date;
$val = json_decode( $row->value );
}
$args = array(
'where' => array(
'key' => 'id',
'value' => 3
),
);
$sheets = new Google_Spreadsheet_To_DB_Query( $args );
$rows = $sheets->getrow();
foreach ( $rows as $row ) {
$id = $row->id;
$date = $row->date;
$val = json_decode( $row->value );
}
- Tested on WP v4.8.0
-
This plugin saves Spreadsheet's data to the global area, not to each post. If you want to have Spredsheet data for individual posts, you can link data
ID
with custom fields. -
The data is added and stored in the
wp_google_ss2db
table as a JSON-encoded array.id date title value 1 2017-12-31 00:00:00 My Spreadsheet {"area":{"a":["brooklyn","bronx","Queens","Manhattan"],"b":["brooklyn","bronx","Queens","Manhattan"]}}
-
This Plugin does not hosting on the wordpress.org repo in order to prevent a flood of support requests from wide audience.
See CHANGELOG file.
See LICENSE file.