This is basically a README but oh well it might be useful to someone.
I created googlesheets-sql-sync to let users manually insert data using Google Sheets while having the power of all available SQL tooling for further processing.
googlesheets-sql-sync uses JDBC and bundles the PostgreSQL driver.
Additional drivers can be added any time.
If you would like to add support for SQLite, MySQL or any other SQL database, open an issue and it can probably be added in no time.
Assumptions and simplifications
To simplify the task of synchronisation, the following assumptions are made:
- Sync happens not too frequently. Think minutes, not milliseconds.
- Number of tables to sync is not too high. Maybe a hundred but not a million.
- The Google Sheets are not too big. They might contain a thousand rows but not millions.
- Headers are mostly stable. It is an exception to rename, add or delete columns.
This allows for a few simplifications in the implementation:
- Do all specified sync tasks in sequence. There is enough time for this.
- Limit API requests to Google's API to one a second to prevent rate limits as much as possible.
- Truncate each table on every sync to ensure all changes are applied.
- Log error when table schema doesn't match headers from sheet and require the user to check error manually. Most likely the user simply drops the table in this case and moves on.
- Re-read the config file from disk before each sync interval, which allows for adjusting the config while the system is running.
Setup
Installation
- Make sure you have Java 8+ installed. Check by running
java -version
- Download latest
googlesheets-sql-sync.jar
from Github.
Setup Google Application
- Create a new Project or work in an existing one
- Enable the Sheets API
- Setup your app's OAuth consent screen
If this is an organisation-internal service, you most likely want to set it as internal and select the scope
spreadsheets.readonly
. - Create a new OAuth client ID or use an existing one
- Set "Application type" to "Web application"
- Set at least one correct "Authorized redirect URI". To run googlesheets-sql-sync on your local machine with default settings use http://localhost:9955/oauth
- Keep "Client ID" and "Client secret" handy for later
Usage
- Create an empty config file
java -jar googlesheets-sql-sync.jar --init
-
Now fill out the missing information in the config file.
- Use your Google credentials from above.
- Specify at least one target and one sheet using that target.
- You can find more DB options in the JDBC docs.
- Name the
table
as you wish for it to appear in your database. - To get a
spreadsheet_id
, open one of your Google Sheets and copy the part between/d/
and/edit
from the URL bar in your Browser. - Specify the
range
using theA1:Z10
. Skip the number to select all rows - likeA:ZZ
. You can also specify a sheet if your spreadsheet contains multiple sheets by prefixing th range likeSomeSheet!A:ZZ
. For example, thespreadsheet_id
forhttps://docs.google.com/spreadsheets/d/1q5BNyL7-FnApmkjq45HlKPK-W-pdEmTrtpz0iaHm8p0/edit#gid=0
is1q5BNyL7-FnApmkjq45HlKPK-W-pdEmTrtpz0iaHm8p0
.
Start the program with:
java -jar googlesheets-sql-sync.jar
You will be prompted to visit an OAuth URL to authorize and connect your Google Account.
After successful authorization, a first sync is triggered
and further ones will occur in the specified interval.
Running without Server
Often you don't want to open up another port just for OAuth of a small sync tool.
To work around this you can run java -jar googlesheets-sql-sync.jar --auth-only
on your local machine, then copy the generated googlesheets_sql_sync.auth.json
file to your server and on the server run java -jar googlesheets-sql-sync.jar --no-server
Customization
The program can be configured using command line flags. To see available options, run:
java -jar googlesheets-sql-sync.jar --help
Pitfalls
- When you authenticate a Google OAuth app, then throw away your
.auth.json
file and try to re-authenticate, Google for some reason will only send youaccess_token
andexpires_in
, norefresh_token
. To fix this go to https://myaccount.google.com/permissions remove the app's permission and try again.
Let me know if you run into any issues or if you have any suggestions for improvements.
Top comments (4)
This awesome! I actually dealt with this problem a ton in the past and decided to create a product around it. SeekWell (seekwell.io/) does a lot of this stuff for someone that wouldn't want to fool with JAR's, etc. Nice work!
Nice!
For us the main reason to do this ourselves was to not go through yet another SaaS ETL service for such a little and specific task. But for more complex use cases, it's definitely a good idea to consider one of the many automation and ETL solutions out there.
This is such a hack. Kudos
Such an interesting way to use google docs - I think this can actually be very useful for giving non-tech people to specific database tables. Thanks for sharing!