sectsect / google-spreadsheet-to-db
Pulls Google Spreadsheet data via Google’s API and saves it in your wordpress database.
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 1
Open Issues: 0
Type:wordpress-plugin
Requires
- php: >=5.5
- google/apiclient: ^2.16
Requires (Dev)
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^7.5 || ^9.5
- squizlabs/php_codesniffer: ^3.7
- szepeviktor/phpstan-wordpress: ^1.3
- wp-coding-standards/wpcs: ^3.0
- yoast/phpunit-polyfills: ^2.0
- dev-master
- v6.5.2
- v6.5.1
- v6.5.0
- v6.4.2
- v6.4.1
- v6.4.0
- v6.3.0
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.0
- v6.0.0
- v5.1.0
- v5.0.1
- v5.0.0
- v4.1.0
- v4.0.1
- v4.0.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.6.0
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/npm_and_yarn/webpack-5.94.0
- dev-feature/phpunit
- dev-feature/github-actions
- dev-feature/update-202106
- dev-typescript
- dev-dev
This package is auto-updated.
Last update: 2024-10-02 06:16:11 UTC
README
The "Google Spreadsheet to DB" plugin is designed for WordPress and facilitates the import of data from Google Sheets into a WordPress database using Google's Sheets API (v4). It supports data manipulation before saving and is configurable via a WordPress admin interface.
Features
- Data Import: Pulls data from Google Sheets and saves it directly into the WordPress database.
- Customization: Offers settings for defining constants, spreadsheet IDs, names, and configuring data formats.
- Admin Interface: Provides an admin page for easy management and configuration of the plugin settings.
Requirements
- PHP version 8.0 or higher.
- Composer for managing PHP dependencies.
Get Started
1. Clone this Repo into your wp-content/plugins
directory.
cd /path-to-your/wp-content/plugins/
git clone git@github.com:sectsect/google-spreadsheet-to-db.git
2. Remove vendor/
in .gitignore
file.
cd google-spreadsheet-to-db
nano .gitignore
- vendor/
3. Install composer packages
cd functions/composer/
composer install
4. Activate the plugin through the 'Plugins' menu in WordPress.
Settings
Getting Your Spreadsheet Ready for Programmatic Access
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 Sheets 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
- ⚠️ Set
client_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! 👌
- Set the
define()
constants for client_secret.json inwp-config.php
.
define( 'GOOGLE_SS2DB_CLIENT_SECRET_PATH', '/path/to/your/client_secret.json' );
- Go to
Settings
->Google Spreadsheet to DB
on your WordPress Admin-Panel. - Set the following values and save it once.
- Data format to be stored in database
- json_encode
- json_encode (JSON_UNESCAPED_UNICODE)
- Click the
Import from Google Spreadsheet
button. 🎉
- Spreadsheet ID
- Spreadsheet name (Optional)
- Single Sheet name
- Top Header Row
- Title (Optional)
Filters
Filtering the Array
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 ( $row, $worksheet_id, $worksheet_name, $sheet_name ) { // Example if ( $worksheet_name === 'My Spreadsheet' && $sheet_name === 'Sheet1' ) { // Do something. return $something; } return $row; }, 10, 3 );
And also use add_filter('google_ss2db_after_save', $return_array )
to perform any processing with the return value.
add_filter( 'google_ss2db_after_save', function ( $data ) { if ( 'My Spreadsheet' === $data['worksheet_name'] ) { // $id = $data['id']; // $date = $data['date']; // $title = $data['title']; // $value = $data['value']; // $work_sheet_id = $data['worksheet_id']; // $work_sheet_name = $data['worksheet_name']; // $sheet_name = $data['sheet_name']; // $result = $data['result']; // `int|false` The number of rows inserted, or false on error. // Example my_callback( $data ); } });
APIs
new Google_Spreadsheet_To_DB_Query();
Parameters
Usage Example
Get all rows
$sheet = new Google_Spreadsheet_To_DB_Query(); $rows = $sheet->getrow(); foreach ( $rows as $row ) { $id = $row->id; $date = $row->date; $val = json_decode( $row->value ); }
Get 3 rows from the 4th in ascending order by ID
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'limit' => 3, 'offset' => 3, ); $sheet = new Google_Spreadsheet_To_DB_Query( $args ); $rows = $sheet->getrow(); foreach ( $rows as $row ) { $id = $row->id; $date = $row->date; $val = json_decode( $row->value ); }
Get the row with specific ID
$args = array( 'where' => array( array( 'key' => 'id', 'value' => 3, ) ), );
Get 3 rows with specific Worksheet ordered by ID
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'limit' => 3, 'where' => array( array( 'key' => 'worksheet_name', 'value' => 'My Spreadsheet', 'compare' => '=' ), ), );
Get the rows larger than or equal the specified datetime
$args = array( 'where' => array( array( 'key' => 'date', 'value' => '2020-08-01 12:34:56', 'compare' => '>=', ) ), );
Get the rows with multiple conditions
$args = array( 'orderby' => 'id', 'order' => 'DESC', 'limit' => 10, 'offset' => 10, 'where' => array( 'relation' => 'AND', // or 'OR' array( 'key' => 'date', 'value' => '2020-08-01 12:34:56', 'compare' => '>=' ), array( 'key' => 'worksheet_name', 'value' => 'My Spreadsheet', 'compare' => '=' ), ), );
Notes
- Tested on WordPress v6.3.1
For Developers
-
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. -
This Plugin does not hosting on the wordpress.org repo in order to prevent a flood of support requests from wide audience.
Troubleshooting
This plugin depends on Guzzle v7, which may conflict with other WordPress plugins or Composer packages using Guzzle v6.
If you encounter errors like:
Uncaught Error: Call to undefined method GuzzleHttp\Utils::chooseHandler()
This is likely due to a Guzzle version conflict. To resolve:
- Update other plugins/packages to versions compatible with Guzzle v7
- Find alternative solutions that don't conflict with this plugin's dependencies
Change log
See CHANGELOG file.
License
See LICENSE file.
✌️
A little project by @sectsect